View Javadoc

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