de276733d754b10e86a2d1f67faef92f0b1a7062
[jalview.git] / utils / getJavaVersion.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.*;
22 import java.util.*;
23 import java.util.jar.JarEntry;
24 import java.util.jar.JarInputStream;
25
26 public class getJavaVersion
27 {
28 /**
29  * Takes a set of Jars and/or class files as arguments. Reports the java version for the classes
30  */
31
32   public static void main(String[] args) throws IOException
33   {
34     Hashtable observed=new Hashtable();
35     for (int i = 0; i < args.length; i++)
36       {
37         checkClassVersion(args[i], observed);
38       }
39     printVersions(observed, System.out);
40   }
41   public static void printVersions(Hashtable observed, java.io.PrintStream outs)
42   {
43     if (observed.size()>0)
44     {
45       int space=0;
46       String key=null;
47       for (Enumeration keys = observed.keys(); keys.hasMoreElements(); ) {
48         key = (String) keys.nextElement();
49         if (space++>0)
50         {
51           outs.print(" ");
52         }
53         outs.print(key);
54       }
55       outs.print("\n");
56     }
57   }
58
59   private static void checkClassVersion(String filename, Hashtable observed)
60           throws IOException
61   {
62     String version = checkClassVersion(filename);
63     if (version == null)
64     {
65 //      System.err.println("Reading "+filename+" as  jar:");
66       try
67       {
68         JarInputStream jis = new JarInputStream(new FileInputStream(
69                 filename));
70         JarEntry entry;
71         Hashtable perjar=new Hashtable();
72         while ((entry = jis.getNextJarEntry()) != null)
73         {
74           if (entry != null)
75           {
76             if (entry.getName().endsWith(".class"))
77             {
78               try
79               {
80                 version = getVersion(new DataInputStream(jis));
81                 if (version != null)
82                 {
83                   addVersion(version, observed);
84                   addVersion(version, perjar);
85                 }
86               } catch (Exception e)
87               {
88
89               }
90             }
91           }
92         }
93         System.err.println("Jar : "+filename);
94         printVersions(perjar,System.err);
95       } catch (Exception e)
96       {
97
98       }
99     }
100     else
101     {
102       addVersion(version, observed);
103     }
104   }
105
106   private static void addVersion(String version, Hashtable observed)
107   {
108     if (version != null)
109     {
110 //      System.err.println("Version is '"+version+"'");
111       int[] vrs = (int[]) observed.get(version);
112       if (vrs == null)
113       {
114         vrs = new int[]
115         { 0 };
116       }
117       vrs[0]++;
118       observed.put(version, vrs);
119     }
120   }
121
122   private static String checkClassVersion(String filename)
123           throws IOException
124   {
125     DataInputStream in = new DataInputStream(new FileInputStream(filename));
126     return getVersion(in);
127   }
128
129   private static Hashtable versions = null;
130
131   private static String parseVersions(int minor, int major)
132   {
133     if (versions == null)
134     {
135       versions = new Hashtable();
136       versions.put("45.3", "1.0");
137       versions.put("45.3", "1.1");
138       versions.put("46.0", "1.2");
139       versions.put("47.0", "1.3");
140       versions.put("48.0", "1.4");
141       versions.put("49.0", "1.5");
142       versions.put("50.0", "1.6");
143     }
144     String version = (String) versions.get(major + "."
145             + minor);
146     if (version == null)
147     {
148       // get nearest known version
149       version = (String) versions.get(major + ".0");
150     }
151 //    System.err.println("Version "+version);
152     return version;
153   }
154
155   private static String getVersion(DataInputStream in) throws IOException
156   {
157     int magic = in.readInt();
158     if (magic != 0xcafebabe)
159     {
160       return null;
161     }
162     int minor = in.readUnsignedShort();
163     int major = in.readUnsignedShort();
164 //    System.err.println("Version "+major+"."+minor);
165     return parseVersions(minor, major);
166   }
167
168 }