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