1 11 package org.eclipse.pde.internal.core.target; 12 13 import java.io.PrintWriter ; 14 15 import org.eclipse.jdt.launching.IVMInstall; 16 import org.eclipse.jdt.launching.JavaRuntime; 17 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 18 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 19 import org.eclipse.pde.internal.core.itarget.ITargetJRE; 20 import org.eclipse.pde.internal.core.itarget.ITargetModel; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.NodeList ; 23 24 public class TargetJRE extends TargetObject implements ITargetJRE { 25 26 private static final long serialVersionUID = 1L; 27 private int fType; 28 private String fName; 29 30 public TargetJRE(ITargetModel model) { 31 super(model); 32 } 33 34 public int getJREType() { 35 return fType; 36 } 37 38 public String getJREName() { 39 return fName; 40 } 41 42 public void setNamedJRE(String name) { 43 int oldType = fType; 44 String oldName = fName; 45 fName = (name == null) ? "" : name; fType = TYPE_NAMED; 47 if (oldType != fType) 48 firePropertyChanged(P_TARGET_JRE, new Integer (oldType), new Integer (fType)); 49 else 50 firePropertyChanged(P_TARGET_JRE, oldName, fName); 51 } 52 53 public void setExecutionEnvJRE(String name) { 54 int oldType = fType; 55 String oldName = fName; 56 fName = (name == null) ? "" : name; fType = TYPE_EXECUTION_ENV; 58 if (oldType != fType) 59 firePropertyChanged(P_TARGET_JRE, new Integer (oldType), new Integer (fType)); 60 else 61 firePropertyChanged(P_TARGET_JRE, oldName, fName); 62 } 63 64 public void setDefaultJRE() { 65 int oldType = fType; 66 fName = ""; fType = TYPE_DEFAULT; 68 if (oldType != fType) 69 firePropertyChanged(P_TARGET_JRE, new Integer (oldType), new Integer (fType)); 70 } 71 72 public void parse(Node node) { 73 NodeList list = node.getChildNodes(); 74 for (int i = 0; i < list.getLength(); i++) { 75 Node child = list.item(i); 76 if (child.getNodeType() == Node.ELEMENT_NODE) { 77 if (child.getNodeName().equals("jreName")) { fType = TYPE_NAMED; 79 fName = getText(child); 80 } else if (child.getNodeName().equals("execEnv")) { fType = TYPE_EXECUTION_ENV; 82 fName = getText(child); 83 } 84 } 85 } 86 if (list.getLength() == 0) { 87 fType = TYPE_DEFAULT; 88 fName = ""; } 90 } 91 92 private String getText(Node node) { 93 node.normalize(); 94 Node text = node.getFirstChild(); 95 if (text != null && text.getNodeType() == Node.TEXT_NODE) { 96 return text.getNodeValue(); 97 } 98 return ""; } 100 101 public void write(String indent, PrintWriter writer) { 102 if (fType == 0) 103 return; 104 writer.println(); 105 writer.println(indent + "<targetJRE>"); if (fType == 1) 107 writer.println(indent + " <jreName>" + getWritableString(fName) + "</jreName>"); else if (fType == 2) 109 writer.println(indent + " <execEnv>" + getWritableString(fName) + "</execEnv>"); writer.println(indent + "</targetJRE>"); } 112 113 public String getCompatibleJRE() { 114 int jreType = getJREType(); 115 116 switch (jreType) { 117 case ITargetJRE.TYPE_DEFAULT: 118 return JavaRuntime.getDefaultVMInstall().getName(); 119 case ITargetJRE.TYPE_NAMED: 120 return getJREName(); 121 case ITargetJRE.TYPE_EXECUTION_ENV: 122 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); 123 IExecutionEnvironment environment = manager.getEnvironment(getJREName()); 124 IVMInstall vm = null; 125 if (environment != null) { 126 vm = environment.getDefaultVM(); 127 if (vm == null) { 128 IVMInstall[] installs = environment.getCompatibleVMs(); 129 for (int i = 0; i < installs.length; i++) { 131 IVMInstall install = installs[i]; 132 if (environment.isStrictlyCompatible(install)) { 133 return install.getName(); 134 } 135 } 136 if (vm == null && installs.length > 0) 138 return installs[0].getName(); 139 } 140 return vm.getName(); 141 } 142 } 143 return JavaRuntime.getDefaultVMInstall().getName(); 144 } 145 146 } 147 | Popular Tags |