View Javadoc

1   package org.apache.maven.shared.jar.identification.exposers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.collections.Bag;
23  import org.apache.commons.collections.bag.HashBag;
24  import org.apache.maven.shared.jar.JarAnalyzer;
25  import org.apache.maven.shared.jar.identification.JarIdentification;
26  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.text.SimpleDateFormat;
30  import java.util.Date;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.jar.JarEntry;
35  
36  /**
37   * Exposer that examines a a JAR and uses the most recent timestamp as a potential version.
38   *
39   * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationExposer" role-hint="timestamp"
40   */
41  public class TimestampExposer
42      implements JarIdentificationExposer
43  {
44      public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
45      {
46          List entries = jarAnalyzer.getEntries();
47          SimpleDateFormat tsformat = new SimpleDateFormat( "yyyyMMdd", Locale.US ); //$NON-NLS-1$
48          Bag timestamps = new HashBag();
49          Iterator it = entries.iterator();
50          while ( it.hasNext() )
51          {
52              JarEntry entry = (JarEntry) it.next();
53              long time = entry.getTime();
54              String timestamp = tsformat.format( new Date( time ) );
55              timestamps.add( timestamp );
56          }
57  
58          it = timestamps.iterator();
59          String ts = "";
60          int tsmax = 0;
61          while ( it.hasNext() )
62          {
63              String timestamp = (String) it.next();
64              int count = timestamps.getCount( timestamp );
65              if ( count > tsmax )
66              {
67                  ts = timestamp;
68                  tsmax = count;
69              }
70          }
71  
72          if ( StringUtils.isNotEmpty( ts ) )
73          {
74              identification.addVersion( ts );
75          }
76      }
77  }