1 11 package org.eclipse.jdt.internal.launching.environments; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.xml.transform.TransformerException ; 24 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IConfigurationElement; 27 import org.eclipse.core.runtime.IExtensionPoint; 28 import org.eclipse.core.runtime.IProgressMonitor; 29 import org.eclipse.core.runtime.NullProgressMonitor; 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener; 32 import org.eclipse.jdt.internal.launching.LaunchingPlugin; 33 import org.eclipse.jdt.launching.IVMInstall; 34 import org.eclipse.jdt.launching.IVMInstallChangedListener; 35 import org.eclipse.jdt.launching.IVMInstallType; 36 import org.eclipse.jdt.launching.JavaRuntime; 37 import org.eclipse.jdt.launching.PropertyChangeEvent; 38 import org.eclipse.jdt.launching.VMStandin; 39 import org.eclipse.jdt.launching.environments.CompatibleEnvironment; 40 import org.eclipse.jdt.launching.environments.IAccessRuleParticipant; 41 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 42 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.xml.sax.SAXException ; 48 49 import com.ibm.icu.text.MessageFormat; 50 51 56 public class EnvironmentsManager implements IExecutionEnvironmentsManager, IVMInstallChangedListener, IPropertyChangeListener { 57 58 61 private static final String ANALYZER_ELEMENT = "analyzer"; 63 66 static final String ENVIRONMENT_ELEMENT = "environment"; 68 71 static final String RULE_PARTICIPANT_ELEMENT = "ruleParticipant"; 73 private static EnvironmentsManager fgManager = null; 74 75 78 private static final String PREF_DEFAULT_ENVIRONMENTS_XML = "org.eclipse.jdt.launching.PREF_DEFAULT_ENVIRONMENTS_XML"; 80 83 private List fEnvironments = null; 84 85 88 private List fRuleParticipants = null; 89 90 93 private Map fEnvironmentsMap = null; 94 95 98 private Map fAnalyzers = null; 99 100 103 private boolean fIsUpdatingDefaults = false; 104 105 108 private boolean fInitializedCompatibilities = false; 109 110 113 private static final String VM_ID = "vmId"; 115 118 private static final String ENVIRONMENT_ID = "environmentId"; 120 123 private static final String DEFAULT_ENVIRONMENT = "defaultEnvironment"; 125 128 private static final String DEFAULT_ENVIRONMENTS = "defaultEnvironments"; 130 135 public static EnvironmentsManager getDefault() { 136 if (fgManager == null) { 137 fgManager = new EnvironmentsManager(); 138 } 139 return fgManager; 140 } 141 142 145 private EnvironmentsManager() { 146 JavaRuntime.addVMInstallChangedListener(this); 147 LaunchingPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this); 148 } 149 150 153 public synchronized IExecutionEnvironment[] getExecutionEnvironments() { 154 initializeExtensions(); 155 return (IExecutionEnvironment[]) fEnvironments.toArray(new IExecutionEnvironment[fEnvironments.size()]); 156 } 157 158 164 public synchronized IAccessRuleParticipant[] getAccessRuleParticipants() { 165 initializeExtensions(); 166 return (IAccessRuleParticipant[]) fRuleParticipants.toArray(new IAccessRuleParticipant[fRuleParticipants.size()]); 167 } 168 169 172 public synchronized IExecutionEnvironment getEnvironment(String id) { 173 initializeExtensions(); 174 return (IExecutionEnvironment) fEnvironmentsMap.get(id); 175 } 176 177 182 public synchronized Analyzer[] getAnalyzers() { 183 initializeExtensions(); 184 Collection collection = fAnalyzers.values(); 185 return (Analyzer[]) collection.toArray(new Analyzer[collection.size()]); 186 } 187 188 private synchronized void initializeExtensions() { 189 if (fEnvironments == null) { 190 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, JavaRuntime.EXTENSION_POINT_EXECUTION_ENVIRONMENTS); 191 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 192 fEnvironments = new ArrayList (); 193 fRuleParticipants = new ArrayList (); 194 fEnvironmentsMap = new HashMap (configs.length); 195 fAnalyzers = new HashMap (configs.length); 196 for (int i = 0; i < configs.length; i++) { 197 IConfigurationElement element = configs[i]; 198 String name = element.getName(); 199 if (name.equals(ENVIRONMENT_ELEMENT)) { 200 String id = element.getAttribute("id"); if (id == null) { 202 LaunchingPlugin.log(MessageFormat.format("Execution environment must specify \"id\" attribute. Contributed by {0}.", new String []{element.getContributor().getName()})); } else { 204 IExecutionEnvironment env = new ExecutionEnvironment(element); 205 fEnvironments.add(env); 206 fEnvironmentsMap.put(id, env); 207 } 208 } else if (name.equals(ANALYZER_ELEMENT)) { 209 String id = element.getAttribute("id"); if (id == null) { 211 LaunchingPlugin.log(MessageFormat.format("Execution environment analyzer must specify \"id\" attribute. Contributed by {0}", new String []{element.getContributor().getName()})); } else { 213 fAnalyzers.put(id, new Analyzer(element)); 214 } 215 } else if (name.equals(RULE_PARTICIPANT_ELEMENT)) { 216 String id = element.getAttribute("id"); if (id == null) { 218 LaunchingPlugin.log(MessageFormat.format("Execution environment rule participant must specify \"id\" attribute. Contributed by {0}", new String []{element.getContributor().getName()})); } else { 220 fRuleParticipants.add(new AccessRuleParticipant(element)); 221 } 222 } 223 } 224 } 225 } 226 227 230 void initializeCompatibilities() { 231 IVMInstallType[] installTypes = JavaRuntime.getVMInstallTypes(); 232 synchronized (this) { 233 if (!fInitializedCompatibilities) { 234 fInitializedCompatibilities = true; 235 for (int i = 0; i < installTypes.length; i++) { 236 IVMInstallType type = installTypes[i]; 237 IVMInstall[] installs = type.getVMInstalls(); 238 for (int j = 0; j < installs.length; j++) { 239 IVMInstall install = installs[j]; 240 analyze(install, new NullProgressMonitor()); 242 } 243 } 244 initializeDefaultVMs(); 245 } 246 } 247 } 248 249 252 private synchronized void initializeDefaultVMs() { 253 String xml = LaunchingPlugin.getDefault().getPluginPreferences().getString(PREF_DEFAULT_ENVIRONMENTS_XML); 254 try { 255 if (xml.length() > 0) { 256 DocumentBuilder parser = LaunchingPlugin.getParser(); 257 258 Document document = parser.parse(new ByteArrayInputStream (xml.getBytes())); 259 Element envs = document.getDocumentElement(); 260 NodeList list = envs.getChildNodes(); 261 int length = list.getLength(); 262 for (int i = 0; i < length; ++i) { 263 Node node = list.item(i); 264 short type = node.getNodeType(); 265 if (type == Node.ELEMENT_NODE) { 266 Element element = (Element) node; 267 if (element.getNodeName().equals(DEFAULT_ENVIRONMENT)) { 268 String envId = element.getAttribute(ENVIRONMENT_ID); 269 String vmId = element.getAttribute(VM_ID); 270 ExecutionEnvironment environment = (ExecutionEnvironment) getEnvironment(envId); 271 if (environment != null) { 272 IVMInstall vm = JavaRuntime.getVMFromCompositeId(vmId); 273 if (vm != null) { 274 environment.initDefaultVM(vm); 275 } 276 } 277 } 278 } 279 } 280 } 281 } catch (CoreException e) { 282 LaunchingPlugin.log(e); 283 } catch (SAXException e) { 284 LaunchingPlugin.log(e); 285 } catch (IOException e) { 286 LaunchingPlugin.log(e); 287 } 288 } 289 290 294 private String getDefatulVMsAsXML() { 295 int count = 0; 296 try { 297 Document doc = LaunchingPlugin.getDocument(); 298 Element envs = doc.createElement(DEFAULT_ENVIRONMENTS); 299 doc.appendChild(envs); 300 IExecutionEnvironment[] environments = getExecutionEnvironments(); 301 for (int i = 0; i < environments.length; i++) { 302 IExecutionEnvironment env = environments[i]; 303 IVMInstall vm = env.getDefaultVM(); 304 if (vm != null) { 305 count++; 306 Element element = doc.createElement(DEFAULT_ENVIRONMENT); 307 element.setAttribute(ENVIRONMENT_ID, env.getId()); 308 element.setAttribute(VM_ID, JavaRuntime.getCompositeIdFromVM(vm)); 309 envs.appendChild(element); 310 } 311 } 312 if (count > 0) { 313 return LaunchingPlugin.serializeDocument(doc); 314 } 315 } catch (ParserConfigurationException e) { 316 LaunchingPlugin.log(e); 317 } catch (IOException e) { 318 LaunchingPlugin.log(e); 319 } catch (TransformerException e) { 320 LaunchingPlugin.log(e); 321 } 322 return ""; } 324 325 331 private void analyze(IVMInstall vm, IProgressMonitor monitor) { 332 Analyzer[] analyzers = getAnalyzers(); 333 for (int i = 0; i < analyzers.length; i++) { 334 Analyzer analyzer = analyzers[i]; 335 try { 336 CompatibleEnvironment[] environments = analyzer.analyze(vm, monitor); 337 for (int j = 0; j < environments.length; j++) { 338 CompatibleEnvironment compatibleEnvironment = environments[j]; 339 ExecutionEnvironment environment = (ExecutionEnvironment) compatibleEnvironment.getCompatibleEnvironment(); 340 environment.add(vm, compatibleEnvironment.isStrictlyCompatbile()); 341 } 342 } catch (CoreException e) { 343 LaunchingPlugin.log(e); 344 } 345 } 346 } 347 348 351 public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) { 352 } 354 355 358 public synchronized void vmChanged(PropertyChangeEvent event) { 359 IVMInstall vm = (IVMInstall) event.getSource(); 360 if (vm instanceof VMStandin) { 361 return; 362 } 363 vmRemoved(vm); 364 vmAdded(vm); 365 } 366 367 370 public synchronized void vmAdded(IVMInstall vm) { 371 if (vm instanceof VMStandin) { 373 return; 374 } 375 analyze(vm, new NullProgressMonitor()); 376 } 377 378 381 public synchronized void vmRemoved(IVMInstall vm) { 382 if (vm instanceof VMStandin) { 383 return; 384 } 385 IExecutionEnvironment[] environments = getExecutionEnvironments(); 386 for (int i = 0; i < environments.length; i++) { 387 ExecutionEnvironment environment = (ExecutionEnvironment) environments[i]; 388 environment.remove(vm); 389 } 390 } 391 392 synchronized void updateDefaultVMs() { 393 try { 394 fIsUpdatingDefaults = true; 395 LaunchingPlugin.getDefault().getPluginPreferences().setValue(PREF_DEFAULT_ENVIRONMENTS_XML, getDefatulVMsAsXML()); 396 } finally { 397 fIsUpdatingDefaults = false; 398 } 399 } 400 401 404 public synchronized void propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) { 405 if (fIsUpdatingDefaults) { 407 return; 408 } 409 if (event.getProperty().equals(PREF_DEFAULT_ENVIRONMENTS_XML)) { 410 initializeDefaultVMs(); 411 } 412 } 413 414 } 415 | Popular Tags |