1 11 package org.eclipse.jdt.launching; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.net.URL ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import javax.xml.parsers.DocumentBuilder ; 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 ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.xml.sax.SAXException ; 43 49 public abstract class AbstractVMInstall implements IVMInstall, IVMInstall2, IVMInstall3 { 50 51 private IVMInstallType fType; 52 private String fId; 53 private String fName; 54 private File fInstallLocation; 55 private LibraryLocation[] fSystemLibraryDescriptions; 56 private URL fJavadocLocation; 57 private String fVMArgs; 58 private static final String PREF_VM_INSTALL_SYSTEM_PROPERTY = "PREF_VM_INSTALL_SYSTEM_PROPERTY"; private boolean fNotify = true; 63 64 74 public AbstractVMInstall(IVMInstallType type, String id) { 75 if (type == null) 76 throw new IllegalArgumentException (LaunchingMessages.vmInstall_assert_typeNotNull); 77 if (id == null) 78 throw new IllegalArgumentException (LaunchingMessages.vmInstall_assert_idNotNull); 79 fType= type; 80 fId= id; 81 } 82 83 87 public String getId() { 88 return fId; 89 } 90 91 95 public String getName() { 96 return fName; 97 } 98 99 103 public void setName(String 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 117 public File getInstallLocation() { 118 return fInstallLocation; 119 } 120 121 125 public void setInstallLocation(File 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 139 public IVMInstallType getVMInstallType() { 140 return fType; 141 } 142 143 146 public IVMRunner getVMRunner(String mode) { 147 return null; 148 } 149 150 153 public LibraryLocation[] getLibraryLocations() { 154 return fSystemLibraryDescriptions; 155 } 156 157 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 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 196 public URL getJavadocLocation() { 197 return fJavadocLocation; 198 } 199 200 203 public void setJavadocLocation(URL url) { 204 if (url == fJavadocLocation) { 205 return; 206 } 207 if (url != null && fJavadocLocation != null) { 208 if (url.equals(fJavadocLocation)) { 209 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 227 protected void setNotify(boolean notify) { 228 fNotify = notify; 229 } 230 231 235 public boolean equals(Object 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 248 public int hashCode() { 249 return getVMInstallType().hashCode() + getId().hashCode(); 250 } 251 252 256 public String [] getVMArguments() { 257 String args = getVMArgs(); 258 if (args == null) { 259 return null; 260 } 261 ExecutionArguments ex = new ExecutionArguments(args, ""); return ex.getVMArgumentsArray(); 263 } 264 265 269 public void setVMArguments(String [] vmArgs) { 270 if (vmArgs == null) { 271 setVMArgs(null); 272 } else { 273 StringBuffer buf = new StringBuffer (); 274 for (int i = 0; i < vmArgs.length; i++) { 275 String string = vmArgs[i]; 276 buf.append(string); 277 buf.append(" "); } 279 setVMArgs(buf.toString().trim()); 280 } 281 } 282 283 286 public String getVMArgs() { 287 return fVMArgs; 288 } 289 290 293 public void setVMArgs(String vmArgs) { 294 if (fVMArgs == null) { 295 if (vmArgs == null) { 296 return; 298 } 299 } else if (fVMArgs.equals(vmArgs)) { 300 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 314 public String getJavaVersion() { 315 return null; 316 } 317 318 321 public Map evaluateSystemProperties(String [] properties, IProgressMonitor monitor) throws CoreException { 322 if (monitor == null) { 324 monitor = new NullProgressMonitor(); 325 } 326 Map map = new HashMap (); 327 328 Preferences preferences = JavaRuntime.getPreferences(); 330 boolean cached = true; 331 for (int i = 0; i < properties.length; i++) { 332 String property = properties[i]; 333 String key = getSystemPropertyKey(property); 334 if (preferences.contains(key)) { 335 String 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 File file = LaunchingPlugin.getFileInPlugin(new Path("lib/launchingsupport.jar")); if (file.exists()) { 347 String 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 mainType = null; 358 if (hasXMLSupport) { 359 mainType = "org.eclipse.jdt.internal.launching.support.SystemProperties"; } else { 361 mainType = "org.eclipse.jdt.internal.launching.support.LegacySystemProperties"; } 363 VMRunnerConfiguration config = new VMRunnerConfiguration(mainType, new String []{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 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 text = null; 406 if (streamsProxy != null) { 407 text = streamsProxy.getOutputStreamMonitor().getContents(); 408 } 409 if (text != null && text.length() > 0) { 410 try { 411 DocumentBuilder parser = LaunchingPlugin.getParser(); 412 Document document = parser.parse(new ByteArrayInputStream (text.getBytes())); 413 Element envs = document.getDocumentElement(); 414 NodeList list = envs.getChildNodes(); 415 int length = list.getLength(); 416 for (int i = 0; i < length; ++i) { 417 Node node = list.item(i); 418 short type = node.getNodeType(); 419 if (type == Node.ELEMENT_NODE) { 420 Element element = (Element ) node; 421 if (element.getNodeName().equals("property")) { String name = element.getAttribute("name"); String value = element.getAttribute("value"); map.put(name, value); 425 } 426 } 427 } 428 } catch (SAXException e) { 429 abort(LaunchingMessages.AbstractVMInstall_4, e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR); 430 } catch (IOException 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 Iterator keys = map.keySet().iterator(); 442 while (keys.hasNext()) { 443 String property = (String )keys.next(); 444 String value = (String ) map.get(property); 445 String key = getSystemPropertyKey(property); 446 preferences.setValue(key, value); 447 } 448 } 449 monitor.done(); 450 return map; 451 } 452 453 460 private String getSystemPropertyKey(String property) { 461 StringBuffer buffer = new StringBuffer (); 462 buffer.append(PREF_VM_INSTALL_SYSTEM_PROPERTY); 463 buffer.append("."); buffer.append(getVMInstallType().getId()); 465 buffer.append("."); buffer.append(getId()); 467 buffer.append("."); buffer.append(property); 469 return buffer.toString(); 470 } 471 472 483 protected void abort(String message, Throwable 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 |