001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.target;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.jdt.launching.IVMInstall;
015: import org.eclipse.jdt.launching.JavaRuntime;
016: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
017: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
018: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
019: import org.eclipse.pde.internal.core.itarget.ITargetModel;
020: import org.w3c.dom.Node;
021: import org.w3c.dom.NodeList;
022:
023: public class TargetJRE extends TargetObject implements ITargetJRE {
024:
025: private static final long serialVersionUID = 1L;
026: private int fType;
027: private String fName;
028:
029: public TargetJRE(ITargetModel model) {
030: super (model);
031: }
032:
033: public int getJREType() {
034: return fType;
035: }
036:
037: public String getJREName() {
038: return fName;
039: }
040:
041: public void setNamedJRE(String name) {
042: int oldType = fType;
043: String oldName = fName;
044: fName = (name == null) ? "" : name; //$NON-NLS-1$
045: fType = TYPE_NAMED;
046: if (oldType != fType)
047: firePropertyChanged(P_TARGET_JRE, new Integer(oldType),
048: new Integer(fType));
049: else
050: firePropertyChanged(P_TARGET_JRE, oldName, fName);
051: }
052:
053: public void setExecutionEnvJRE(String name) {
054: int oldType = fType;
055: String oldName = fName;
056: fName = (name == null) ? "" : name; //$NON-NLS-1$
057: fType = TYPE_EXECUTION_ENV;
058: if (oldType != fType)
059: firePropertyChanged(P_TARGET_JRE, new Integer(oldType),
060: new Integer(fType));
061: else
062: firePropertyChanged(P_TARGET_JRE, oldName, fName);
063: }
064:
065: public void setDefaultJRE() {
066: int oldType = fType;
067: fName = ""; //$NON-NLS-1$
068: fType = TYPE_DEFAULT;
069: if (oldType != fType)
070: firePropertyChanged(P_TARGET_JRE, new Integer(oldType),
071: new Integer(fType));
072: }
073:
074: public void parse(Node node) {
075: NodeList list = node.getChildNodes();
076: for (int i = 0; i < list.getLength(); i++) {
077: Node child = list.item(i);
078: if (child.getNodeType() == Node.ELEMENT_NODE) {
079: if (child.getNodeName().equals("jreName")) { //$NON-NLS-1$
080: fType = TYPE_NAMED;
081: fName = getText(child);
082: } else if (child.getNodeName().equals("execEnv")) { //$NON-NLS-1$
083: fType = TYPE_EXECUTION_ENV;
084: fName = getText(child);
085: }
086: }
087: }
088: if (list.getLength() == 0) {
089: fType = TYPE_DEFAULT;
090: fName = ""; //$NON-NLS-1$
091: }
092: }
093:
094: private String getText(Node node) {
095: node.normalize();
096: Node text = node.getFirstChild();
097: if (text != null && text.getNodeType() == Node.TEXT_NODE) {
098: return text.getNodeValue();
099: }
100: return ""; //$NON-NLS-1$
101: }
102:
103: public void write(String indent, PrintWriter writer) {
104: if (fType == 0)
105: return;
106: writer.println();
107: writer.println(indent + "<targetJRE>"); //$NON-NLS-1$
108: if (fType == 1)
109: writer
110: .println(indent
111: + " <jreName>" + getWritableString(fName) + "</jreName>"); //$NON-NLS-1$ //$NON-NLS-2$
112: else if (fType == 2)
113: writer
114: .println(indent
115: + " <execEnv>" + getWritableString(fName) + "</execEnv>"); //$NON-NLS-1$ //$NON-NLS-2$
116: writer.println(indent + "</targetJRE>"); //$NON-NLS-1$
117: }
118:
119: public String getCompatibleJRE() {
120: int jreType = getJREType();
121:
122: switch (jreType) {
123: case ITargetJRE.TYPE_DEFAULT:
124: return JavaRuntime.getDefaultVMInstall().getName();
125: case ITargetJRE.TYPE_NAMED:
126: return getJREName();
127: case ITargetJRE.TYPE_EXECUTION_ENV:
128: IExecutionEnvironmentsManager manager = JavaRuntime
129: .getExecutionEnvironmentsManager();
130: IExecutionEnvironment environment = manager
131: .getEnvironment(getJREName());
132: IVMInstall vm = null;
133: if (environment != null) {
134: vm = environment.getDefaultVM();
135: if (vm == null) {
136: IVMInstall[] installs = environment
137: .getCompatibleVMs();
138: // take the first strictly compatible vm if there is no default
139: for (int i = 0; i < installs.length; i++) {
140: IVMInstall install = installs[i];
141: if (environment.isStrictlyCompatible(install)) {
142: return install.getName();
143: }
144: }
145: // use the first vm failing that
146: if (vm == null && installs.length > 0)
147: return installs[0].getName();
148: }
149: return vm.getName();
150: }
151: }
152: return JavaRuntime.getDefaultVMInstall().getName();
153: }
154:
155: }
|