KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VersionRelease


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 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.math.BigInteger JavaDoc;
30 import java.security.MessageDigest JavaDoc;
31 import java.security.NoSuchAlgorithmException JavaDoc;
32 import java.util.jar.Attributes JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35 import java.util.jar.JarEntry JavaDoc;
36 import java.util.jar.JarOutputStream JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.TreeSet JavaDoc;
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 /** A utility which goes through a standard dist build and tags every jar
48  * with the current build version using the jar file version manifest
49  * headers. The unique jars and their version info and md5 digests are
50  * output to the jboss.home/jar-versions.xml.
51  *
52  * @author Scott.Stark@jboss.org
53  * @version $Revision: 39575 $
54  */

55 public class VersionRelease
56 {
57    static byte[] buffer = new byte[4096];
58
59    /** The jboss dist root directory */
60    File JavaDoc jbossHome;
61    String JavaDoc specVersion;
62    String JavaDoc specVendor;
63    String JavaDoc specTitle;
64    String JavaDoc implTitle;
65    String JavaDoc implURL;
66    String JavaDoc implVersion;
67    String JavaDoc implVendor;
68    String JavaDoc implVendorID;
69    MessageDigest JavaDoc md5;
70    TreeSet JavaDoc jars = new TreeSet JavaDoc();
71
72    public VersionRelease(String JavaDoc homeDir)
73       throws FileNotFoundException JavaDoc, NoSuchAlgorithmException JavaDoc
74    {
75       jbossHome = new File JavaDoc(homeDir);
76       if( jbossHome.exists() == false )
77          throw new FileNotFoundException JavaDoc(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 JavaDoc iter = jars.iterator();
98          while( iter.hasNext() )
99          {
100             JarInfo info = (JarInfo) iter.next();
101             info.writeXML(root);
102          }
103
104          File JavaDoc versionsXml = new File JavaDoc(jbossHome, "jar-versions.xml");
105          FileWriter JavaDoc versionInfo = new FileWriter JavaDoc(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 JavaDoc e)
114       {
115          e.printStackTrace();
116       }
117    }
118
119    void processDir(File JavaDoc dir)
120    {
121       File JavaDoc[] files = dir.listFiles();
122       for(int f = 0; f < files.length; f ++)
123       {
124          File JavaDoc child = files[f];
125          if( child.isDirectory() == true )
126             processDir(child);
127          else
128             processFile(child);
129       }
130    }
131    void processFile(File JavaDoc file)
132    {
133       System.out.println("Checking file: "+file);
134       // See if this is a jar archive
135
try
136       {
137          JarInfo info = new JarInfo(file, this);
138          info.write(md5);
139          jars.add(info);
140       }
141       catch(FileNotFoundException JavaDoc e)
142       {
143       }
144       catch(Exception JavaDoc e)
145       {
146          e.printStackTrace();
147       }
148    }
149
150    static class JarInfo implements Comparable JavaDoc
151    {
152       File JavaDoc file;
153       File JavaDoc tmpFile;
154
155       Manifest JavaDoc mf;
156       JarFile JavaDoc jarFile;
157       String JavaDoc jarName;
158       boolean sealed;
159       String JavaDoc md5Digest;
160       String JavaDoc specVersion;
161       String JavaDoc specVendor;
162       String JavaDoc specTitle;
163       String JavaDoc implTitle;
164       String JavaDoc implURL;
165       String JavaDoc implVersion;
166       String JavaDoc implVendor;
167       String JavaDoc implVendorID;
168
169       JarInfo(File JavaDoc file, VersionRelease release)
170          throws IOException JavaDoc
171       {
172          this.file = file;
173          this.jarName = file.getName();
174          this.tmpFile = new File JavaDoc(file.getAbsolutePath()+".tmp");
175          if( file.renameTo(tmpFile) == false )
176             throw new IOException JavaDoc("Failed to rename: "+file);
177
178          try
179          {
180             this.jarFile = new JarFile JavaDoc(tmpFile);
181          }
182          catch(IOException JavaDoc e)
183          {
184             tmpFile.renameTo(file);
185             throw new FileNotFoundException JavaDoc("Not a JarFile: "+file);
186          }
187
188          this.mf = jarFile.getManifest();
189          Attributes JavaDoc mfAttrs = mf.getMainAttributes();
190
191          String JavaDoc 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 JavaDoc md5)
246          throws IOException JavaDoc
247       {
248          md5.reset();
249          if( sealed == true )
250          {
251             System.out.println("Skipping sealed jar: "+file);
252          }
253          else
254          {
255             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
256             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(fos, mf);
257             Enumeration JavaDoc entries = jarFile.entries();
258             while( entries.hasMoreElements() )
259             {
260                JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
261                String JavaDoc name = entry.getName();
262                if( name.equals("META-INF/MANIFEST.MF") )
263                {
264                   continue;
265                }
266
267                JarEntry JavaDoc outEntry = new JarEntry JavaDoc(entry.getName());
268                outEntry.setTime(entry.getTime());
269                if( entry.getComment() != null )
270                   outEntry.setComment(entry.getComment());
271                jos.putNextEntry(outEntry);
272                InputStream JavaDoc 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          // Calculate the md5sum
287
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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 JavaDoc bi = new BigInteger JavaDoc(-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 JavaDoc o)
303       {
304          JarInfo info = (JarInfo) o;
305          return jarName.compareTo(info.jarName);
306       }
307       public boolean equals(Object JavaDoc 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       /* Output an xml string element like:
317       <jar name='twiddle.jar' specVersion='3.2.4'
318             specVendor='JBoss (http://www.jboss.org/)'
319             specTitle='JBoss'
320             implVersion='3.2.4RC2 (build: CVSTag=Branch_3_2 date=200404182118)'
321             implVendor='JBoss.org'
322             implTitle='JBoss [WonderLand]'
323             implVendorID='http://www.jboss.org/'
324             implURL='http://www.jboss.org/'
325             sealed='false'
326             md5Digest='ebf8681b4e600cbe7bb2eff68c537c79' />
327       */

328       public String JavaDoc toString()
329       {
330          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("<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 JavaDoc[] args)
373       throws Exception JavaDoc
374    {
375       VersionRelease vr = new VersionRelease(args[0]);
376       vr.run();
377    }
378 }
379
Popular Tags