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.io.BufferedReader;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.jar.JarEntry;
31  
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.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Exposer that examines a a JAR for files that contain the text <code>version</code> (case-insensitive) and
40   * adds the contents as potential version(s).
41   */
42  @Singleton
43  @Named("textFile")
44  public class TextFileExposer implements JarIdentificationExposer {
45      private final Logger logger = LoggerFactory.getLogger(getClass());
46  
47      @Override
48      public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
49          List<String> textFiles = findTextFileVersions(jarAnalyzer);
50          for (String ver : textFiles) {
51              identification.addVersion(ver);
52          }
53      }
54  
55      private List<String> findTextFileVersions(JarAnalyzer jarAnalyzer) {
56          List<String> textVersions = new ArrayList<>();
57          List<JarEntry> hits = jarAnalyzer.getVersionEntries();
58  
59          for (JarEntry entry : hits) {
60              // skip this entry if it's a class file.
61              if (!entry.getName().endsWith(".class")) // $NON-NLS-1$
62              {
63                  logger.debug("Version Hit: " + entry.getName());
64                  try (InputStream is = jarAnalyzer.getEntryInputStream(entry)) {
65                      BufferedReader br = new BufferedReader(new InputStreamReader(is));
66  
67                      String line = br.readLine();
68                      // TODO: check for key=value pair.
69                      // TODO: maybe even for groupId entries.
70  
71                      logger.debug(line);
72                      if (line != null && !line.isEmpty()) {
73                          textVersions.add(line);
74                      }
75                  } catch (IOException e) {
76                      logger.warn("Unable to read line from " + entry.getName(), e);
77                  }
78              }
79          }
80          return textVersions;
81      }
82  }