KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > model > PluginRegistryModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.runtime.model;
12
13 import java.net.URL JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.internal.model.PluginMap;
17 import org.eclipse.core.internal.model.RegistryResolver;
18 import org.eclipse.core.internal.plugins.InternalPlatform;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.IStatus;
21
22 /**
23  * A container for a collection of plug-in descriptors.
24  * <p>
25  * This class may be instantiated, or further subclassed.
26  * </p>
27  * @deprecated In Eclipse 3.0 the runtime was refactored and all
28  * non-essential elements removed. This class provides facilities primarily intended
29  * for tooling. As such it has been removed and no directly substitutable API provided.
30  */

31 public class PluginRegistryModel {
32
33     // transient properties (not included in plug-in manifest)
34
protected PluginMap plugins = new PluginMap(new HashMap JavaDoc(30), false, true);
35     protected PluginMap fragments = new PluginMap(new HashMap JavaDoc(30), false, true);
36     private boolean readOnly = false;
37     private boolean resolved = false;
38
39     /**
40      * Creates a new plug-in registry model which contains no plug-ins.
41      */

42     public PluginRegistryModel() {
43         super();
44     }
45
46     /**
47      * Adds the specified plug-in fragment to this registry. An existing fragment
48      * with the same unique id and version is replaced by the new
49      * value.
50      *
51      * @param fragment the plug-in fragment to add
52      */

53     public void addFragment(PluginFragmentModel fragment) {
54         assertIsWriteable();
55         fragments.add(fragment);
56     }
57
58     /**
59      * Adds the specified plug-in to this registry. An existing plug-in
60      * with the same unique id and version is replaced by the new
61      * value.
62      *
63      * @param plugin the plug-in descriptor to add
64      */

65     public void addPlugin(PluginDescriptorModel plugin) {
66         assertIsWriteable();
67         plugins.add(plugin);
68     }
69
70     /**
71      * Checks that this model object is writeable. A runtime exception
72      * is thrown if it is not.
73      */

74     protected void assertIsWriteable() {
75         Assert.isTrue(!isReadOnly(), "Model is read-only"); //$NON-NLS-1$
76
}
77
78     /**
79      * Returns the plug-in fragment with the given identifier
80      * in this plug-in registry, or <code>null</code> if there is no such
81      * fragment. If there are multiple versions of the identified fragment,
82      * one will be non-deterministically chosen and returned.
83      *
84      * @param id the unique identifier of the plug-in fragment
85      * (e.g. <code>"com.example.acme"</code>).
86      * @return the plug-in fragment, or <code>null</code>
87      */

88     public PluginFragmentModel getFragment(String JavaDoc id) {
89         return (PluginFragmentModel) fragments.getAny(id);
90     }
91
92     /**
93      * Returns the identified plug-in fragment or <code>null</code> if
94      * the fragment does not exist.
95      *
96      * @param id the unique identifier of the plug-in fragment
97      * @param version fragment version identifier. If <code>null</code> is
98      * specified, a non-deterministically chosen version of the identified fragment
99      * (if any) will be returned
100      * @return the matching fragment in this registry, or <code>null</code>
101      */

102     public PluginFragmentModel getFragment(String JavaDoc id, String JavaDoc version) {
103         return (PluginFragmentModel) fragments.get(id, version);
104     }
105
106     /**
107      * Returns the list of plug-in fragments managed by this registry.
108      *
109      * @return the fragments in this registry
110      */

111     public PluginFragmentModel[] getFragments() {
112         PluginFragmentModel[] result = new PluginFragmentModel[fragments.size()];
113         fragments.copyToArray(result);
114         return result;
115     }
116
117     /**
118      * Returns all versions of the identified plug-in fragment
119      * known to this plug-in registry.
120      * Returns an empty array if there are no fragments
121      * with the specified identifier.
122      *
123      * @param id the unique identifier of the plug-in fragment
124      * (e.g. <code>"org.eclipse.core.resources"</code>).
125      * @return the fragments known to this plug-in registry with the given id
126      */

127     public PluginFragmentModel[] getFragments(String JavaDoc id) {
128         List JavaDoc versions = fragments.getVersions(id);
129         if (versions == null || versions.isEmpty())
130             return new PluginFragmentModel[0];
131         return (PluginFragmentModel[]) versions.toArray(new PluginFragmentModel[versions.size()]);
132     }
133
134     /**
135      * Returns the plug-in descriptor with the given plug-in identifier
136      * in this plug-in registry, or <code>null</code> if there is no such
137      * plug-in. If there are multiple versions of the identified plug-in,
138      * one will be non-deterministically chosen and returned.
139      *
140      * @param pluginId the unique identifier of the plug-in
141      * (e.g. <code>"com.example.acme"</code>).
142      * @return the plug-in descriptor, or <code>null</code>
143      */

144     public PluginDescriptorModel getPlugin(String JavaDoc pluginId) {
145         return (PluginDescriptorModel) plugins.getAny(pluginId);
146     }
147
148     /**
149      * Returns the identified plug-in or <code>null</code> if
150      * the plug-in does not exist.
151      *
152      * @param pluginId the unique identifier of the plug-in
153      * (e.g. <code>"org.eclipse.core.resources"</code>)
154      * @param version plug-in version identifier. If <code>null</code> is specified,
155      * a non-deterministically chosen version of the identified plug-in (if any)
156      * will be returned
157      * @return the matching plug-in in this registry or <code>null</code>
158      */

159     public PluginDescriptorModel getPlugin(String JavaDoc pluginId, String JavaDoc version) {
160         PluginDescriptorModel[] list = getPlugins(pluginId);
161         if (list == null || list.length == 0)
162             return null;
163         if (version == null)
164             // Just return the first one in the list (random)
165
return list[0];
166
167         for (int i = 0; i < list.length; i++) {
168             PluginDescriptorModel element = list[i];
169             if (element.getVersion().equals(version))
170                 return element;
171         }
172         return null;
173     }
174
175     /**
176      * Returns the list of plug-ins managed by this registry.
177      *
178      * @return the plug-ins in this registry
179      */

180     public PluginDescriptorModel[] getPlugins() {
181         PluginDescriptorModel[] result = new PluginDescriptorModel[plugins.size()];
182         plugins.copyToArray(result);
183         return result;
184     }
185
186     /**
187      * Returns all versions of the identified plug-in descriptor
188      * known to this plug-in registry.
189      * Returns an empty array if there are no plug-ins
190      * with the specified identifier.
191      *
192      * @param pluginId the unique identifier of the plug-in
193      * (e.g. <code>"org.eclipse.core.resources"</code>).
194      * @return the plug-in descriptors known to this plug-in registry
195      */

196     public PluginDescriptorModel[] getPlugins(String JavaDoc pluginId) {
197         List JavaDoc versions = plugins.getVersions(pluginId);
198         if (versions == null || versions.isEmpty())
199             return new PluginDescriptorModel[0];
200         return (PluginDescriptorModel[]) versions.toArray(new PluginDescriptorModel[versions.size()]);
201
202     }
203
204     /**
205      * Returns whether or not this model object is read-only.
206      *
207      * @return <code>true</code> if this model object is read-only,
208      * <code>false</code> otherwise
209      * @see #markReadOnly()
210      */

211     public boolean isReadOnly() {
212         return readOnly;
213     }
214
215     /**
216      * Returns whether or not this model object has been resolved.
217      *
218      * @return <code>true</code> if this model object has been resolved,
219      * <code>false</code> otherwise
220      */

221     public boolean isResolved() {
222         return resolved;
223     }
224
225     /**
226      * Sets this model object and all of its descendents to be read-only.
227      * Subclasses may extend this implementation.
228      *
229      * @see #isReadOnly()
230      */

231     public void markReadOnly() {
232         readOnly = true;
233         plugins.markReadOnly();
234         fragments.markReadOnly();
235     }
236
237     /**
238      * Sets this model object to be resolved.
239      */

240     public void markResolved() {
241         resolved = true;
242     }
243
244     /**
245      * Removes the fragment with id and version if it exists in this registry.
246      * This method has no effect if a fragment with the given id and version
247      * cannot be found.
248      *
249      * @param id the unique identifier of the fragment to remove
250      * @param version the version of the fragment to remove
251      */

252     public void removeFragment(String JavaDoc id, String JavaDoc version) {
253         assertIsWriteable();
254         fragments.remove(id, version);
255     }
256
257     /**
258      * Removes all versions of the identified plug-in fragment from this registry.
259      * This method has no effect if such a fragment cannot be found.
260      *
261      * @param id the unique identifier of the fragments to remove
262      */

263     public void removeFragments(String JavaDoc id) {
264         assertIsWriteable();
265         fragments.removeVersions(id);
266     }
267
268     /**
269      * Removes the plug-in with id and version if it exists in this registry.
270      * This method has no effect if a plug-in with the given id and version
271      * cannot be found.
272      *
273      * @param pluginId the unique identifier of the plug-in to remove
274      * @param version the version of the plug-in to remove
275      */

276     public void removePlugin(String JavaDoc pluginId, String JavaDoc version) {
277         assertIsWriteable();
278         plugins.remove(pluginId, version);
279     }
280
281     /**
282      * Removes all versions of the given plug-in from this registry.
283      * This method has no effect if such a plug-in cannot be found.
284      *
285      * @param pluginId the unique identifier of the plug-ins to remove
286      */

287     public void removePlugins(String JavaDoc pluginId) {
288         assertIsWriteable();
289         plugins.removeVersions(pluginId);
290     }
291
292     /**
293      * Runs a resolve through the entire registry. This resolve will
294      * mark any PluginDescriptorModels which do not have access to all
295      * of their prerequisites as disabled. Prerequisites which cause
296      * cyclical dependencies will be marked as disabled.
297      * <p>
298      * If the parameter trimDisabledPlugins is set to true, all PluginDescriptorModels
299      * which are labelled as disabled will be removed from the registry.
300      * </p><p>
301      * If the paramter doCrossLinking is set to true, links will be
302      * created between ExtensionPointModels and their corresponding
303      * ExtensionModels. Not that these links will include disabled
304      * plugins if trimDisabledPlugins was set to false.
305      * </p>
306      * @param trimDisabledPlugins if true, remove all disabled plugins
307      * from the registry (recommended value = true)
308      * @param doCrossLinking if true, link all ExtensionModels in the registry
309      * to their corresponding ExtensionPointModel (recommended value = true).
310      * @return a status object describing the result of resolving.
311      */

312     public IStatus resolve(boolean trimDisabledPlugins, boolean doCrossLinking) {
313         RegistryResolver resolver = new RegistryResolver();
314         resolver.setTrimPlugins(trimDisabledPlugins);
315         resolver.setCrossLink(doCrossLinking);
316         return resolver.resolve(this);
317     }
318
319     /**
320      * Returns a plug-in registry containing all of the plug-ins discovered
321      * on the given plug-in path. Any problems encountered are added to
322      * the status managed by the supplied factory.
323      * <p>
324      * The given plug-in path is the list of locations in which to look for plug-ins.
325      * If an entry identifies a directory (i.e., ends in a '/'), this method
326      * attempts to scan all sub-directories for plug-ins. Alternatively, an
327      * entry may identify a particular plug-in manifest (<code>plugin.xml</code>) file.
328      * </p>
329      * <p>
330      * <b>Note:</b> this method does not affect the running platform. It is intended
331      * for introspecting installed plug-ins on this and other platforms. The returned
332      * registry is <b>not</b> the same as the platform's registry.
333      * </p>
334      * @param pluginPath the list of locations in which to look for plug-ins
335      * @param factory the factory to use to create runtime model objects
336      * @return the registry of parsed plug-ins
337      */

338     public static PluginRegistryModel parsePlugins(URL JavaDoc[] pluginPath, Factory factory) {
339         return InternalPlatform.parsePlugins(pluginPath, factory, false);
340     }
341 }
342
Popular Tags