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