1 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 JavaSE_1_6 = "JavaSE-1.6"; private static final String J2SE_1_5 = "J2SE-1.5"; private static final String J2SE_1_4 = "J2SE-1.4"; private static final String J2SE_1_3 = "J2SE-1.3"; private static final String J2SE_1_2 = "J2SE-1.2"; private static final String JRE_1_1 = "JRE-1.1"; 29 private static final String CDC_FOUNDATION_1_1 = "CDC-1.1/Foundation-1.1"; private static final String CDC_FOUNDATION_1_0 = "CDC-1.0/Foundation-1.0"; 32 private static final String OSGI_MINIMUM_1_0 = "OSGi/Minimum-1.0"; private static final String OSGI_MINIMUM_1_1 = "OSGi/Minimum-1.1"; 35 private static final String JAVA_SPEC_VERSION = "java.specification.version"; private static final String JAVA_SPEC_NAME = "java.specification.name"; private static final String JAVA_VERSION = "java.version"; 39 private static final String [] VM_PROPERTIES = {JAVA_SPEC_NAME, JAVA_SPEC_VERSION, JAVA_VERSION}; 40 private static final String FOUNDATION = "foundation"; private static final Map mappings = new HashMap(); 42 43 static { 44 mappings.put(CDC_FOUNDATION_1_0, new String [] {OSGI_MINIMUM_1_0}); 46 mappings.put(CDC_FOUNDATION_1_1, new String [] {CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_1}); 47 mappings.put(OSGI_MINIMUM_1_1, new String [] {OSGI_MINIMUM_1_0}); 48 mappings.put(J2SE_1_2, new String [] {JRE_1_1}); 49 mappings.put(J2SE_1_3, new String [] {J2SE_1_2, CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_0}); 50 mappings.put(J2SE_1_4, new String [] {J2SE_1_3, CDC_FOUNDATION_1_1, OSGI_MINIMUM_1_1}); 51 mappings.put(J2SE_1_5, new String [] {J2SE_1_4}); 52 mappings.put(JavaSE_1_6, new String [] {J2SE_1_5}); 53 } 54 55 public static String getCompliance(String 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 [] getKnownExecutionEnvironments() { 68 return new String [] { 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 javaVersion = vm2.getJavaVersion(); 89 if (javaVersion == null) { 90 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")) types = getTypes(JavaSE_1_6); 98 else if (javaVersion.startsWith("1.5")) types = getTypes(J2SE_1_5); 100 else if (javaVersion.startsWith("1.4")) types = getTypes(J2SE_1_4); 102 else if (javaVersion.startsWith("1.3")) types = getTypes(J2SE_1_3); 104 else if (javaVersion.startsWith("1.2")) types = getTypes(J2SE_1_2); 106 else if (javaVersion.startsWith("1.1")) { 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")) { 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 ) types.get(i), i ==0); 120 } 121 return (CompatibleEnvironment[])result.toArray(new CompatibleEnvironment[result.size()]); 122 } 123 124 127 private boolean isFoundation(Map properties) { 128 for (int i=0; i < VM_PROPERTIES.length; i++) { 129 String value = (String ) 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; } 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; } 148 149 private void addEnvironment(ArrayList result, String 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 private List getTypes(String type) { 158 List result = new ArrayList(); 159 result.add(type); 160 String [] values = (String []) 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 |