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