KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.BufferedReader JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26
27
28 import org.eclipse.debug.core.DebugPlugin;
29 import org.eclipse.debug.core.ILaunch;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31
32 import org.eclipse.jdt.internal.corext.util.Messages;
33
34 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
35 import org.eclipse.jdt.launching.AbstractVMRunner;
36 import org.eclipse.jdt.launching.ExecutionArguments;
37 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
38 import org.eclipse.jdt.launching.IVMInstall;
39 import org.eclipse.jdt.launching.IVMRunner;
40 import org.eclipse.jdt.launching.JavaRuntime;
41 import org.eclipse.jdt.launching.LibraryLocation;
42 import org.eclipse.jdt.launching.VMRunnerConfiguration;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45
46 /**
47  * Launch configuration delegate to launch the computation of the serial version
48  * ID.
49  *
50  * @since 3.1
51  */

52 public final class SerialVersionLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate {
53     
54     /**
55      * An id of value <code>FAILING_ID</code> indicates a failure in calculating
56      * the serial version id
57      */

58     public static final int FAILING_ID= 0;
59
60     /**
61      * VM runner for the serial version ID computation.
62      */

63     final class SerialVersionRunner extends AbstractVMRunner {
64
65         /** The temp file encoding */
66         private static final String JavaDoc TEMP_FILE_ENCODING= "utf-8"; //$NON-NLS-1$
67

68         /** The temp file name */
69         private static final String JavaDoc TEMP_FILE_NAME= "serials.tmp"; //$NON-NLS-1$
70

71         /** The vm install */
72         private final IVMInstall fInstall;
73
74         /**
75          * Creates a new serial version runner.
76          *
77          * @param install
78          * The vm install to base on
79          */

80         SerialVersionRunner(final IVMInstall install) {
81             Assert.isNotNull(install);
82
83             fInstall= install;
84         }
85
86         /**
87          * Flattens the indicated class path to a string.
88          *
89          * @param path
90          * the class path to flatten
91          * @return the flattened class path
92          */

93         private String JavaDoc flattenClassPath(final String JavaDoc[] path) {
94             Assert.isNotNull(path);
95             int count= 0;
96             if (path.length == 0)
97                 return ""; //$NON-NLS-1$
98
final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
99             for (int index= 0; index < path.length; index++) {
100                 if (count > 0)
101                     buffer.append(File.pathSeparator);
102                 buffer.append(path[index]);
103                 count++;
104             }
105             return buffer.toString();
106         }
107
108         /**
109          * Construct and return a String containing the full path of a java
110          * executable command such as 'java' or 'javaw.exe'. If the
111          * configuration specifies an explicit executable, that is used.
112          *
113          * @return full path to java executable
114          * @exception CoreException
115          * if unable to locate an executable
116          */

117         private String JavaDoc getJavaExecutable(final VMRunnerConfiguration configuration) throws CoreException {
118             Assert.isNotNull(configuration);
119             String JavaDoc command= null;
120             final Map JavaDoc map= configuration.getVMSpecificAttributesMap();
121             if (map != null)
122                 command= (String JavaDoc) map.get(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND);
123             if (command == null) {
124                 final File JavaDoc executable= findJavaExecutable(fInstall.getInstallLocation());
125                 if (executable == null)
126                     abort(Messages.format(CorrectionMessages.SerialVersionHashProposal_unable_locate_executable, new String JavaDoc[] { fInstall.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
127                 return executable.getAbsolutePath();
128             }
129             final String JavaDoc location= fInstall.getInstallLocation().getAbsolutePath() + File.separatorChar;
130             File JavaDoc executable= new File JavaDoc(location + "bin" + File.separatorChar + command); //$NON-NLS-1$
131
if (executable.exists() && executable.isFile())
132                 return executable.getAbsolutePath();
133             executable= new File JavaDoc(executable.getAbsolutePath() + ".exe"); //$NON-NLS-1$
134
if (executable.exists() && executable.isFile())
135                 return executable.getAbsolutePath();
136             executable= new File JavaDoc(location + "jre" + File.separatorChar + "bin" + File.separatorChar + command); //$NON-NLS-1$ //$NON-NLS-2$
137
if (executable.exists() && executable.isFile())
138                 return executable.getAbsolutePath();
139             executable= new File JavaDoc(executable.getAbsolutePath() + ".exe"); //$NON-NLS-1$
140
if (executable.exists() && executable.isFile())
141                 return executable.getAbsolutePath();
142             abort(Messages.format(CorrectionMessages.SerialVersionHashProposal_wrong_executable, new String JavaDoc[] { command, fInstall.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
143             return null;
144         }
145
146         /**
147          * {@inheritDoc}
148          */

149         protected String JavaDoc getPluginIdentifier() {
150             return JavaPlugin.getPluginId();
151         }
152
153         /**
154          * {@inheritDoc}
155          */

156         public void run(final VMRunnerConfiguration configuration, final ILaunch launch, final IProgressMonitor monitor) throws CoreException {
157             Assert.isNotNull(configuration);
158             Assert.isNotNull(launch);
159             Assert.isNotNull(monitor);
160             
161             fErrorMessage= null;
162             fSerialVersionID= null;
163             
164             monitor.beginTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_launching_vm, 40);
165             try {
166                 monitor.worked(10);
167                 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_constructing_command_line);
168                 final List JavaDoc arguments= new ArrayList JavaDoc();
169                 arguments.add(getJavaExecutable(configuration));
170                 final String JavaDoc[] vmArguments= combineVmArgs(configuration, fInstall);
171                 for (int index= 0; index < vmArguments.length; index++)
172                     arguments.add(vmArguments[index]);
173                 String JavaDoc[] bootClassPath= configuration.getBootClassPath();
174                 final String JavaDoc[] classPath= configuration.getClassPath();
175                 String JavaDoc[] combinedClassPath= null;
176                 LibraryLocation[] locations= null;
177                 if (bootClassPath == null) {
178                     locations= JavaRuntime.getLibraryLocations(fInstall);
179                     bootClassPath= new String JavaDoc[locations.length];
180                     for (int index= 0; index < locations.length; index++)
181                         bootClassPath[index]= locations[index].getSystemLibraryPath().toOSString();
182                 }
183                 if (monitor.isCanceled())
184                     return;
185                 combinedClassPath= new String JavaDoc[bootClassPath.length + classPath.length];
186                 int offset= 0;
187                 for (int index= 0; index < bootClassPath.length; index++) {
188                     combinedClassPath[offset]= bootClassPath[index];
189                     offset++;
190                 }
191                 for (int index= 0; index < classPath.length; index++) {
192                     combinedClassPath[offset]= classPath[index];
193                     offset++;
194                 }
195                 if (combinedClassPath.length > 0) {
196                     arguments.add("-classpath"); //$NON-NLS-1$
197
arguments.add(flattenClassPath(combinedClassPath));
198                 }
199                 arguments.add(configuration.getClassToLaunch());
200                 final String JavaDoc[] programArguments= configuration.getProgramArguments();
201                 for (int index= 0; index < programArguments.length; index++)
202                     arguments.add(programArguments[index]);
203                 final String JavaDoc[] commandLine= new String JavaDoc[arguments.size()];
204                 arguments.toArray(commandLine);
205                 if (monitor.isCanceled())
206                     return;
207                 monitor.worked(10);
208                 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_starting_vm);
209                 final Process JavaDoc process= exec(commandLine, null);
210                 if (process != null) {
211                     try {
212                         process.waitFor();
213                     } catch (InterruptedException JavaDoc exception) {
214                         // Do nothing
215
}
216                     monitor.worked(10);
217                     final String JavaDoc directory= System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
218
if (directory != null && !"".equals(directory)) { //$NON-NLS-1$
219
final String JavaDoc separator= System.getProperty("file.separator"); //$NON-NLS-1$
220
if (separator != null && !"".equals(separator)) { //$NON-NLS-1$
221
final File JavaDoc file= new File JavaDoc(directory + separator + TEMP_FILE_NAME);
222                             if (file.exists()) {
223                                 monitor.worked(40);
224                                 file.deleteOnExit();
225                                 BufferedReader JavaDoc reader= null;
226                                 final List JavaDoc lines= new ArrayList JavaDoc();
227                                 try {
228                                     reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(file), TEMP_FILE_ENCODING));
229                                     while (reader.ready()) {
230                                         final String JavaDoc line= reader.readLine();
231                                         if (line != null && !"".equals(line)) //$NON-NLS-1$
232
lines.add(line);
233                                     }
234                                 } catch (IOException JavaDoc exception) {
235                                     fErrorMessage= exception.getLocalizedMessage();
236                                 } finally {
237                                     if (reader != null) {
238                                         try {
239                                             reader.close();
240                                         } catch (IOException JavaDoc exception) {
241                                             // Do nothing
242
}
243                                     }
244                                 }
245                                 fSerialVersionID= new long[lines.size()];
246                                 for (int index= 0; index < fSerialVersionID.length; index++) {
247                                     final String JavaDoc line= (String JavaDoc) lines.get(index);
248                                     try {
249                                         fSerialVersionID[index]= Long.parseLong(line);
250                                     } catch (NumberFormatException JavaDoc exception) {
251                                         fSerialVersionID[index]= FAILING_ID;
252                                     }
253                                 }
254                             } else
255                                 fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_temp_file_not_exists;
256                         } else
257                             fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_error_getting_separator_property;
258                     } else
259                         fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_error_getting_temp_dir_property;
260                     if (monitor.isCanceled())
261                         process.destroy();
262                 }
263             } finally {
264                 monitor.done();
265             }
266         }
267     }
268
269     private static final char fgSeparator = File.separatorChar;
270     /**
271      * The list of locations in which to look for the java executable in candidate
272      * VM install locations, relative to the VM install location.
273      */

274     private static final String JavaDoc[] fgCandidateJavaFiles = {"javaw", "javaw.exe", "java", "java.exe", "j9w", "j9w.exe", "j9", "j9.exe"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
275
private static final String JavaDoc[] fgCandidateJavaLocations = {"bin" + fgSeparator, "jre" + fgSeparator + "bin" + fgSeparator}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
276

277     /**
278      * Starting in the specified VM install location, attempt to find the 'java' executable
279      * file. If found, return the corresponding <code>File</code> object, otherwise return
280      * <code>null</code>.
281      */

282     public static File JavaDoc findJavaExecutable(File JavaDoc vmInstallLocation) {
283         // Try each candidate in order. The first one found wins. Thus, the order
284
// of fgCandidateJavaLocations and fgCandidateJavaFiles is significant.
285
for (int i = 0; i < fgCandidateJavaFiles.length; i++) {
286             for (int j = 0; j < fgCandidateJavaLocations.length; j++) {
287                 File JavaDoc javaFile = new File JavaDoc(vmInstallLocation, fgCandidateJavaLocations[j] + fgCandidateJavaFiles[i]);
288                 if (javaFile.isFile()) {
289                     return javaFile;
290                 }
291             }
292         }
293         return null;
294     }
295
296     /** The error message */
297     private String JavaDoc fErrorMessage= null;
298
299     /** The computed serial version ids */
300     private long[] fSerialVersionID= {};
301
302     /**
303      * Returns any error message that occurred during the computation.
304      *
305      * @return the error message, or <code>null</code>
306      */

307     public String JavaDoc getErrorMessage() {
308         return fErrorMessage;
309     }
310
311     /**
312      * Returns the computed serial version IDs.
313      *
314      * @return the computed serial version IDs
315      */

316     public long[] getSerialVersionIDs() {
317         return fSerialVersionID;
318     }
319
320     /**
321      * {@inheritDoc}
322      */

323     public void launch(final ILaunchConfiguration configuration, final String JavaDoc mode, final ILaunch launch, IProgressMonitor monitor) throws CoreException {
324         Assert.isNotNull(configuration);
325         Assert.isNotNull(monitor);
326         try {
327             monitor.beginTask(Messages.format("{0}...", new String JavaDoc[] { configuration.getName()}), 100); //$NON-NLS-1$
328
if (monitor.isCanceled())
329                 return;
330
331             monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_verifying_launch_attributes);
332
333             final String JavaDoc type= verifyMainTypeName(configuration);
334             final IVMInstall install= verifyVMInstall(configuration);
335             final IVMRunner runner= new SerialVersionRunner(install);
336             monitor.worked(10);
337
338             monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_setting_up);
339
340             final String JavaDoc[] environment= DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
341             final String JavaDoc programArguments= getProgramArguments(configuration);
342             final String JavaDoc vmArguments= getVMArguments(configuration);
343             final ExecutionArguments execArguments= new ExecutionArguments(vmArguments, programArguments);
344             final Map JavaDoc attributes= getVMSpecificAttributesMap(configuration);
345             final String JavaDoc[] classpath= getClasspath(configuration);
346
347             monitor.worked(5);
348
349             final VMRunnerConfiguration vmConfiguration= new VMRunnerConfiguration(type, classpath);
350             vmConfiguration.setProgramArguments(execArguments.getProgramArgumentsArray());
351             vmConfiguration.setEnvironment(environment);
352             vmConfiguration.setVMArguments(execArguments.getVMArgumentsArray());
353             vmConfiguration.setVMSpecificAttributesMap(attributes);
354             vmConfiguration.setBootClassPath(getBootpath(configuration));
355
356             if (monitor.isCanceled())
357                 return;
358
359             monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_launching_computation);
360             monitor.worked(5);
361
362             runner.run(vmConfiguration, launch, new SubProgressMonitor(monitor, 80));
363
364             if (monitor.isCanceled())
365                 return;
366
367         } finally {
368             monitor.done();
369         }
370     }
371 }
372
Popular Tags