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