1 11 package org.eclipse.jdt.internal.launching.environments; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.StringTokenizer ; 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 34 public class ExecutionEnvironmentAnalyzer implements IExecutionEnvironmentAnalyzerDelegate { 35 36 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"; 43 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"; 46 private static final String OSGI_MINIMUM_1_0 = "OSGi/Minimum-1.0"; private static final String OSGI_MINIMUM_1_1 = "OSGi/Minimum-1.1"; 49 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"; 53 private static final String [] VM_PROPERTIES = {JAVA_SPEC_NAME, JAVA_SPEC_VERSION, JAVA_VERSION}; 54 private static final String FOUNDATION = "foundation"; private static final Map mappings = new HashMap (); 56 57 static { 58 mappings.put(CDC_FOUNDATION_1_0, new String [] {OSGI_MINIMUM_1_0}); 60 mappings.put(CDC_FOUNDATION_1_1, new String [] {CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_1}); 61 mappings.put(OSGI_MINIMUM_1_1, new String [] {OSGI_MINIMUM_1_0}); 62 mappings.put(J2SE_1_2, new String [] {JRE_1_1}); 63 mappings.put(J2SE_1_3, new String [] {J2SE_1_2, CDC_FOUNDATION_1_0, OSGI_MINIMUM_1_0}); 64 mappings.put(J2SE_1_4, new String [] {J2SE_1_3, CDC_FOUNDATION_1_1, OSGI_MINIMUM_1_1}); 65 mappings.put(J2SE_1_5, new String [] {J2SE_1_4}); 66 mappings.put(JavaSE_1_6, new String [] {J2SE_1_5}); 67 } 68 public CompatibleEnvironment[] analyze(IVMInstall vm, IProgressMonitor monitor) throws CoreException { 69 ArrayList result = new ArrayList (); 70 if (!(vm instanceof IVMInstall2)) 71 return new CompatibleEnvironment[0]; 72 IVMInstall2 vm2 = (IVMInstall2) vm; 73 74 List types = null; 75 String javaVersion = vm2.getJavaVersion(); 76 if (javaVersion == null) { 77 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")) types = getTypes(JavaSE_1_6); 85 else if (javaVersion.startsWith("1.5")) types = getTypes(J2SE_1_5); 87 else if (javaVersion.startsWith("1.4")) types = getTypes(J2SE_1_4); 89 else if (javaVersion.startsWith("1.3")) types = getTypes(J2SE_1_3); 91 else if (javaVersion.startsWith("1.2")) types = getTypes(J2SE_1_2); 93 else if (javaVersion.startsWith("1.1")) { 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")) { 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 ) types.get(i), i ==0); 107 } 108 return (CompatibleEnvironment[])result.toArray(new CompatibleEnvironment[result.size()]); 109 } 110 111 114 private boolean isFoundation(Map properties) { 115 for (int i=0; i < VM_PROPERTIES.length; i++) { 116 String value = (String ) properties.get(VM_PROPERTIES[i]); 117 if (value == null) 118 continue; 119 for (StringTokenizer tokenizer = new StringTokenizer (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 map = vm.evaluateSystemProperties(VM_PROPERTIES, null); 128 return isFoundation(map) ? "1.0".equals(map.get(JAVA_SPEC_VERSION)) : false; } 130 131 private boolean isFoundation1_1(IVMInstall3 vm) throws CoreException { 132 Map map = vm.evaluateSystemProperties(VM_PROPERTIES, null); 133 return isFoundation(map) ? "1.1".equals(map.get(JAVA_SPEC_VERSION)) : false; } 135 136 private void addEnvironment(ArrayList result, String 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 private List getTypes(String type) { 145 List result = new ArrayList (); 146 result.add(type); 147 String [] values = (String []) 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 |