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  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * Exposer that examines a a JAR and uses the most recent timestamp as a potential version.
39   */
40  @Singleton
41  @Named("timestamp")
42  public class TimestampExposer implements JarIdentificationExposer {
43      @Override
44      public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
45          List<JarEntry> entries = jarAnalyzer.getEntries();
46          SimpleDateFormat tsformat = new SimpleDateFormat("yyyyMMdd", Locale.US); // $NON-NLS-1$
47          Bag<String> timestamps = new HashBag<>();
48          for (JarEntry entry : entries) {
49              long time = entry.getTime();
50              String timestamp = tsformat.format(new Date(time));
51              timestamps.add(timestamp);
52          }
53  
54          String ts = "";
55          int tsmax = 0;
56          for (String timestamp : timestamps) {
57              int count = timestamps.getCount(timestamp);
58              if (count > tsmax) {
59                  ts = timestamp;
60                  tsmax = count;
61              }
62          }
63  
64          if (StringUtils.isNotEmpty(ts)) {
65              identification.addVersion(ts);
66          }
67      }
68  }