1 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.FileOutputStream ; 26 import java.io.FileInputStream ; 27 import java.io.InputStream ; 28 import java.io.FileWriter ; 29 import java.math.BigInteger ; 30 import java.security.MessageDigest ; 31 import java.security.NoSuchAlgorithmException ; 32 import java.util.jar.Attributes ; 33 import java.util.jar.JarFile ; 34 import java.util.jar.Manifest ; 35 import java.util.jar.JarEntry ; 36 import java.util.jar.JarOutputStream ; 37 import java.util.Enumeration ; 38 import java.util.Iterator ; 39 import java.util.TreeSet ; 40 41 import org.dom4j.Document; 42 import org.dom4j.DocumentFactory; 43 import org.dom4j.Element; 44 import org.dom4j.io.OutputFormat; 45 import org.dom4j.io.XMLWriter; 46 47 55 public class VersionRelease 56 { 57 static byte[] buffer = new byte[4096]; 58 59 60 File jbossHome; 61 String specVersion; 62 String specVendor; 63 String specTitle; 64 String implTitle; 65 String implURL; 66 String implVersion; 67 String implVendor; 68 String implVendorID; 69 MessageDigest md5; 70 TreeSet jars = new TreeSet (); 71 72 public VersionRelease(String homeDir) 73 throws FileNotFoundException , NoSuchAlgorithmException 74 { 75 jbossHome = new File (homeDir); 76 if( jbossHome.exists() == false ) 77 throw new FileNotFoundException (jbossHome.getAbsolutePath() + " does not exist"); 78 specTitle = System.getProperty("specification.title"); 79 specVersion = System.getProperty("specification.version"); 80 specVendor = System.getProperty("specification.vendor"); 81 implTitle = System.getProperty("implementation.title"); 82 implURL = System.getProperty("implementation.url"); 83 implVersion = System.getProperty("implementation.version"); 84 implVendor = System.getProperty("implementation.vendor"); 85 implVendorID = System.getProperty("implementation.vendor.id"); 86 md5 = MessageDigest.getInstance("MD5"); 87 } 88 89 public void run() 90 { 91 processDir(jbossHome); 92 try 93 { 94 DocumentFactory df = DocumentFactory.getInstance(); 95 Document doc = df.createDocument(); 96 Element root = doc.addElement("jar-versions"); 97 Iterator iter = jars.iterator(); 98 while( iter.hasNext() ) 99 { 100 JarInfo info = (JarInfo) iter.next(); 101 info.writeXML(root); 102 } 103 104 File versionsXml = new File (jbossHome, "jar-versions.xml"); 105 FileWriter versionInfo = new FileWriter (versionsXml); 106 OutputFormat outformat = OutputFormat.createPrettyPrint(); 107 XMLWriter writer = new XMLWriter(versionInfo, outformat); 108 writer.setEscapeText(true); 109 writer.write(doc); 110 writer.flush(); 111 versionInfo.close(); 112 } 113 catch(IOException e) 114 { 115 e.printStackTrace(); 116 } 117 } 118 119 void processDir(File dir) 120 { 121 File [] files = dir.listFiles(); 122 for(int f = 0; f < files.length; f ++) 123 { 124 File child = files[f]; 125 if( child.isDirectory() == true ) 126 processDir(child); 127 else 128 processFile(child); 129 } 130 } 131 void processFile(File file) 132 { 133 System.out.println("Checking file: "+file); 134 try 136 { 137 JarInfo info = new JarInfo(file, this); 138 info.write(md5); 139 jars.add(info); 140 } 141 catch(FileNotFoundException e) 142 { 143 } 144 catch(Exception e) 145 { 146 e.printStackTrace(); 147 } 148 } 149 150 static class JarInfo implements Comparable 151 { 152 File file; 153 File tmpFile; 154 155 Manifest mf; 156 JarFile jarFile; 157 String jarName; 158 boolean sealed; 159 String md5Digest; 160 String specVersion; 161 String specVendor; 162 String specTitle; 163 String implTitle; 164 String implURL; 165 String implVersion; 166 String implVendor; 167 String implVendorID; 168 169 JarInfo(File file, VersionRelease release) 170 throws IOException 171 { 172 this.file = file; 173 this.jarName = file.getName(); 174 this.tmpFile = new File (file.getAbsolutePath()+".tmp"); 175 if( file.renameTo(tmpFile) == false ) 176 throw new IOException ("Failed to rename: "+file); 177 178 try 179 { 180 this.jarFile = new JarFile (tmpFile); 181 } 182 catch(IOException e) 183 { 184 tmpFile.renameTo(file); 185 throw new FileNotFoundException ("Not a JarFile: "+file); 186 } 187 188 this.mf = jarFile.getManifest(); 189 Attributes mfAttrs = mf.getMainAttributes(); 190 191 String sealedAttr = mfAttrs.getValue(Attributes.Name.SEALED); 192 sealed = Boolean.valueOf(sealedAttr).booleanValue(); 193 194 specVersion = mfAttrs.getValue(Attributes.Name.SPECIFICATION_VERSION); 195 if( specVersion == null ) 196 { 197 specVersion = release.specVersion; 198 mfAttrs.put(Attributes.Name.SPECIFICATION_VERSION, specVersion); 199 } 200 specVendor = mfAttrs.getValue(Attributes.Name.SPECIFICATION_VENDOR); 201 if( specVendor == null ) 202 { 203 specVendor = release.specVendor; 204 mfAttrs.put(Attributes.Name.SPECIFICATION_VENDOR, specVendor); 205 } 206 specTitle = mfAttrs.getValue(Attributes.Name.SPECIFICATION_TITLE); 207 if( specTitle == null ) 208 { 209 specTitle = release.specTitle; 210 mfAttrs.put(Attributes.Name.SPECIFICATION_TITLE, specTitle); 211 } 212 213 implTitle = mfAttrs.getValue(Attributes.Name.IMPLEMENTATION_TITLE); 214 if( implTitle == null ) 215 { 216 implTitle = release.implTitle; 217 mfAttrs.put(Attributes.Name.IMPLEMENTATION_TITLE, implTitle); 218 } 219 implVersion = mfAttrs.getValue(Attributes.Name.IMPLEMENTATION_VERSION); 220 if( implVersion == null ) 221 { 222 implVersion = release.implVersion; 223 mfAttrs.put(Attributes.Name.IMPLEMENTATION_VERSION, implVersion); 224 } 225 implVendor = mfAttrs.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); 226 if( implVendor == null ) 227 { 228 implVendor = release.implVendor; 229 mfAttrs.put(Attributes.Name.IMPLEMENTATION_VENDOR, implVendor); 230 } 231 implVendorID = mfAttrs.getValue(Attributes.Name.IMPLEMENTATION_VENDOR_ID); 232 if( implVendorID == null ) 233 { 234 implVendorID = release.implVendorID; 235 mfAttrs.put(Attributes.Name.IMPLEMENTATION_VENDOR_ID, implVendorID); 236 } 237 implURL = mfAttrs.getValue(Attributes.Name.IMPLEMENTATION_URL); 238 if( implURL == null ) 239 { 240 implURL = release.implURL; 241 mfAttrs.put(Attributes.Name.IMPLEMENTATION_URL, implURL); 242 } 243 } 244 245 public void write(MessageDigest md5) 246 throws IOException 247 { 248 md5.reset(); 249 if( sealed == true ) 250 { 251 System.out.println("Skipping sealed jar: "+file); 252 } 253 else 254 { 255 FileOutputStream fos = new FileOutputStream (file); 256 JarOutputStream jos = new JarOutputStream (fos, mf); 257 Enumeration entries = jarFile.entries(); 258 while( entries.hasMoreElements() ) 259 { 260 JarEntry entry = (JarEntry ) entries.nextElement(); 261 String name = entry.getName(); 262 if( name.equals("META-INF/MANIFEST.MF") ) 263 { 264 continue; 265 } 266 267 JarEntry outEntry = new JarEntry (entry.getName()); 268 outEntry.setTime(entry.getTime()); 269 if( entry.getComment() != null ) 270 outEntry.setComment(entry.getComment()); 271 jos.putNextEntry(outEntry); 272 InputStream is = jarFile.getInputStream(entry); 273 int bytes = is.read(buffer); 274 while( bytes > 0 ) 275 { 276 jos.write(buffer, 0, bytes); 277 bytes = is.read(buffer); 278 } 279 jos.closeEntry(); 280 } 281 jarFile.close(); 282 jos.close(); 283 tmpFile.delete(); 284 } 285 286 FileInputStream fis = new FileInputStream (file); 288 int bytes = fis.read(buffer); 289 while( bytes > 0 ) 290 { 291 md5.update(buffer, 0, bytes); 292 bytes = fis.read(buffer); 293 } 294 fis.close(); 295 byte[] digest = md5.digest(); 296 BigInteger bi = new BigInteger (-1, digest); 297 bi = bi.abs(); 298 md5Digest = bi.toString(16); 299 System.out.println(file+", md5: "+md5Digest); 300 } 301 302 public int compareTo(Object o) 303 { 304 JarInfo info = (JarInfo) o; 305 return jarName.compareTo(info.jarName); 306 } 307 public boolean equals(Object o) 308 { 309 JarInfo info = (JarInfo) o; 310 return jarName.equals(info.jarName); 311 } 312 public int hashCode() 313 { 314 return jarName.hashCode(); 315 } 316 328 public String toString() 329 { 330 StringBuffer tmp = new StringBuffer ("<jar name='"); 331 tmp.append(jarName); 332 tmp.append("' specVersion='"); 333 tmp.append(specVersion); 334 tmp.append("' specVendor='"); 335 tmp.append(specVendor); 336 tmp.append("' specTitle='"); 337 tmp.append(specTitle); 338 tmp.append("' implVersion='"); 339 tmp.append(implVersion); 340 tmp.append("' implVendor='"); 341 tmp.append(implVendor); 342 tmp.append("' implTitle='"); 343 tmp.append(implTitle); 344 tmp.append("' implVendorID='"); 345 tmp.append(implVendorID); 346 tmp.append("' implURL='"); 347 tmp.append(implURL); 348 tmp.append("' sealed='"); 349 tmp.append(sealed); 350 tmp.append("' md5Digest='"); 351 tmp.append(md5Digest); 352 tmp.append("' />"); 353 return tmp.toString(); 354 } 355 public void writeXML(Element root) 356 { 357 Element jar = root.addElement("jar"); 358 jar.addAttribute("name", jarName); 359 jar.addAttribute("specVersion", specVersion); 360 jar.addAttribute("specVendor", specVendor); 361 jar.addAttribute("specTitle", specTitle); 362 jar.addAttribute("implVersion", implVersion); 363 jar.addAttribute("implVendor", implVendor); 364 jar.addAttribute("implTitle", implTitle); 365 jar.addAttribute("implVendorID", implVendorID); 366 jar.addAttribute("implURL", implURL); 367 jar.addAttribute("sealed", ""+sealed); 368 jar.addAttribute("md5Digest", md5Digest); 369 } 370 } 371 372 public static void main(String [] args) 373 throws Exception 374 { 375 VersionRelease vr = new VersionRelease(args[0]); 376 vr.run(); 377 } 378 } 379 | Popular Tags |