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;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.StringTokenizer;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jdt.core.JavaCore;
021: import org.eclipse.jdt.launching.IVMInstall;
022: import org.eclipse.jdt.launching.IVMInstall2;
023: import org.eclipse.jdt.launching.IVMInstall3;
024: import org.eclipse.jdt.launching.JavaRuntime;
025: import org.eclipse.jdt.launching.environments.CompatibleEnvironment;
026: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
027: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentAnalyzerDelegate;
028: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
029:
030: public class ExecutionEnvironmentAnalyzer implements
031: IExecutionEnvironmentAnalyzerDelegate {
032:
033: private static final String JavaSE_1_6 = "JavaSE-1.6"; //$NON-NLS-1$
034: private static final String J2SE_1_5 = "J2SE-1.5"; //$NON-NLS-1$
035: private static final String J2SE_1_4 = "J2SE-1.4"; //$NON-NLS-1$
036: private static final String J2SE_1_3 = "J2SE-1.3"; //$NON-NLS-1$
037: private static final String J2SE_1_2 = "J2SE-1.2"; //$NON-NLS-1$
038: private static final String JRE_1_1 = "JRE-1.1"; //$NON-NLS-1$
039:
040: private static final String CDC_FOUNDATION_1_1 = "CDC-1.1/Foundation-1.1"; //$NON-NLS-1$
041: private static final String CDC_FOUNDATION_1_0 = "CDC-1.0/Foundation-1.0"; //$NON-NLS-1$
042:
043: private static final String OSGI_MINIMUM_1_0 = "OSGi/Minimum-1.0"; //$NON-NLS-1$
044: private static final String OSGI_MINIMUM_1_1 = "OSGi/Minimum-1.1"; //$NON-NLS-1$
045:
046: private static final String JAVA_SPEC_VERSION = "java.specification.version"; //$NON-NLS-1$
047: private static final String JAVA_SPEC_NAME = "java.specification.name"; //$NON-NLS-1$
048: private static final String JAVA_VERSION = "java.version"; //$NON-NLS-1$
049:
050: private static final String[] VM_PROPERTIES = { JAVA_SPEC_NAME,
051: JAVA_SPEC_VERSION, JAVA_VERSION };
052: private static final String FOUNDATION = "foundation"; //$NON-NLS-1$
053: private static final Map mappings = new HashMap();
054:
055: static {
056: // table where the key is the EE and the value is an array of EEs that it is a super-set of
057: mappings.put(CDC_FOUNDATION_1_0,
058: new String[] { OSGI_MINIMUM_1_0 });
059: mappings.put(CDC_FOUNDATION_1_1, new String[] {
060: CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_1 });
061: mappings.put(OSGI_MINIMUM_1_1,
062: new String[] { OSGI_MINIMUM_1_0 });
063: mappings.put(J2SE_1_2, new String[] { JRE_1_1 });
064: mappings.put(J2SE_1_3, new String[] { J2SE_1_2,
065: CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_0 });
066: mappings.put(J2SE_1_4, new String[] { J2SE_1_3,
067: CDC_FOUNDATION_1_1, OSGI_MINIMUM_1_1 });
068: mappings.put(J2SE_1_5, new String[] { J2SE_1_4 });
069: mappings.put(JavaSE_1_6, new String[] { J2SE_1_5 });
070: }
071:
072: public static String getCompliance(String ee) {
073: if (ee == null)
074: return null;
075: if (JavaSE_1_6.equals(ee))
076: return JavaCore.VERSION_1_6;
077: if (J2SE_1_5.equals(ee))
078: return JavaCore.VERSION_1_5;
079: if (J2SE_1_4.equals(ee) || CDC_FOUNDATION_1_1.equals(ee))
080: return JavaCore.VERSION_1_4;
081: return JavaCore.VERSION_1_3;
082: }
083:
084: public static String[] getKnownExecutionEnvironments() {
085: return new String[] { JRE_1_1, J2SE_1_2, OSGI_MINIMUM_1_0,
086: OSGI_MINIMUM_1_1, CDC_FOUNDATION_1_0, J2SE_1_3,
087: CDC_FOUNDATION_1_1, J2SE_1_4, J2SE_1_5, JavaSE_1_6 };
088: }
089:
090: public CompatibleEnvironment[] analyze(IVMInstall vm,
091: IProgressMonitor monitor) throws CoreException {
092: ArrayList result = new ArrayList();
093: if (!(vm instanceof IVMInstall2))
094: return new CompatibleEnvironment[0];
095: IVMInstall2 vm2 = (IVMInstall2) vm;
096:
097: List types = null;
098: String javaVersion = vm2.getJavaVersion();
099: if (javaVersion == null) {
100: // We have a contributed VM type. Check to see if its a foundation VM, if we can.
101: if ((vm instanceof IVMInstall3)
102: && isFoundation1_0((IVMInstall3) vm))
103: types = getTypes(CDC_FOUNDATION_1_0);
104: else if ((vm instanceof IVMInstall3)
105: && isFoundation1_1((IVMInstall3) vm))
106: types = getTypes(CDC_FOUNDATION_1_1);
107: } else {
108: if (javaVersion.startsWith("1.6")) //$NON-NLS-1$
109: types = getTypes(JavaSE_1_6);
110: else if (javaVersion.startsWith("1.5")) //$NON-NLS-1$
111: types = getTypes(J2SE_1_5);
112: else if (javaVersion.startsWith("1.4")) //$NON-NLS-1$
113: types = getTypes(J2SE_1_4);
114: else if (javaVersion.startsWith("1.3")) //$NON-NLS-1$
115: types = getTypes(J2SE_1_3);
116: else if (javaVersion.startsWith("1.2")) //$NON-NLS-1$
117: types = getTypes(J2SE_1_2);
118: else if (javaVersion.startsWith("1.1")) { //$NON-NLS-1$
119: if ((vm instanceof IVMInstall3)
120: && isFoundation1_1((IVMInstall3) vm))
121: types = getTypes(CDC_FOUNDATION_1_1);
122: else
123: types = getTypes(JRE_1_1);
124: } else if (javaVersion.startsWith("1.0")) { //$NON-NLS-1$
125: if ((vm instanceof IVMInstall3)
126: && isFoundation1_0((IVMInstall3) vm))
127: types = getTypes(CDC_FOUNDATION_1_0);
128: }
129: }
130:
131: if (types != null) {
132: for (int i = 0; i < types.size(); i++)
133: addEnvironment(result, (String) types.get(i), i == 0);
134: }
135: return (CompatibleEnvironment[]) result
136: .toArray(new CompatibleEnvironment[result.size()]);
137: }
138:
139: /*
140: * Check a couple of known system properties for the word "foundation".
141: */
142: private boolean isFoundation(Map properties) {
143: for (int i = 0; i < VM_PROPERTIES.length; i++) {
144: String value = (String) properties.get(VM_PROPERTIES[i]);
145: if (value == null)
146: continue;
147: for (StringTokenizer tokenizer = new StringTokenizer(value); tokenizer
148: .hasMoreTokens();)
149: if (FOUNDATION.equalsIgnoreCase(tokenizer.nextToken()))
150: return true;
151: }
152: return false;
153: }
154:
155: private boolean isFoundation1_0(IVMInstall3 vm)
156: throws CoreException {
157: Map map = vm.evaluateSystemProperties(VM_PROPERTIES, null);
158: return isFoundation(map) ? "1.0".equals(map.get(JAVA_SPEC_VERSION)) : false; //$NON-NLS-1$
159: }
160:
161: private boolean isFoundation1_1(IVMInstall3 vm)
162: throws CoreException {
163: Map map = vm.evaluateSystemProperties(VM_PROPERTIES, null);
164: return isFoundation(map) ? "1.1".equals(map.get(JAVA_SPEC_VERSION)) : false; //$NON-NLS-1$
165: }
166:
167: private void addEnvironment(ArrayList result, String id,
168: boolean strict) {
169: IExecutionEnvironmentsManager manager = JavaRuntime
170: .getExecutionEnvironmentsManager();
171: IExecutionEnvironment env = manager.getEnvironment(id);
172: if (env != null)
173: result.add(new CompatibleEnvironment(env, strict));
174: }
175:
176: // first entry in the list is the perfect match
177: private List getTypes(String type) {
178: List result = new ArrayList();
179: result.add(type);
180: String[] values = (String[]) mappings.get(type);
181: if (values != null) {
182: for (int i = 0; i < values.length; i++)
183: result.addAll(getTypes(values[i]));
184: }
185: return result;
186: }
187:
188: }
|