KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > SerialVersionComputer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import java.io.BufferedWriter JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.ObjectStreamClass JavaDoc;
18 import java.io.OutputStreamWriter JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.text.DateFormat JavaDoc; // do not convert to ICU, class runs in separate VM
22
import java.text.MessageFormat JavaDoc; // do not convert to ICU, class runs in separate VM
23
import java.util.Date JavaDoc;
24
25 /**
26  * Support class to compute the serial version ID in a separate VM.
27  * <p>
28  * To use this class for the computation of a serial version ID, the following
29  * steps have to be performed:
30  * <ul>
31  * <li>Create a new VM configuration corresponding to the one that created the
32  * class whose serial version ID has to be computed</li>
33  * <li>Set up the class path for the new VM</li>
34  * <li>Set up the command line. The only arguments to pass are the fully
35  * qualified names of the classes to compute the IDs for.</li>
36  * <li>Launch the configured VM</li>
37  * <li>Read the results from the serial IDs temp file</li>
38  * </ul>
39  *
40  * @since 3.1
41  */

42 public final class SerialVersionComputer {
43
44     private static final String JavaDoc NON_RESOLVABLE_CLASS= "The class {0} could not be resolved."; //$NON-NLS-1$
45
private static final String JavaDoc NON_SERIALIZABLE_CLASS= "The class {0} does not implement ''java.io.Serializable'' or ''java.io.Externalizable'' or has already an id"; //$NON-NLS-1$
46

47     /**
48      * Should the process be debugged? (adapt the path of the log file
49      * accordingly)
50      */

51     private static final boolean DEBUG= false;
52
53     /** The temp file encoding */
54     private static final String JavaDoc TEMP_FILE_ENCODING= "utf-8"; //$NON-NLS-1$
55

56     /** The temp file name */
57     private static final String JavaDoc TEMP_FILE_NAME= "serials.tmp"; //$NON-NLS-1$
58

59     /**
60      * The entry point of this process.
61      *
62      * @param arguments
63      * The arguments to pass
64      */

65     public static void main(final String JavaDoc[] arguments) {
66         BufferedWriter JavaDoc logger= null;
67         if (DEBUG) {
68             try {
69                 logger= new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc("C:\\serial.log"))); //$NON-NLS-1$
70
final Date JavaDoc date= new Date JavaDoc(System.currentTimeMillis());
71                 logger.write("Begin Session: " + DateFormat.getDateInstance().format(date) + " at " + DateFormat.getTimeInstance().format(date) + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
72
logger.write("Argument Count: " + arguments.length + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
73
} catch (IOException JavaDoc exception) {
74                 // Do nothing
75
}
76         }
77         if (arguments.length > 0) {
78             final String JavaDoc directory= System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
79
if (directory != null && !"".equals(directory)) { //$NON-NLS-1$
80
final String JavaDoc separator= System.getProperty("file.separator"); //$NON-NLS-1$
81
if (separator != null && !"".equals(separator)) { //$NON-NLS-1$
82
final File JavaDoc file= new File JavaDoc(directory + separator + TEMP_FILE_NAME);
83                     if (DEBUG) {
84                         try {
85                             logger.write("Created file: " + file.getCanonicalPath() + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
86
} catch (IOException JavaDoc exception) {
87                             // Do nothing
88
}
89                     }
90                     Writer JavaDoc writer= null;
91                     try {
92                         file.delete();
93                         file.createNewFile();
94                         writer= new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), TEMP_FILE_ENCODING));
95                         for (int i= 0; i < arguments.length; i++) {
96                             try {
97                                 final ObjectStreamClass JavaDoc clazz= ObjectStreamClass.lookup(Class.forName(arguments[i]));
98                                 if (clazz != null) {
99                                     writer.write(new Long JavaDoc(clazz.getSerialVersionUID()).toString());
100                                     writer.write('\n');
101                                 } else {
102                                     writer.write(format(NON_SERIALIZABLE_CLASS, arguments[i]));
103                                     writer.write('\n');
104                                 }
105                             } catch (ClassNotFoundException JavaDoc exception) {
106                                 writer.write(format(NON_RESOLVABLE_CLASS, arguments[i]));
107                                 writer.write('\n');
108                             }
109                         }
110                     } catch (Throwable JavaDoc throwable) {
111                         if (DEBUG) {
112                             PrintWriter JavaDoc printer= null;
113                             try {
114                                 logger.write("Exception occurred: " + throwable.getLocalizedMessage() + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
115
printer= new PrintWriter JavaDoc(logger);
116                                 throwable.printStackTrace(printer);
117                             } catch (IOException JavaDoc exc) {
118                                 // Do nothing
119
} finally {
120                                 if (printer != null) {
121                                     printer.close();
122                                 }
123                             }
124                         }
125                     } finally {
126                         if (writer != null) {
127                             try {
128                                 writer.close();
129                             } catch (IOException JavaDoc exception) {
130                                 // Do nothing
131
}
132                         }
133                     }
134                 }
135             }
136         }
137         if (DEBUG) {
138             try {
139                 logger.write("End Session\r\n"); //$NON-NLS-1$
140
logger.close();
141             } catch (IOException JavaDoc exception) {
142                 // Do nothing
143
}
144         }
145     }
146     
147     private static String JavaDoc format(String JavaDoc message, Object JavaDoc object) {
148         return MessageFormat.format(message, new Object JavaDoc[] { object});
149     }
150 }
151
Popular Tags