KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > ExecutionEnvironmentAnalyzer


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;
12
13 import java.util.*;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jdt.core.JavaCore;
17 import org.eclipse.jdt.launching.*;
18 import org.eclipse.jdt.launching.environments.*;
19
20 public class ExecutionEnvironmentAnalyzer implements IExecutionEnvironmentAnalyzerDelegate {
21     
22     private static final String JavaDoc JavaSE_1_6 = "JavaSE-1.6"; //$NON-NLS-1$
23
private static final String JavaDoc J2SE_1_5 = "J2SE-1.5"; //$NON-NLS-1$
24
private static final String JavaDoc J2SE_1_4 = "J2SE-1.4"; //$NON-NLS-1$
25
private static final String JavaDoc J2SE_1_3 = "J2SE-1.3"; //$NON-NLS-1$
26
private static final String JavaDoc J2SE_1_2 = "J2SE-1.2"; //$NON-NLS-1$
27
private static final String JavaDoc JRE_1_1 = "JRE-1.1"; //$NON-NLS-1$
28

29     private static final String JavaDoc CDC_FOUNDATION_1_1 = "CDC-1.1/Foundation-1.1"; //$NON-NLS-1$
30
private static final String JavaDoc CDC_FOUNDATION_1_0 = "CDC-1.0/Foundation-1.0"; //$NON-NLS-1$
31

32     private static final String JavaDoc OSGI_MINIMUM_1_0 = "OSGi/Minimum-1.0"; //$NON-NLS-1$
33
private static final String JavaDoc OSGI_MINIMUM_1_1 = "OSGi/Minimum-1.1"; //$NON-NLS-1$
34

35     private static final String JavaDoc JAVA_SPEC_VERSION = "java.specification.version"; //$NON-NLS-1$
36
private static final String JavaDoc JAVA_SPEC_NAME = "java.specification.name"; //$NON-NLS-1$
37
private static final String JavaDoc JAVA_VERSION = "java.version"; //$NON-NLS-1$
38

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

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