KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > AbstractVMInstall


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.launching;
12
13
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Preferences;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.debug.core.ILaunchManager;
32 import org.eclipse.debug.core.Launch;
33 import org.eclipse.debug.core.model.IProcess;
34 import org.eclipse.debug.core.model.IStreamsProxy;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.internal.launching.LaunchingMessages;
37 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 /**
44  * Abstract implementation of a VM install.
45  * <p>
46  * Clients implementing VM installs must subclass this class.
47  * </p>
48  */

49 public abstract class AbstractVMInstall implements IVMInstall, IVMInstall2, IVMInstall3 {
50
51     private IVMInstallType fType;
52     private String JavaDoc fId;
53     private String JavaDoc fName;
54     private File JavaDoc fInstallLocation;
55     private LibraryLocation[] fSystemLibraryDescriptions;
56     private URL JavaDoc fJavadocLocation;
57     private String JavaDoc fVMArgs;
58     // system properties are cached in user preferences prefixed with this key, followed
59
// by vm type, vm id, and system property name
60
private static final String JavaDoc PREF_VM_INSTALL_SYSTEM_PROPERTY = "PREF_VM_INSTALL_SYSTEM_PROPERTY"; //$NON-NLS-1$
61
// whether change events should be fired
62
private boolean fNotify = true;
63     
64     /**
65      * Constructs a new VM install.
66      *
67      * @param type The type of this VM install.
68      * Must not be <code>null</code>
69      * @param id The unique identifier of this VM instance
70      * Must not be <code>null</code>.
71      * @throws IllegalArgumentException if any of the required
72      * parameters are <code>null</code>.
73      */

74     public AbstractVMInstall(IVMInstallType type, String JavaDoc id) {
75         if (type == null)
76             throw new IllegalArgumentException JavaDoc(LaunchingMessages.vmInstall_assert_typeNotNull);
77         if (id == null)
78             throw new IllegalArgumentException JavaDoc(LaunchingMessages.vmInstall_assert_idNotNull);
79         fType= type;
80         fId= id;
81     }
82
83     /* (non-Javadoc)
84      * Subclasses should not override this method.
85      * @see IVMInstall#getId()
86      */

87     public String JavaDoc getId() {
88         return fId;
89     }
90
91     /* (non-Javadoc)
92      * Subclasses should not override this method.
93      * @see IVMInstall#getName()
94      */

95     public String JavaDoc getName() {
96         return fName;
97     }
98
99     /* (non-Javadoc)
100      * Subclasses should not override this method.
101      * @see IVMInstall#setName(String)
102      */

103     public void setName(String JavaDoc name) {
104         if (!name.equals(fName)) {
105             PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_NAME, fName, name);
106             fName= name;
107             if (fNotify) {
108                 JavaRuntime.fireVMChanged(event);
109             }
110         }
111     }
112
113     /* (non-Javadoc)
114      * Subclasses should not override this method.
115      * @see IVMInstall#getInstallLocation()
116      */

117     public File JavaDoc getInstallLocation() {
118         return fInstallLocation;
119     }
120
121     /* (non-Javadoc)
122      * Subclasses should not override this method.
123      * @see IVMInstall#setInstallLocation(File)
124      */

125     public void setInstallLocation(File JavaDoc installLocation) {
126         if (!installLocation.equals(fInstallLocation)) {
127             PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_INSTALL_LOCATION, fInstallLocation, installLocation);
128             fInstallLocation= installLocation;
129             if (fNotify) {
130                 JavaRuntime.fireVMChanged(event);
131             }
132         }
133     }
134
135     /* (non-Javadoc)
136      * Subclasses should not override this method.
137      * @see IVMInstall#getVMInstallType()
138      */

139     public IVMInstallType getVMInstallType() {
140         return fType;
141     }
142
143     /* (non-Javadoc)
144      * @see IVMInstall#getVMRunner(String)
145      */

146     public IVMRunner getVMRunner(String JavaDoc mode) {
147         return null;
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.jdt.launching.IVMInstall#getLibraryLocations()
152      */

153     public LibraryLocation[] getLibraryLocations() {
154         return fSystemLibraryDescriptions;
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.jdt.launching.IVMInstall#setLibraryLocations(org.eclipse.jdt.launching.LibraryLocation[])
159      */

160     public void setLibraryLocations(LibraryLocation[] locations) {
161         if (locations == fSystemLibraryDescriptions) {
162             return;
163         }
164         LibraryLocation[] newLocations = locations;
165         if (newLocations == null) {
166             newLocations = getVMInstallType().getDefaultLibraryLocations(getInstallLocation());
167         }
168         LibraryLocation[] prevLocations = fSystemLibraryDescriptions;
169         if (prevLocations == null) {
170             prevLocations = getVMInstallType().getDefaultLibraryLocations(getInstallLocation());
171         }
172         
173         if (newLocations.length == prevLocations.length) {
174             int i = 0;
175             boolean equal = true;
176             while (i < newLocations.length && equal) {
177                 equal = newLocations[i].equals(prevLocations[i]);
178                 i++;
179             }
180             if (equal) {
181                 // no change
182
return;
183             }
184         }
185
186         PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_LIBRARY_LOCATIONS, prevLocations, newLocations);
187         fSystemLibraryDescriptions = locations;
188         if (fNotify) {
189             JavaRuntime.fireVMChanged(event);
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.jdt.launching.IVMInstall#getJavadocLocation()
195      */

196     public URL JavaDoc getJavadocLocation() {
197         return fJavadocLocation;
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.jdt.launching.IVMInstall#setJavadocLocation(java.net.URL)
202      */

203     public void setJavadocLocation(URL JavaDoc url) {
204         if (url == fJavadocLocation) {
205             return;
206         }
207         if (url != null && fJavadocLocation != null) {
208             if (url.equals(fJavadocLocation)) {
209                 // no change
210
return;
211             }
212         }
213         
214         PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_JAVADOC_LOCATION, fJavadocLocation, url);
215         fJavadocLocation = url;
216         if (fNotify) {
217             JavaRuntime.fireVMChanged(event);
218         }
219     }
220
221     /**
222      * Whether this VM should fire property change notifications.
223      *
224      * @param notify
225      * @since 2.1
226      */

227     protected void setNotify(boolean notify) {
228         fNotify = notify;
229     }
230
231     /* (non-Javadoc)
232      * @see java.lang.Object#equals(java.lang.Object)
233      * @since 2.1
234      */

235     public boolean equals(Object JavaDoc object) {
236         if (object instanceof IVMInstall) {
237             IVMInstall vm = (IVMInstall)object;
238             return getVMInstallType().equals(vm.getVMInstallType()) &&
239                 getId().equals(vm.getId());
240         }
241         return false;
242     }
243
244     /* (non-Javadoc)
245      * @see java.lang.Object#hashCode()
246      * @since 2.1
247      */

248     public int hashCode() {
249         return getVMInstallType().hashCode() + getId().hashCode();
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.jdt.launching.IVMInstall#getDefaultVMArguments()
254      * @since 3.0
255      */

256     public String JavaDoc[] getVMArguments() {
257         String JavaDoc args = getVMArgs();
258         if (args == null) {
259             return null;
260         }
261         ExecutionArguments ex = new ExecutionArguments(args, ""); //$NON-NLS-1$
262
return ex.getVMArgumentsArray();
263     }
264     
265     /* (non-Javadoc)
266      * @see org.eclipse.jdt.launching.IVMInstall#setDefaultVMArguments(java.lang.String[])
267      * @since 3.0
268      */

269     public void setVMArguments(String JavaDoc[] vmArgs) {
270         if (vmArgs == null) {
271             setVMArgs(null);
272         } else {
273             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
274             for (int i = 0; i < vmArgs.length; i++) {
275                 String JavaDoc string = vmArgs[i];
276                 buf.append(string);
277                 buf.append(" "); //$NON-NLS-1$
278
}
279             setVMArgs(buf.toString().trim());
280         }
281     }
282     
283     /* (non-Javadoc)
284      * @see org.eclipse.jdt.launching.IVMInstall2#getVMArgs()
285      */

286     public String JavaDoc getVMArgs() {
287         return fVMArgs;
288     }
289     
290     /* (non-Javadoc)
291      * @see org.eclipse.jdt.launching.IVMInstall2#setVMArgs(java.lang.String)
292      */

293     public void setVMArgs(String JavaDoc vmArgs) {
294         if (fVMArgs == null) {
295             if (vmArgs == null) {
296                 // No change
297
return;
298             }
299         } else if (fVMArgs.equals(vmArgs)) {
300             // No change
301
return;
302         }
303         PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_VM_ARGUMENTS, fVMArgs, vmArgs);
304         fVMArgs = vmArgs;
305         if (fNotify) {
306             JavaRuntime.fireVMChanged(event);
307         }
308     }
309     
310     /* (non-Javadoc)
311      * Subclasses should override.
312      * @see org.eclipse.jdt.launching.IVMInstall2#getJavaVersion()
313      */

314     public String JavaDoc getJavaVersion() {
315         return null;
316     }
317     
318     /* (non-Javadoc)
319      * @see org.eclipse.jdt.launching.IVMInstall3#evaluateSystemProperties(java.lang.String[], org.eclipse.core.runtime.IProgressMonitor)
320      */

321     public Map JavaDoc evaluateSystemProperties(String JavaDoc[] properties, IProgressMonitor monitor) throws CoreException {
322         //locate the launching support jar - it contains the main program to run
323
if (monitor == null) {
324             monitor = new NullProgressMonitor();
325         }
326         Map JavaDoc map = new HashMap JavaDoc();
327         
328         // first check cache (preference store) to avoid launching VM
329
Preferences preferences = JavaRuntime.getPreferences();
330         boolean cached = true;
331         for (int i = 0; i < properties.length; i++) {
332             String JavaDoc property = properties[i];
333             String JavaDoc key = getSystemPropertyKey(property);
334             if (preferences.contains(key)) {
335                 String JavaDoc value = preferences.getString(key);
336                 map.put(property, value);
337             } else {
338                 map.clear();
339                 cached = false;
340                 break;
341             }
342         }
343         if (!cached) {
344             // launch VM to evaluate properties
345
File JavaDoc file = LaunchingPlugin.getFileInPlugin(new Path("lib/launchingsupport.jar")); //$NON-NLS-1$
346
if (file.exists()) {
347                 String JavaDoc javaVersion = getJavaVersion();
348                 boolean hasXMLSupport = false;
349                 if (javaVersion != null) {
350                     hasXMLSupport = true;
351                     if (javaVersion.startsWith(JavaCore.VERSION_1_1) ||
352                             javaVersion.startsWith(JavaCore.VERSION_1_2) ||
353                             javaVersion.startsWith(JavaCore.VERSION_1_3)) {
354                         hasXMLSupport = false;
355                     }
356                 }
357                 String JavaDoc mainType = null;
358                 if (hasXMLSupport) {
359                     mainType = "org.eclipse.jdt.internal.launching.support.SystemProperties"; //$NON-NLS-1$
360
} else {
361                     mainType = "org.eclipse.jdt.internal.launching.support.LegacySystemProperties"; //$NON-NLS-1$
362
}
363                 VMRunnerConfiguration config = new VMRunnerConfiguration(mainType, new String JavaDoc[]{file.getAbsolutePath()});
364                 IVMRunner runner = getVMRunner(ILaunchManager.RUN_MODE);
365                 if (runner == null) {
366                     abort(LaunchingMessages.AbstractVMInstall_0, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
367                 }
368                 config.setProgramArguments(properties);
369                 Launch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
370                 if (monitor.isCanceled()) {
371                     return map;
372                 }
373                 monitor.beginTask(LaunchingMessages.AbstractVMInstall_1, 2);
374                 runner.run(config, launch, monitor);
375                 IProcess[] processes = launch.getProcesses();
376                 if (processes.length != 1) {
377                     abort(LaunchingMessages.AbstractVMInstall_0, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
378                 }
379                 IProcess process = processes[0];
380                 try {
381                     int total = 0;
382                     int max = JavaRuntime.getPreferences().getInt(JavaRuntime.PREF_CONNECT_TIMEOUT);
383                     while (!process.isTerminated()) {
384                         try {
385                             if (total > max) {
386                                 break;
387                             }
388                             Thread.sleep(50);
389                             total+=50;
390                         } catch (InterruptedException JavaDoc e) {
391                         }
392                     }
393                 } finally {
394                     if (!launch.isTerminated()) {
395                         launch.terminate();
396                     }
397                 }
398                 monitor.worked(1);
399                 if (monitor.isCanceled()) {
400                     return map;
401                 }
402                 
403                 monitor.subTask(LaunchingMessages.AbstractVMInstall_3);
404                 IStreamsProxy streamsProxy = process.getStreamsProxy();
405                 String JavaDoc text = null;
406                 if (streamsProxy != null) {
407                     text = streamsProxy.getOutputStreamMonitor().getContents();
408                 }
409                 if (text != null && text.length() > 0) {
410                     try {
411                         DocumentBuilder JavaDoc parser = LaunchingPlugin.getParser();
412                         Document JavaDoc document = parser.parse(new ByteArrayInputStream JavaDoc(text.getBytes()));
413                         Element JavaDoc envs = document.getDocumentElement();
414                         NodeList JavaDoc list = envs.getChildNodes();
415                         int length = list.getLength();
416                         for (int i = 0; i < length; ++i) {
417                             Node JavaDoc node = list.item(i);
418                             short type = node.getNodeType();
419                             if (type == Node.ELEMENT_NODE) {
420                                 Element JavaDoc element = (Element JavaDoc) node;
421                                 if (element.getNodeName().equals("property")) { //$NON-NLS-1$
422
String JavaDoc name = element.getAttribute("name"); //$NON-NLS-1$
423
String JavaDoc value = element.getAttribute("value"); //$NON-NLS-1$
424
map.put(name, value);
425                                 }
426                             }
427                         }
428                     } catch (SAXException JavaDoc e) {
429                         abort(LaunchingMessages.AbstractVMInstall_4, e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
430                     } catch (IOException JavaDoc e) {
431                         abort(LaunchingMessages.AbstractVMInstall_4, e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
432                     }
433                 } else {
434                     abort(LaunchingMessages.AbstractVMInstall_0, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
435                 }
436                 monitor.worked(1);
437             } else {
438                 abort(LaunchingMessages.AbstractVMInstall_0, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
439             }
440             // cache for future reference
441
Iterator JavaDoc keys = map.keySet().iterator();
442             while (keys.hasNext()) {
443                 String JavaDoc property = (String JavaDoc)keys.next();
444                 String JavaDoc value = (String JavaDoc) map.get(property);
445                 String JavaDoc key = getSystemPropertyKey(property);
446                 preferences.setValue(key, value);
447             }
448         }
449         monitor.done();
450         return map;
451     }
452
453     /**
454      * Generates a key used to cache system property for this VM in this plug-ins
455      * preference store.
456      *
457      * @param property system property name
458      * @return preference store key
459      */

460     private String JavaDoc getSystemPropertyKey(String JavaDoc property) {
461         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
462         buffer.append(PREF_VM_INSTALL_SYSTEM_PROPERTY);
463         buffer.append("."); //$NON-NLS-1$
464
buffer.append(getVMInstallType().getId());
465         buffer.append("."); //$NON-NLS-1$
466
buffer.append(getId());
467         buffer.append("."); //$NON-NLS-1$
468
buffer.append(property);
469         return buffer.toString();
470     }
471     
472     /**
473      * Throws a core exception with an error status object built from the given
474      * message, lower level exception, and error code.
475      *
476      * @param message the status message
477      * @param exception lower level exception associated with the error, or
478      * <code>null</code> if none
479      * @param code error code
480      * @throws CoreException the "abort" core exception
481      * @since 3.2
482      */

483     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
484         throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin
485                 .getUniqueIdentifier(), code, message, exception));
486     }
487     
488 }
489
Popular Tags