1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
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
61 if (!entry.getName().endsWith(".class"))
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
69
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 }