KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tools > SerialVersionUID


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tools;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36 import java.util.jar.JarEntry JavaDoc;
37 import java.net.URLClassLoader JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40
41 /** A tool/service that computes all the class serialVersionUIDs under the
42  * jboss home directory.
43  *
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37459 $
46  */

47 public class SerialVersionUID
48 {
49    /** A jdk logger so that only this + ClassVersionInfo are needed */
50    static Logger JavaDoc log = Logger.getLogger("SerialVersionUID");
51
52    static void buildJarSet(File JavaDoc dir, HashSet JavaDoc jarFiles)
53       throws MalformedURLException JavaDoc
54    {
55       File JavaDoc[] files = dir.listFiles();
56       for(int n = 0; n < files.length; n ++)
57       {
58          File JavaDoc child = files[n];
59          // Ignore the server tmp directory
60
if( child.isDirectory() && child.getName().equals("tmp") == false )
61             buildJarSet(child, jarFiles);
62          else if( child.getName().endsWith(".jar") )
63             jarFiles.add(child.toURL());
64       }
65    }
66
67    /**
68     * Build a TreeMap of the class name to ClassVersionInfo
69     * @param jar
70     * @param classVersionMap TreeMap<String, ClassVersionInfo> for serializable
71     * classes
72     * @param cl - the class loader to use
73     * @throws IOException thrown if the jar cannot be opened
74     */

75    static void generateJarSerialVersionUIDs(URL JavaDoc jar, TreeMap JavaDoc classVersionMap,
76       ClassLoader JavaDoc cl, String JavaDoc pkgPrefix) throws IOException JavaDoc
77    {
78       String JavaDoc jarName = jar.getFile();
79       JarFile JavaDoc jf = new JarFile JavaDoc(jarName);
80       Enumeration JavaDoc entries = jf.entries();
81       while( entries.hasMoreElements() )
82       {
83          JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
84          String JavaDoc name = entry.getName();
85          if( name.endsWith(".class") && name.startsWith(pkgPrefix) )
86          {
87             name = name.substring(0, name.length() - 6);
88             String JavaDoc classname = name.replace('/', '.');
89             try
90             {
91                log.fine("Creating ClassVersionInfo for: "+classname);
92                ClassVersionInfo cvi = new ClassVersionInfo(classname, cl);
93                if( cvi.getSerialVersion() != 0 )
94                {
95                   ClassVersionInfo prevCVI = (ClassVersionInfo)
96                      classVersionMap.put(classname, cvi);
97                   if( prevCVI != null )
98                   {
99                      if( prevCVI.getSerialVersion() != cvi.getSerialVersion() )
100                      {
101                         log.severe("Found inconsistent classes, "
102                            +prevCVI+" != "+cvi+", jar: "+jarName);
103                      }
104                   }
105                   if( cvi.getHasExplicitSerialVersionUID() == false )
106                   {
107                      log.warning("No explicit serialVersionUID: "+cvi);
108                   }
109                }
110             }
111             catch(OutOfMemoryError JavaDoc e)
112             {
113                log.log(Level.SEVERE, "Check the MaxPermSize", e);
114             }
115             catch(Throwable JavaDoc e)
116             {
117                log.log(Level.FINE, "While loading: "+name, e);
118             }
119          }
120       }
121       jf.close();
122    }
123
124    /**
125     * Create a Map<String, ClassVersionInfo> for the jboss dist jars.
126     *
127     * @param jbossHome - the jboss dist root directory
128     * @return Map<String, ClassVersionInfo>
129     * @throws IOException
130     */

131    public static Map JavaDoc generateJBossSerialVersionUIDReport(File JavaDoc jbossHome)
132       throws IOException JavaDoc
133    {
134       // Obtain the jars from the /lib and /server/all locations
135
HashSet JavaDoc jarFiles = new HashSet JavaDoc();
136       File JavaDoc lib = new File JavaDoc(jbossHome, "lib");
137       buildJarSet(lib, jarFiles);
138       File JavaDoc all = new File JavaDoc(jbossHome, "server/all");
139       buildJarSet(all, jarFiles);
140       URL JavaDoc[] cp = new URL JavaDoc[jarFiles.size()];
141       jarFiles.toArray(cp);
142       ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
143       URLClassLoader JavaDoc completeClasspath = new URLClassLoader JavaDoc(cp, parent);
144
145       TreeMap JavaDoc classVersionMap = new TreeMap JavaDoc();
146       Iterator JavaDoc jarIter = jarFiles.iterator();
147       while( jarIter.hasNext() )
148       {
149          URL JavaDoc jar = (URL JavaDoc) jarIter.next();
150          try
151          {
152             generateJarSerialVersionUIDs(jar, classVersionMap, completeClasspath, "");
153          }
154          catch(IOException JavaDoc e)
155          {
156             log.info("Failed to process jar: "+jar);
157          }
158       }
159
160       return classVersionMap;
161    }
162
163    /**
164     * Create a Map<String, ClassVersionInfo> for the jboss dist jars.
165     * @param j2eeHome - the j2ee ri dist root directory
166     * @return Map<String, ClassVersionInfo>
167     * @throws IOException
168     */

169    public static Map JavaDoc generateRISerialVersionUIDReport(File JavaDoc j2eeHome)
170       throws IOException JavaDoc
171    {
172       // Obtain the jars from the /lib
173
HashSet JavaDoc jarFiles = new HashSet JavaDoc();
174       File JavaDoc lib = new File JavaDoc(j2eeHome, "lib");
175       buildJarSet(lib, jarFiles);
176       URL JavaDoc[] cp = new URL JavaDoc[jarFiles.size()];
177       jarFiles.toArray(cp);
178       ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
179       URLClassLoader JavaDoc completeClasspath = new URLClassLoader JavaDoc(cp, parent);
180
181       TreeMap JavaDoc classVersionMap = new TreeMap JavaDoc();
182       Iterator JavaDoc jarIter = jarFiles.iterator();
183       while( jarIter.hasNext() )
184       {
185          URL JavaDoc jar = (URL JavaDoc) jarIter.next();
186          try
187          {
188             generateJarSerialVersionUIDs(jar, classVersionMap, completeClasspath, "javax");
189          }
190          catch(IOException JavaDoc e)
191          {
192             log.info("Failed to process jar: "+jar);
193          }
194       }
195
196       return classVersionMap;
197    }
198
199    /**
200     * Generate a mapping of the serial version UIDs for the serializable classes
201     * under the jboss dist directory
202     * @param args - [0] = jboss dist root directory
203     * @throws Exception
204     */

205    public static void main(String JavaDoc[] args) throws Exception JavaDoc
206    {
207       if( args.length != 1 )
208       {
209          System.err.println("Usage: jboss-home");
210          System.exit(1);
211       }
212       File JavaDoc distHome = new File JavaDoc(args[0]);
213       Map JavaDoc classVersionMap = null;
214       if( distHome.getName().startsWith("jboss") )
215          classVersionMap = generateJBossSerialVersionUIDReport(distHome);
216       else
217          classVersionMap = generateRISerialVersionUIDReport(distHome);
218       // Write the map out the object file
219
log.info("Total classes with serialVersionUID != 0: "+classVersionMap.size());
220       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("serialuid.ser");
221       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
222       oos.writeObject(classVersionMap);
223       fos.close();
224    }
225 }
226
Popular Tags