001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.product;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.jdt.launching.IVMInstall;
016: import org.eclipse.jdt.launching.IVMInstallType;
017: import org.eclipse.jdt.launching.JavaRuntime;
018: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
019: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
020: import org.eclipse.pde.internal.core.iproduct.IJREInfo;
021: import org.eclipse.pde.internal.core.iproduct.IProductModel;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025:
026: public class JREInfo extends ProductObject implements IJREInfo {
027:
028: private static final long serialVersionUID = 1L;
029: private String fJVMLin = ""; //$NON-NLS-1$
030: private String fJVMMac = ""; //$NON-NLS-1$
031: private String fJVMSol = ""; //$NON-NLS-1$
032: private String fJVMWin = ""; //$NON-NLS-1$
033:
034: private int fJVMLinType = 0;
035: private int fJVMMacType = 0;
036: private int fJVMSolType = 0;
037: private int fJVMWinType = 0;
038:
039: public JREInfo(IProductModel model) {
040: super (model);
041: }
042:
043: public String getJVM(int platform) {
044: switch (platform) {
045: case LINUX:
046: return fJVMLin;
047: case MACOS:
048: return fJVMMac;
049: case SOLAR:
050: return fJVMSol;
051: case WIN32:
052: return fJVMWin;
053: }
054: return ""; //$NON-NLS-1$
055: }
056:
057: public String getJVMLocation(String os) {
058: if (Platform.OS_WIN32.equals(os)) {
059: return getJVMLocation(fJVMWin, fJVMWinType);
060: } else if (Platform.OS_LINUX.equals(os)) {
061: return getJVMLocation(fJVMLin, fJVMLinType);
062: } else if (Platform.OS_MACOSX.equals(os)) {
063: return getJVMLocation(fJVMMac, fJVMMacType);
064: } else if (Platform.OS_SOLARIS.equals(os)) {
065: return getJVMLocation(fJVMSol, fJVMSolType);
066: }
067: return ""; //$NON-NLS-1$
068: }
069:
070: private String getJVMLocation(String name, int type) {
071: if (type == TYPE_EE) {
072: IExecutionEnvironmentsManager manager = JavaRuntime
073: .getExecutionEnvironmentsManager();
074: IExecutionEnvironment environment = manager
075: .getEnvironment(name);
076: IVMInstall vm = null;
077: if (environment != null) {
078: vm = environment.getDefaultVM();
079: if (vm == null) {
080: IVMInstall[] installs = environment
081: .getCompatibleVMs();
082: // take the first strictly compatible vm if there is no default
083: for (int i = 0; i < installs.length; i++) {
084: IVMInstall install = installs[i];
085: if (environment.isStrictlyCompatible(install)) {
086: return install.getInstallLocation()
087: .getAbsolutePath();
088: }
089: }
090: // use the first vm failing that
091: if (vm == null && installs.length > 0)
092: return installs[0].getInstallLocation()
093: .getAbsolutePath();
094: }
095: return vm.getInstallLocation().getAbsolutePath();
096: }
097: } else if (type == TYPE_JRE) {
098: IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
099: for (int i = 0; i < types.length; i++) {
100: IVMInstall[] installs = types[i].getVMInstalls();
101: for (int k = 0; k < installs.length; k++) {
102: if (installs[k].getName().equals(name))
103: return installs[k].getInstallLocation()
104: .getAbsolutePath();
105: }
106: }
107: }
108: // we can't find anything suitable
109: return null;
110: }
111:
112: public void setJVM(String args, int platform, int type) {
113: String old;
114: if (args == null)
115: args = ""; //$NON-NLS-1$
116: switch (platform) {
117: case LINUX:
118: old = fJVMLin;
119: fJVMLin = args;
120: fJVMLinType = type;
121: if (isEditable())
122: firePropertyChanged(JRE_LIN, old, fJVMLin);
123: break;
124: case MACOS:
125: old = fJVMMac;
126: fJVMMac = args;
127: fJVMMacType = type;
128: if (isEditable())
129: firePropertyChanged(JRE_MAC, old, fJVMMac);
130: break;
131: case SOLAR:
132: old = fJVMSol;
133: fJVMSol = args;
134: fJVMSolType = type;
135: if (isEditable())
136: firePropertyChanged(JRE_SOL, old, fJVMSol);
137: break;
138: case WIN32:
139: old = fJVMWin;
140: fJVMWin = args;
141: fJVMWinType = type;
142: if (isEditable())
143: firePropertyChanged(JRE_WIN, old, fJVMWin);
144: break;
145: }
146: }
147:
148: public void parse(Node node) {
149: NodeList list = node.getChildNodes();
150: for (int i = 0; i < list.getLength(); i++) {
151: Node child = list.item(i);
152: if (child.getNodeType() == Node.ELEMENT_NODE) {
153: if (child.getNodeName().equals(JRE_LIN)) {
154: fJVMLin = getText(child);
155: fJVMLinType = parseTypeString(((Element) child)
156: .getAttribute("type")); //$NON-NLS-1$
157: } else if (child.getNodeName().equals(JRE_MAC)) {
158: fJVMMac = getText(child);
159: fJVMMacType = parseTypeString(((Element) child)
160: .getAttribute("type")); //$NON-NLS-1$
161: } else if (child.getNodeName().equals(JRE_SOL)) {
162: fJVMSol = getText(child);
163: fJVMSolType = parseTypeString(((Element) child)
164: .getAttribute("type")); //$NON-NLS-1$
165: } else if (child.getNodeName().equals(JRE_WIN)) {
166: fJVMWin = getText(child);
167: fJVMWinType = parseTypeString(((Element) child)
168: .getAttribute("type")); //$NON-NLS-1$
169: }
170: }
171: }
172: }
173:
174: private String getText(Node node) {
175: node.normalize();
176: Node text = node.getFirstChild();
177: if (text != null && text.getNodeType() == Node.TEXT_NODE) {
178: return text.getNodeValue();
179: }
180: return ""; //$NON-NLS-1$
181: }
182:
183: public void write(String indent, PrintWriter writer) {
184: writer.println(indent + "<vm>"); //$NON-NLS-1$
185: if (fJVMLin.length() > 0) {
186: writer
187: .println(indent
188: + " " + "<" + JRE_LIN + " type=\"" + getWritableTypeString(fJVMLinType) + "\">" + getWritableString(fJVMLin) + "</" + JRE_LIN + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
189: }
190: if (fJVMMac.length() > 0) {
191: writer
192: .println(indent
193: + " " + "<" + JRE_MAC + " type=\"" + getWritableTypeString(fJVMMacType) + "\">" + getWritableString(fJVMMac) + "</" + JRE_MAC + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
194: }
195: if (fJVMSol.length() > 0) {
196: writer
197: .println(indent
198: + " " + "<" + JRE_SOL + " type=\"" + getWritableTypeString(fJVMSolType) + "\">" + getWritableString(fJVMSol) + "</" + JRE_SOL + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
199: }
200: if (fJVMWin.length() > 0) {
201: writer
202: .println(indent
203: + " " + "<" + JRE_WIN + " type=\"" + getWritableTypeString(fJVMWinType) + "\">" + getWritableString(fJVMWin) + "</" + JRE_WIN + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
204: }
205: writer.println(indent + "</vm>"); //$NON-NLS-1$
206: }
207:
208: private String getWritableTypeString(int type) {
209: if (type == TYPE_EE)
210: return EE;
211: if (type == TYPE_JRE)
212: return JRE;
213: return ""; //$NON-NLS-1$
214: }
215:
216: private int parseTypeString(String type) {
217: if (type.equalsIgnoreCase(EE))
218: return TYPE_EE;
219: if (type.equalsIgnoreCase(JRE))
220: return TYPE_JRE;
221: return TYPE_JRE;
222: }
223:
224: public int getJVMType(int platform) {
225: switch (platform) {
226: case LINUX:
227: return fJVMLinType;
228: case MACOS:
229: return fJVMMacType;
230: case SOLAR:
231: return fJVMSolType;
232: case WIN32:
233: return fJVMWinType;
234: }
235: return TYPE_JRE;
236: }
237:
238: }
|