1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Enumeration;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.jar.Attributes;
28 import java.util.jar.JarEntry;
29 import java.util.jar.JarFile;
30 import java.util.jar.Manifest;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34
35
36
37 public class JarVersion
38 {
39 private static JarEntry findEntry(JarFile jar, String regex)
40 {
41 Pattern pattern = Pattern.compile(regex);
42 Matcher matcher;
43 Enumeration<JarEntry> en = jar.entries();
44 while (en.hasMoreElements())
45 {
46 JarEntry entry = en.nextElement();
47 matcher = pattern.matcher(entry.getName());
48 if (matcher.matches())
49 {
50 return entry;
51 }
52 }
53
54 return null;
55 }
56
57 private static String getBundleVersion(Manifest manifest)
58 {
59 Attributes attribs = manifest.getMainAttributes();
60 if (attribs == null)
61 return null;
62
63 String version = attribs.getValue("Bundle-Version");
64 if (version == null)
65 return null;
66
67 return stripV(version);
68 }
69
70 private static String getMainManifestImplVersion(Manifest manifest)
71 {
72 Attributes attribs = manifest.getMainAttributes();
73 if (attribs == null)
74 return null;
75
76 String version = attribs.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
77 if (version == null)
78 return null;
79
80 return stripV(version);
81 }
82
83 private static String getMavenVersion(JarFile jar) throws IOException
84 {
85 JarEntry pomProp = findEntry(jar,"META-INF/maven/.*/pom\\.properties$");
86 if (pomProp == null)
87 return null;
88
89 InputStream stream = null;
90
91 try
92 {
93 stream = jar.getInputStream(pomProp);
94 Properties props = new Properties();
95 props.load(stream);
96
97 String version = props.getProperty("version");
98 if (version == null)
99 return null;
100
101 return stripV(version);
102 }
103 finally
104 {
105 Main.close(stream);
106 }
107 }
108
109 private static String getSubManifestImplVersion(Manifest manifest)
110 {
111 Map<String, Attributes> entries = manifest.getEntries();
112
113 for (Attributes attribs : entries.values())
114 {
115 if (attribs == null)
116 continue;
117
118 String version = attribs.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
119 if (version == null)
120 continue;
121
122 return stripV(version);
123 }
124
125 return null;
126 }
127
128 public static String getVersion(File file)
129 {
130 try
131 {
132 JarFile jar = new JarFile(file);
133
134 String version = null;
135
136 Manifest manifest = jar.getManifest();
137
138 version = getMainManifestImplVersion(manifest);
139 if (version != null)
140 return version;
141
142 version = getSubManifestImplVersion(manifest);
143 if (version != null)
144 return version;
145
146 version = getBundleVersion(manifest);
147 if (version != null)
148 return version;
149
150 version = getMavenVersion(jar);
151 if (version != null)
152 return version;
153
154 return "(not specified)";
155 }
156 catch (IOException e)
157 {
158 return "(error: " + e.getClass().getSimpleName() + " " + e.getMessage() + ")";
159 }
160 }
161
162 private static String stripV(String version)
163 {
164 if (version.charAt(0) == 'v')
165 return version.substring(1);
166
167 return version;
168 }
169 }