1 11 package org.eclipse.pde.internal.core.product; 12 13 import java.io.PrintWriter ; 14 15 import org.eclipse.core.runtime.Platform; 16 import org.eclipse.jdt.launching.IVMInstall; 17 import org.eclipse.jdt.launching.IVMInstallType; 18 import org.eclipse.jdt.launching.JavaRuntime; 19 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 20 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 21 import org.eclipse.pde.internal.core.iproduct.IJREInfo; 22 import org.eclipse.pde.internal.core.iproduct.IProductModel; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.NodeList ; 26 27 public class JREInfo extends ProductObject implements IJREInfo { 28 29 private static final long serialVersionUID = 1L; 30 private String fJVMLin = ""; private String fJVMMac = ""; private String fJVMSol = ""; private String fJVMWin = ""; 35 private int fJVMLinType = 0; 36 private int fJVMMacType = 0; 37 private int fJVMSolType = 0; 38 private int fJVMWinType = 0; 39 40 public JREInfo(IProductModel model) { 41 super(model); 42 } 43 44 public String getJVM(int platform) { 45 switch (platform) { 46 case LINUX: 47 return fJVMLin; 48 case MACOS: 49 return fJVMMac; 50 case SOLAR: 51 return fJVMSol; 52 case WIN32: 53 return fJVMWin; 54 } 55 return ""; } 57 58 public String getJVMLocation(String os) { 59 if (Platform.OS_WIN32.equals(os)) { 60 return getJVMLocation(fJVMWin, fJVMWinType); 61 } else if (Platform.OS_LINUX.equals(os)) { 62 return getJVMLocation(fJVMLin, fJVMLinType); 63 } else if (Platform.OS_MACOSX.equals(os)) { 64 return getJVMLocation(fJVMMac, fJVMMacType); 65 } else if (Platform.OS_SOLARIS.equals(os)) { 66 return getJVMLocation(fJVMSol, fJVMSolType); 67 } 68 return ""; } 70 71 private String getJVMLocation(String name, int type) { 72 if(type == TYPE_EE) { 73 IExecutionEnvironmentsManager manager = 74 JavaRuntime.getExecutionEnvironmentsManager(); 75 IExecutionEnvironment environment= manager.getEnvironment(name); 76 IVMInstall vm = null; 77 if (environment != null) { 78 vm = environment.getDefaultVM(); 79 if (vm == null) { 80 IVMInstall[] installs = environment.getCompatibleVMs(); 81 for (int i = 0; i < installs.length; i++) { 83 IVMInstall install = installs[i]; 84 if (environment.isStrictlyCompatible(install)) { 85 return install.getInstallLocation().getAbsolutePath(); 86 } 87 } 88 if (vm == null && installs.length > 0) 90 return installs[0].getInstallLocation().getAbsolutePath(); 91 } 92 return vm.getInstallLocation().getAbsolutePath(); 93 } 94 } 95 else if(type == TYPE_JRE) { 96 IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); 97 for (int i = 0; i < types.length; i++) { 98 IVMInstall[] installs = types[i].getVMInstalls(); 99 for (int k = 0; k < installs.length; k++) { 100 if (installs[k].getName().equals(name)) 101 return installs[k].getInstallLocation().getAbsolutePath(); 102 } 103 } 104 } 105 return null; 107 } 108 109 public void setJVM(String args, int platform, int type) { 110 String old; 111 if (args == null) 112 args = ""; switch (platform) { 114 case LINUX: 115 old = fJVMLin; 116 fJVMLin = args; 117 fJVMLinType = type; 118 if (isEditable()) 119 firePropertyChanged(JRE_LIN, old, fJVMLin); 120 break; 121 case MACOS: 122 old = fJVMMac; 123 fJVMMac = args; 124 fJVMMacType = type; 125 if (isEditable()) 126 firePropertyChanged(JRE_MAC, old, fJVMMac); 127 break; 128 case SOLAR: 129 old = fJVMSol; 130 fJVMSol = args; 131 fJVMSolType = type; 132 if (isEditable()) 133 firePropertyChanged(JRE_SOL, old, fJVMSol); 134 break; 135 case WIN32: 136 old = fJVMWin; 137 fJVMWin = args; 138 fJVMWinType = type; 139 if (isEditable()) 140 firePropertyChanged(JRE_WIN, old, fJVMWin); 141 break; 142 } 143 } 144 145 public void parse(Node node) { 146 NodeList list = node.getChildNodes(); 147 for (int i = 0; i < list.getLength(); i++) { 148 Node child = list.item(i); 149 if (child.getNodeType() == Node.ELEMENT_NODE) { 150 if (child.getNodeName().equals(JRE_LIN)) { 151 fJVMLin = getText(child); 152 fJVMLinType = parseTypeString(((Element ) child).getAttribute("type")); } else if (child.getNodeName().equals(JRE_MAC)) { 154 fJVMMac = getText(child); 155 fJVMMacType = parseTypeString(((Element ) child).getAttribute("type")); } else if (child.getNodeName().equals(JRE_SOL)) { 157 fJVMSol = getText(child); 158 fJVMSolType = parseTypeString(((Element ) child).getAttribute("type")); } else if (child.getNodeName().equals(JRE_WIN)) { 160 fJVMWin = getText(child); 161 fJVMWinType = parseTypeString(((Element ) child).getAttribute("type")); } 163 } 164 } 165 } 166 167 private String getText(Node node) { 168 node.normalize(); 169 Node text = node.getFirstChild(); 170 if (text != null && text.getNodeType() == Node.TEXT_NODE) { 171 return text.getNodeValue(); 172 } 173 return ""; } 175 176 public void write(String indent, PrintWriter writer) { 177 writer.println(indent + "<vm>"); if (fJVMLin.length() > 0) { 179 writer.println(indent + " " + "<" + JRE_LIN + " type=\"" + getWritableTypeString(fJVMLinType) + "\">" + getWritableString(fJVMLin) + "</" + JRE_LIN + ">"); } 181 if (fJVMMac.length() > 0) { 182 writer.println(indent + " " + "<" + JRE_MAC + " type=\"" + getWritableTypeString(fJVMMacType) + "\">" + getWritableString(fJVMMac) + "</" + JRE_MAC + ">"); } 184 if (fJVMSol.length() > 0) { 185 writer.println(indent + " " + "<" + JRE_SOL + " type=\"" + getWritableTypeString(fJVMSolType) + "\">" + getWritableString(fJVMSol) + "</" + JRE_SOL + ">"); } 187 if (fJVMWin.length() > 0) { 188 writer.println(indent + " " + "<" + JRE_WIN + " type=\"" + getWritableTypeString(fJVMWinType) + "\">" + getWritableString(fJVMWin) + "</" + JRE_WIN + ">"); } 190 writer.println(indent + "</vm>"); } 192 193 private String getWritableTypeString(int type) { 194 if(type == TYPE_EE) 195 return EE; 196 if(type == TYPE_JRE) 197 return JRE; 198 return ""; } 200 201 private int parseTypeString(String type) { 202 if(type.equalsIgnoreCase(EE)) 203 return TYPE_EE; 204 if(type.equalsIgnoreCase(JRE)) 205 return TYPE_JRE; 206 return TYPE_JRE; 207 } 208 209 public int getJVMType(int platform) { 210 switch (platform) { 211 case LINUX: 212 return fJVMLinType; 213 case MACOS: 214 return fJVMMacType; 215 case SOLAR: 216 return fJVMSolType; 217 case WIN32: 218 return fJVMWinType; 219 } 220 return TYPE_JRE; 221 } 222 223 } 224 | Popular Tags |