KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > environments > ExecutionEnvironmentAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 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.jdt.internal.launching.environments;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jdt.launching.IVMInstall;
22 import org.eclipse.jdt.launching.IVMInstall2;
23 import org.eclipse.jdt.launching.IVMInstall3;
24 import org.eclipse.jdt.launching.JavaRuntime;
25 import org.eclipse.jdt.launching.environments.CompatibleEnvironment;
26 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
27 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentAnalyzerDelegate;
28 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
29 /**
30  * Environment analyzer for standard execution environments.
31  *
32  * @since 3.3
33  */

34 public class ExecutionEnvironmentAnalyzer implements IExecutionEnvironmentAnalyzerDelegate {
35     
36     private static final String JavaDoc JavaSE_1_6 = "JavaSE-1.6"; //$NON-NLS-1$
37
private static final String JavaDoc J2SE_1_5 = "J2SE-1.5"; //$NON-NLS-1$
38
private static final String JavaDoc J2SE_1_4 = "J2SE-1.4"; //$NON-NLS-1$
39
private static final String JavaDoc J2SE_1_3 = "J2SE-1.3"; //$NON-NLS-1$
40
private static final String JavaDoc J2SE_1_2 = "J2SE-1.2"; //$NON-NLS-1$
41
private static final String JavaDoc JRE_1_1 = "JRE-1.1"; //$NON-NLS-1$
42

43     private static final String JavaDoc CDC_FOUNDATION_1_1 = "CDC-1.1/Foundation-1.1"; //$NON-NLS-1$
44
private static final String JavaDoc CDC_FOUNDATION_1_0 = "CDC-1.0/Foundation-1.0"; //$NON-NLS-1$
45

46     private static final String JavaDoc OSGI_MINIMUM_1_0 = "OSGi/Minimum-1.0"; //$NON-NLS-1$
47
private static final String JavaDoc OSGI_MINIMUM_1_1 = "OSGi/Minimum-1.1"; //$NON-NLS-1$
48

49     private static final String JavaDoc JAVA_SPEC_VERSION = "java.specification.version"; //$NON-NLS-1$
50
private static final String JavaDoc JAVA_SPEC_NAME = "java.specification.name"; //$NON-NLS-1$
51
private static final String JavaDoc JAVA_VERSION = "java.version"; //$NON-NLS-1$
52

53     private static final String JavaDoc[] VM_PROPERTIES = {JAVA_SPEC_NAME, JAVA_SPEC_VERSION, JAVA_VERSION};
54     private static final String JavaDoc FOUNDATION = "foundation"; //$NON-NLS-1$
55
private static final Map JavaDoc mappings = new HashMap JavaDoc();
56
57     static {
58         // table where the key is the EE and the value is an array of EEs that it is a super-set of
59
mappings.put(CDC_FOUNDATION_1_0, new String JavaDoc[] {OSGI_MINIMUM_1_0});
60         mappings.put(CDC_FOUNDATION_1_1, new String JavaDoc[] {CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_1});
61         mappings.put(OSGI_MINIMUM_1_1, new String JavaDoc[] {OSGI_MINIMUM_1_0});
62         mappings.put(J2SE_1_2, new String JavaDoc[] {JRE_1_1});
63         mappings.put(J2SE_1_3, new String JavaDoc[] {J2SE_1_2, CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_0});
64         mappings.put(J2SE_1_4, new String JavaDoc[] {J2SE_1_3, CDC_FOUNDATION_1_1, OSGI_MINIMUM_1_1});
65         mappings.put(J2SE_1_5, new String JavaDoc[] {J2SE_1_4});
66         mappings.put(JavaSE_1_6, new String JavaDoc[] {J2SE_1_5});
67     }
68     public CompatibleEnvironment[] analyze(IVMInstall vm, IProgressMonitor monitor) throws CoreException {
69         ArrayList JavaDoc result = new ArrayList JavaDoc();
70         if (!(vm instanceof IVMInstall2))
71             return new CompatibleEnvironment[0];
72         IVMInstall2 vm2 = (IVMInstall2) vm;
73         
74         List JavaDoc types = null;
75         String JavaDoc javaVersion = vm2.getJavaVersion();
76         if (javaVersion == null) {
77             // We have a contributed VM type. Check to see if its a foundation VM, if we can.
78
if ((vm instanceof IVMInstall3) && isFoundation1_0((IVMInstall3) vm))
79                 types = getTypes(CDC_FOUNDATION_1_0);
80             else if ((vm instanceof IVMInstall3) && isFoundation1_1((IVMInstall3) vm))
81                 types = getTypes(CDC_FOUNDATION_1_1);
82         } else {
83             if (javaVersion.startsWith("1.6")) //$NON-NLS-1$
84
types = getTypes(JavaSE_1_6);
85             else if (javaVersion.startsWith("1.5")) //$NON-NLS-1$
86
types = getTypes(J2SE_1_5);
87             else if (javaVersion.startsWith("1.4")) //$NON-NLS-1$
88
types = getTypes(J2SE_1_4);
89             else if (javaVersion.startsWith("1.3")) //$NON-NLS-1$
90
types = getTypes(J2SE_1_3);
91             else if (javaVersion.startsWith("1.2")) //$NON-NLS-1$
92
types = getTypes(J2SE_1_2);
93             else if (javaVersion.startsWith("1.1")) { //$NON-NLS-1$
94
if ((vm instanceof IVMInstall3) && isFoundation1_1((IVMInstall3) vm))
95                     types = getTypes(CDC_FOUNDATION_1_1);
96                 else
97                     types = getTypes(JRE_1_1);
98             } else if (javaVersion.startsWith("1.0")) { //$NON-NLS-1$
99
if ((vm instanceof IVMInstall3) && isFoundation1_0((IVMInstall3) vm))
100                     types = getTypes(CDC_FOUNDATION_1_0);
101             }
102         }
103
104         if (types != null) {
105             for (int i=0; i < types.size(); i++)
106                 addEnvironment(result, (String JavaDoc) types.get(i), i ==0);
107         }
108         return (CompatibleEnvironment[])result.toArray(new CompatibleEnvironment[result.size()]);
109     }
110
111     /*
112      * Check a couple of known system properties for the word "foundation".
113      */

114     private boolean isFoundation(Map JavaDoc properties) {
115         for (int i=0; i < VM_PROPERTIES.length; i++) {
116             String JavaDoc value = (String JavaDoc) properties.get(VM_PROPERTIES[i]);
117             if (value == null)
118                 continue;
119             for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value); tokenizer.hasMoreTokens(); )
120                 if (FOUNDATION.equalsIgnoreCase(tokenizer.nextToken()))
121                     return true;
122         }
123         return false;
124     }
125
126     private boolean isFoundation1_0(IVMInstall3 vm) throws CoreException {
127         Map JavaDoc map = vm.evaluateSystemProperties(VM_PROPERTIES, null);
128         return isFoundation(map) ? "1.0".equals(map.get(JAVA_SPEC_VERSION)) : false; //$NON-NLS-1$
129
}
130
131     private boolean isFoundation1_1(IVMInstall3 vm) throws CoreException {
132         Map JavaDoc map = vm.evaluateSystemProperties(VM_PROPERTIES, null);
133         return isFoundation(map) ? "1.1".equals(map.get(JAVA_SPEC_VERSION)) : false; //$NON-NLS-1$
134
}
135
136     private void addEnvironment(ArrayList JavaDoc result, String JavaDoc id, boolean strict) {
137         IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
138         IExecutionEnvironment env = manager.getEnvironment(id);
139         if (env != null)
140             result.add(new CompatibleEnvironment(env, strict));
141     }
142     
143     // first entry in the list is the perfect match
144
private List JavaDoc getTypes(String JavaDoc type) {
145         List JavaDoc result = new ArrayList JavaDoc();
146         result.add(type);
147         String JavaDoc[] values = (String JavaDoc[]) mappings.get(type);
148         if (values != null) {
149             for (int i=0; i<values.length; i++)
150                 result.addAll(getTypes(values[i]));
151         }
152         return result;
153     }
154
155 }
156
Popular Tags