KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > environments > EnvironmentsManager


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.jdt.internal.launching.environments;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 import com.ibm.icu.text.MessageFormat;
50
51 /**
52  * Utility class for execution environments.
53  *
54  * @since 3.2
55  */

56 public class EnvironmentsManager implements IExecutionEnvironmentsManager, IVMInstallChangedListener, IPropertyChangeListener {
57
58     /**
59      * Extension configuration element name.
60      */

61     private static final String JavaDoc ANALYZER_ELEMENT = "analyzer"; //$NON-NLS-1$
62

63     /**
64      * Extension configuration element name.
65      */

66     static final String JavaDoc ENVIRONMENT_ELEMENT = "environment"; //$NON-NLS-1$
67

68     /**
69      * Extension configuration element name.
70      */

71     static final String JavaDoc RULE_PARTICIPANT_ELEMENT = "ruleParticipant"; //$NON-NLS-1$
72

73     private static EnvironmentsManager fgManager = null;
74     
75     /**
76      * Preference store key for XML storing default environments.
77      */

78     private static final String JavaDoc PREF_DEFAULT_ENVIRONMENTS_XML = "org.eclipse.jdt.launching.PREF_DEFAULT_ENVIRONMENTS_XML"; //$NON-NLS-1$
79

80     /**
81      * List of environments
82      */

83     private List JavaDoc fEnvironments = null;
84     
85     /**
86      * List of access rule participants
87      */

88     private List JavaDoc fRuleParticipants = null;
89     
90     /**
91      * Map of environments keyed by id
92      */

93     private Map JavaDoc fEnvironmentsMap = null;
94     
95     /**
96      * Map of analyzers keyed by id
97      */

98     private Map JavaDoc fAnalyzers = null;
99     
100     /**
101      * <code>true</code> while updating the default settings pref
102      */

103     private boolean fIsUpdatingDefaults = false;
104     
105     /**
106      * Whether compatibile environnments have been initialized
107      */

108     private boolean fInitializedCompatibilities = false;
109
110     /**
111      * XML attribute
112      */

113     private static final String JavaDoc VM_ID = "vmId"; //$NON-NLS-1$
114

115     /**
116      * XML attribute
117      */

118     private static final String JavaDoc ENVIRONMENT_ID = "environmentId"; //$NON-NLS-1$
119

120     /**
121      * XML element
122      */

123     private static final String JavaDoc DEFAULT_ENVIRONMENT = "defaultEnvironment"; //$NON-NLS-1$
124

125     /**
126      * XML document
127      */

128     private static final String JavaDoc DEFAULT_ENVIRONMENTS = "defaultEnvironments"; //$NON-NLS-1$
129

130     /**
131      * Returns the singleton environments manager.
132      *
133      * @return environments manager
134      */

135     public static EnvironmentsManager getDefault() {
136         if (fgManager == null) {
137             fgManager = new EnvironmentsManager();
138         }
139         return fgManager;
140     }
141     
142     /**
143      * Constructs the new manager.
144      */

145     private EnvironmentsManager() {
146         JavaRuntime.addVMInstallChangedListener(this);
147         LaunchingPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this);
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager#getExecutionEnvironments()
152      */

153     public synchronized IExecutionEnvironment[] getExecutionEnvironments() {
154         initializeExtensions();
155         return (IExecutionEnvironment[]) fEnvironments.toArray(new IExecutionEnvironment[fEnvironments.size()]);
156     }
157     
158     /**
159      * Returns all access rule participants that are not specific to an execution environment.
160      *
161      * @return all access rule participants that are not specific to an execution environment.
162      * @since 3.3
163      */

164     public synchronized IAccessRuleParticipant[] getAccessRuleParticipants() {
165         initializeExtensions();
166         return (IAccessRuleParticipant[]) fRuleParticipants.toArray(new IAccessRuleParticipant[fRuleParticipants.size()]);
167     }
168     
169     /* (non-Javadoc)
170      * @see org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager#getEnvironment(java.lang.String)
171      */

172     public synchronized IExecutionEnvironment getEnvironment(String JavaDoc id) {
173         initializeExtensions();
174         return (IExecutionEnvironment) fEnvironmentsMap.get(id);
175     }
176     
177     /**
178      * Returns all registered analyzers
179      *
180      * @return
181      */

182     public synchronized Analyzer[] getAnalyzers() {
183         initializeExtensions();
184         Collection JavaDoc 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 JavaDoc();
193             fRuleParticipants = new ArrayList JavaDoc();
194             fEnvironmentsMap = new HashMap JavaDoc(configs.length);
195             fAnalyzers = new HashMap JavaDoc(configs.length);
196             for (int i = 0; i < configs.length; i++) {
197                 IConfigurationElement element = configs[i];
198                 String JavaDoc name = element.getName();
199                 if (name.equals(ENVIRONMENT_ELEMENT)) {
200                     String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
201
if (id == null) {
202                         LaunchingPlugin.log(MessageFormat.format("Execution environment must specify \"id\" attribute. Contributed by {0}.", new String JavaDoc[]{element.getContributor().getName()})); //$NON-NLS-1$
203
} 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 JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
210
if (id == null) {
211                         LaunchingPlugin.log(MessageFormat.format("Execution environment analyzer must specify \"id\" attribute. Contributed by {0}", new String JavaDoc[]{element.getContributor().getName()})); //$NON-NLS-1$
212
} else {
213                         fAnalyzers.put(id, new Analyzer(element));
214                     }
215                 } else if (name.equals(RULE_PARTICIPANT_ELEMENT)) {
216                     String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
217
if (id == null) {
218                         LaunchingPlugin.log(MessageFormat.format("Execution environment rule participant must specify \"id\" attribute. Contributed by {0}", new String JavaDoc[]{element.getContributor().getName()})); //$NON-NLS-1$
219
} else {
220                         fRuleParticipants.add(new AccessRuleParticipant(element));
221                     }
222                 }
223             }
224         }
225     }
226     
227     /**
228      * Initializes compatibility settings.
229      */

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                         // TODO: progress reporting?
241
analyze(install, new NullProgressMonitor());
242                     }
243                 }
244                 initializeDefaultVMs();
245             }
246         }
247     }
248     
249     /**
250      * Reads persisted default VMs from pref store
251      */

252     private synchronized void initializeDefaultVMs() {
253         String JavaDoc xml = LaunchingPlugin.getDefault().getPluginPreferences().getString(PREF_DEFAULT_ENVIRONMENTS_XML);
254         try {
255             if (xml.length() > 0) {
256                 DocumentBuilder JavaDoc parser = LaunchingPlugin.getParser();
257                 
258                 Document JavaDoc document = parser.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
259                 Element envs = document.getDocumentElement();
260                 NodeList JavaDoc list = envs.getChildNodes();
261                 int length = list.getLength();
262                 for (int i = 0; i < length; ++i) {
263                     Node JavaDoc 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 JavaDoc envId = element.getAttribute(ENVIRONMENT_ID);
269                             String JavaDoc 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 JavaDoc e) {
284             LaunchingPlugin.log(e);
285         } catch (IOException JavaDoc e) {
286             LaunchingPlugin.log(e);
287         }
288     }
289
290     /**
291      * Returns an XML description of default VMs per environment. Returns
292      * an empty string when there are none.
293      */

294     private String JavaDoc getDefatulVMsAsXML() {
295         int count = 0;
296         try {
297             Document JavaDoc 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 JavaDoc e) {
316             LaunchingPlugin.log(e);
317         } catch (IOException JavaDoc e) {
318             LaunchingPlugin.log(e);
319         } catch (TransformerException JavaDoc e) {
320             LaunchingPlugin.log(e);
321         }
322         return ""; //$NON-NLS-1$
323
}
324     
325     /**
326      * Analyzes and compatible execution environments for the given vm install.
327      *
328      * @param vm
329      * @param monitor
330      */

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     /* (non-Javadoc)
349      * @see org.eclipse.jdt.launching.IVMInstallChangedListener#defaultVMInstallChanged(org.eclipse.jdt.launching.IVMInstall, org.eclipse.jdt.launching.IVMInstall)
350      */

351     public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
352         // nothing
353
}
354
355     /* (non-Javadoc)
356      * @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmChanged(org.eclipse.jdt.launching.PropertyChangeEvent)
357      */

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     /* (non-Javadoc)
368      * @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmAdded(org.eclipse.jdt.launching.IVMInstall)
369      */

370     public synchronized void vmAdded(IVMInstall vm) {
371         // TODO: progress reporting?
372
if (vm instanceof VMStandin) {
373             return;
374         }
375         analyze(vm, new NullProgressMonitor());
376     }
377
378     /* (non-Javadoc)
379      * @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmRemoved(org.eclipse.jdt.launching.IVMInstall)
380      */

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     /* (non-Javadoc)
402      * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
403      */

404     public synchronized void propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
405         // don't respond to myself
406
if (fIsUpdatingDefaults) {
407             return;
408         }
409         if (event.getProperty().equals(PREF_DEFAULT_ENVIRONMENTS_XML)) {
410             initializeDefaultVMs();
411         }
412     }
413         
414 }
415
Popular Tags