KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > product > JREInfo


1 /*******************************************************************************
2  * Copyright (c) 2007 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.product;
12
13 import java.io.PrintWriter JavaDoc;
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 JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26
27 public class JREInfo extends ProductObject implements IJREInfo {
28
29     private static final long serialVersionUID = 1L;
30     private String JavaDoc fJVMLin = ""; //$NON-NLS-1$
31
private String JavaDoc fJVMMac = ""; //$NON-NLS-1$
32
private String JavaDoc fJVMSol = ""; //$NON-NLS-1$
33
private String JavaDoc fJVMWin = ""; //$NON-NLS-1$
34

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 JavaDoc 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 ""; //$NON-NLS-1$
56
}
57
58     public String JavaDoc getJVMLocation(String JavaDoc 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 ""; //$NON-NLS-1$
69
}
70
71     private String JavaDoc getJVMLocation(String JavaDoc 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                     // take the first strictly compatible vm if there is no default
82
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                     // use the first vm failing that
89
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         // we can't find anything suitable
106
return null;
107     }
108
109     public void setJVM(String JavaDoc args, int platform, int type) {
110         String JavaDoc old;
111         if (args == null)
112             args = ""; //$NON-NLS-1$
113
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 JavaDoc node) {
146         NodeList JavaDoc list = node.getChildNodes();
147         for (int i = 0; i < list.getLength(); i++) {
148             Node JavaDoc 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 JavaDoc) child).getAttribute("type")); //$NON-NLS-1$
153
} else if (child.getNodeName().equals(JRE_MAC)) {
154                     fJVMMac = getText(child);
155                     fJVMMacType = parseTypeString(((Element JavaDoc) child).getAttribute("type")); //$NON-NLS-1$
156
} else if (child.getNodeName().equals(JRE_SOL)) {
157                     fJVMSol = getText(child);
158                     fJVMSolType = parseTypeString(((Element JavaDoc) child).getAttribute("type")); //$NON-NLS-1$
159
} else if (child.getNodeName().equals(JRE_WIN)) {
160                     fJVMWin = getText(child);
161                     fJVMWinType = parseTypeString(((Element JavaDoc) child).getAttribute("type")); //$NON-NLS-1$
162
}
163             }
164         }
165     }
166
167     private String JavaDoc getText(Node JavaDoc node) {
168         node.normalize();
169         Node JavaDoc text = node.getFirstChild();
170         if (text != null && text.getNodeType() == Node.TEXT_NODE) {
171             return text.getNodeValue();
172         }
173         return ""; //$NON-NLS-1$
174
}
175
176     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
177         writer.println(indent + "<vm>"); //$NON-NLS-1$
178
if (fJVMLin.length() > 0) {
179             writer.println(indent + " " + "<" + 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$
180
}
181         if (fJVMMac.length() > 0) {
182             writer.println(indent + " " + "<" + 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$
183
}
184         if (fJVMSol.length() > 0) {
185             writer.println(indent + " " + "<" + 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$
186
}
187         if (fJVMWin.length() > 0) {
188             writer.println(indent + " " + "<" + 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$
189
}
190         writer.println(indent + "</vm>"); //$NON-NLS-1$
191
}
192
193     private String JavaDoc getWritableTypeString(int type) {
194         if(type == TYPE_EE)
195             return EE;
196         if(type == TYPE_JRE)
197             return JRE;
198         return ""; //$NON-NLS-1$
199
}
200
201     private int parseTypeString(String JavaDoc 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