1 22 package org.jboss.tools; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.IOException ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Enumeration ; 31 import java.util.TreeMap ; 32 import java.util.Map ; 33 import java.util.logging.Logger ; 34 import java.util.logging.Level ; 35 import java.util.jar.JarFile ; 36 import java.util.jar.JarEntry ; 37 import java.net.URLClassLoader ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 41 47 public class SerialVersionUID 48 { 49 50 static Logger log = Logger.getLogger("SerialVersionUID"); 51 52 static void buildJarSet(File dir, HashSet jarFiles) 53 throws MalformedURLException 54 { 55 File [] files = dir.listFiles(); 56 for(int n = 0; n < files.length; n ++) 57 { 58 File child = files[n]; 59 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 75 static void generateJarSerialVersionUIDs(URL jar, TreeMap classVersionMap, 76 ClassLoader cl, String pkgPrefix) throws IOException 77 { 78 String jarName = jar.getFile(); 79 JarFile jf = new JarFile (jarName); 80 Enumeration entries = jf.entries(); 81 while( entries.hasMoreElements() ) 82 { 83 JarEntry entry = (JarEntry ) entries.nextElement(); 84 String name = entry.getName(); 85 if( name.endsWith(".class") && name.startsWith(pkgPrefix) ) 86 { 87 name = name.substring(0, name.length() - 6); 88 String 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 e) 112 { 113 log.log(Level.SEVERE, "Check the MaxPermSize", e); 114 } 115 catch(Throwable e) 116 { 117 log.log(Level.FINE, "While loading: "+name, e); 118 } 119 } 120 } 121 jf.close(); 122 } 123 124 131 public static Map generateJBossSerialVersionUIDReport(File jbossHome) 132 throws IOException 133 { 134 HashSet jarFiles = new HashSet (); 136 File lib = new File (jbossHome, "lib"); 137 buildJarSet(lib, jarFiles); 138 File all = new File (jbossHome, "server/all"); 139 buildJarSet(all, jarFiles); 140 URL [] cp = new URL [jarFiles.size()]; 141 jarFiles.toArray(cp); 142 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 143 URLClassLoader completeClasspath = new URLClassLoader (cp, parent); 144 145 TreeMap classVersionMap = new TreeMap (); 146 Iterator jarIter = jarFiles.iterator(); 147 while( jarIter.hasNext() ) 148 { 149 URL jar = (URL ) jarIter.next(); 150 try 151 { 152 generateJarSerialVersionUIDs(jar, classVersionMap, completeClasspath, ""); 153 } 154 catch(IOException e) 155 { 156 log.info("Failed to process jar: "+jar); 157 } 158 } 159 160 return classVersionMap; 161 } 162 163 169 public static Map generateRISerialVersionUIDReport(File j2eeHome) 170 throws IOException 171 { 172 HashSet jarFiles = new HashSet (); 174 File lib = new File (j2eeHome, "lib"); 175 buildJarSet(lib, jarFiles); 176 URL [] cp = new URL [jarFiles.size()]; 177 jarFiles.toArray(cp); 178 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 179 URLClassLoader completeClasspath = new URLClassLoader (cp, parent); 180 181 TreeMap classVersionMap = new TreeMap (); 182 Iterator jarIter = jarFiles.iterator(); 183 while( jarIter.hasNext() ) 184 { 185 URL jar = (URL ) jarIter.next(); 186 try 187 { 188 generateJarSerialVersionUIDs(jar, classVersionMap, completeClasspath, "javax"); 189 } 190 catch(IOException e) 191 { 192 log.info("Failed to process jar: "+jar); 193 } 194 } 195 196 return classVersionMap; 197 } 198 199 205 public static void main(String [] args) throws Exception 206 { 207 if( args.length != 1 ) 208 { 209 System.err.println("Usage: jboss-home"); 210 System.exit(1); 211 } 212 File distHome = new File (args[0]); 213 Map classVersionMap = null; 214 if( distHome.getName().startsWith("jboss") ) 215 classVersionMap = generateJBossSerialVersionUIDReport(distHome); 216 else 217 classVersionMap = generateRISerialVersionUIDReport(distHome); 218 log.info("Total classes with serialVersionUID != 0: "+classVersionMap.size()); 220 FileOutputStream fos = new FileOutputStream ("serialuid.ser"); 221 ObjectOutputStream oos = new ObjectOutputStream (fos); 222 oos.writeObject(classVersionMap); 223 fos.close(); 224 } 225 } 226 | Popular Tags |