98747f7e002e76c5150e95cf80c3c3f7198eb22a
[jalview.git] / utils / getJavaVersion.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 import java.io.DataInputStream;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.jar.JarEntry;
27 import java.util.jar.JarInputStream;
28
29 public class getJavaVersion
30 {
31 /**
32  * Takes a set of Jars and/or class files as arguments. Reports the java version for the classes
33  */
34
35   public static void main(String[] args) throws IOException
36   {
37     Hashtable observed=new Hashtable();
38     for (int i = 0; i < args.length; i++)
39       {
40         checkClassVersion(args[i], observed);
41       }
42     printVersions(observed, System.out);
43   }
44   public static void printVersions(Hashtable observed, java.io.PrintStream outs)
45   {
46     if (observed.size()>0)
47     {
48       int space=0;
49       String key=null;
50       for (Enumeration keys = observed.keys(); keys.hasMoreElements(); ) {
51         key = (String) keys.nextElement();
52         if (space++>0)
53         {
54           outs.print(" ");
55         }
56         outs.print(key);
57       }
58       outs.print("\n");
59     }
60   }
61
62   private static void checkClassVersion(String filename, Hashtable observed)
63           throws IOException
64   {
65     String version = checkClassVersion(filename);
66     if (version == null)
67     {
68 //      System.err.println("Reading "+filename+" as  jar:");
69       try
70       {
71         JarInputStream jis = new JarInputStream(new FileInputStream(
72                 filename));
73         JarEntry entry;
74         Hashtable perjar=new Hashtable();
75         while ((entry = jis.getNextJarEntry()) != null)
76         {
77           if (entry != null)
78           {
79             if (entry.getName().endsWith(".class"))
80             {
81               try
82               {
83                 version = getVersion(new DataInputStream(jis));
84                 if (version != null)
85                 {
86                   addVersion(version, observed);
87                   addVersion(version, perjar);
88                 }
89               } catch (Exception e)
90               {
91
92               }
93             }
94           }
95         }
96         System.err.println("Jar : "+filename);
97         printVersions(perjar,System.err);
98       } catch (Exception e)
99       {
100
101       }
102     }
103     else
104     {
105       addVersion(version, observed);
106     }
107   }
108
109   private static void addVersion(String version, Hashtable observed)
110   {
111     if (version != null)
112     {
113 //      System.err.println("Version is '"+version+"'");
114       int[] vrs = (int[]) observed.get(version);
115       if (vrs == null)
116       {
117         vrs = new int[]
118         { 0 };
119       }
120       vrs[0]++;
121       observed.put(version, vrs);
122     }
123   }
124
125   private static String checkClassVersion(String filename)
126           throws IOException
127   {
128     DataInputStream in = new DataInputStream(new FileInputStream(filename));
129     return getVersion(in);
130   }
131
132   private static Hashtable versions = null;
133
134   private static String parseVersions(int minor, int major)
135   {
136     if (versions == null)
137     {
138       versions = new Hashtable();
139       versions.put("45.3", "1.0");
140       versions.put("45.3", "1.1");
141       versions.put("46.0", "1.2");
142       versions.put("47.0", "1.3");
143       versions.put("48.0", "1.4");
144       versions.put("49.0", "1.5");
145       versions.put("50.0", "1.6");
146       versions.put("51.0", "1.7");
147       versions.put("52.0", "1.8");
148
149     }
150     String version = (String) versions.get(major + "."
151             + minor);
152     if (version == null)
153     {
154       // get nearest known version
155       version = (String) versions.get(major + ".0");
156     }
157 //    System.err.println("Version "+version);
158     if (version == null)
159     {
160       versions.put(major + "." + minor, "Class v" + major + ".0");
161     }
162     return version;
163   }
164
165   private static String getVersion(DataInputStream in) throws IOException
166   {
167     int magic = in.readInt();
168     if (magic != 0xcafebabe)
169     {
170       return null;
171     }
172     int minor = in.readUnsignedShort();
173     int major = in.readUnsignedShort();
174 //    System.err.println("Version "+major+"."+minor);
175     return parseVersions(minor, major);
176   }
177
178 }