KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > target > TargetJRE


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.target;
12
13 import java.io.PrintWriter JavaDoc;
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 JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 public class TargetJRE extends TargetObject implements ITargetJRE {
25
26     private static final long serialVersionUID = 1L;
27     private int fType;
28     private String JavaDoc fName;
29
30     public TargetJRE(ITargetModel model) {
31         super(model);
32     }
33
34     public int getJREType() {
35         return fType;
36     }
37
38     public String JavaDoc getJREName() {
39         return fName;
40     }
41
42     public void setNamedJRE(String JavaDoc name) {
43         int oldType = fType;
44         String JavaDoc oldName = fName;
45         fName = (name == null) ? "" : name; //$NON-NLS-1$
46
fType = TYPE_NAMED;
47         if (oldType != fType)
48             firePropertyChanged(P_TARGET_JRE, new Integer JavaDoc(oldType), new Integer JavaDoc(fType));
49         else
50             firePropertyChanged(P_TARGET_JRE, oldName, fName);
51     }
52
53     public void setExecutionEnvJRE(String JavaDoc name) {
54         int oldType = fType;
55         String JavaDoc oldName = fName;
56         fName = (name == null) ? "" : name; //$NON-NLS-1$
57
fType = TYPE_EXECUTION_ENV;
58         if (oldType != fType)
59             firePropertyChanged(P_TARGET_JRE, new Integer JavaDoc(oldType), new Integer JavaDoc(fType));
60         else
61             firePropertyChanged(P_TARGET_JRE, oldName, fName);
62     }
63
64     public void setDefaultJRE() {
65         int oldType = fType;
66         fName = ""; //$NON-NLS-1$
67
fType = TYPE_DEFAULT;
68         if (oldType != fType)
69             firePropertyChanged(P_TARGET_JRE, new Integer JavaDoc(oldType), new Integer JavaDoc(fType));
70     }
71
72     public void parse(Node JavaDoc node) {
73         NodeList JavaDoc list = node.getChildNodes();
74         for (int i = 0; i < list.getLength(); i++) {
75             Node JavaDoc child = list.item(i);
76             if (child.getNodeType() == Node.ELEMENT_NODE) {
77                 if (child.getNodeName().equals("jreName")) { //$NON-NLS-1$
78
fType = TYPE_NAMED;
79                     fName = getText(child);
80                 } else if (child.getNodeName().equals("execEnv")) { //$NON-NLS-1$
81
fType = TYPE_EXECUTION_ENV;
82                     fName = getText(child);
83                 }
84             }
85         }
86         if (list.getLength() == 0) {
87             fType = TYPE_DEFAULT;
88             fName = ""; //$NON-NLS-1$
89
}
90     }
91     
92     private String JavaDoc getText(Node JavaDoc node) {
93         node.normalize();
94         Node JavaDoc text = node.getFirstChild();
95         if (text != null && text.getNodeType() == Node.TEXT_NODE) {
96             return text.getNodeValue();
97         }
98         return ""; //$NON-NLS-1$
99
}
100
101     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
102         if (fType == 0)
103             return;
104         writer.println();
105         writer.println(indent + "<targetJRE>"); //$NON-NLS-1$
106
if (fType == 1)
107             writer.println(indent + " <jreName>" + getWritableString(fName) + "</jreName>"); //$NON-NLS-1$ //$NON-NLS-2$
108
else if (fType == 2)
109             writer.println(indent + " <execEnv>" + getWritableString(fName) + "</execEnv>"); //$NON-NLS-1$ //$NON-NLS-2$
110
writer.println(indent + "</targetJRE>"); //$NON-NLS-1$
111
}
112     
113     public String JavaDoc 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                     // take the first strictly compatible vm if there is no default
130
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                     // use the first vm failing that
137
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