KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > xml > PluginDescriptorImpl


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2007 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.registry.xml;
20
21 import java.net.URL JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.java.plugin.registry.Documentation;
31 import org.java.plugin.registry.Extension;
32 import org.java.plugin.registry.ExtensionPoint;
33 import org.java.plugin.registry.Identity;
34 import org.java.plugin.registry.Library;
35 import org.java.plugin.registry.ManifestProcessingException;
36 import org.java.plugin.registry.PluginAttribute;
37 import org.java.plugin.registry.PluginDescriptor;
38 import org.java.plugin.registry.PluginPrerequisite;
39 import org.java.plugin.registry.PluginRegistry;
40 import org.java.plugin.registry.Version;
41
42 /**
43  * @version $Id: PluginDescriptorImpl.java,v 1.6 2007/01/05 13:32:08 ddimon Exp $
44  */

45 class PluginDescriptorImpl extends IdentityImpl implements PluginDescriptor {
46     private final PluginRegistry registry;
47     private final URL JavaDoc location;
48     private final ModelPluginDescriptor model;
49     private Map JavaDoc pluginPrerequisites;
50     private Map JavaDoc libraries;
51     private Map JavaDoc extensionPoints;
52     private Map JavaDoc extensions;
53     private Documentation doc;
54     private List JavaDoc fragments;
55     private List JavaDoc attributes;
56
57     PluginDescriptorImpl(final PluginRegistry aRegistry,
58             final ModelPluginDescriptor aModel, final URL JavaDoc aLocation)
59             throws ManifestProcessingException {
60         super(aModel.getId());
61         registry = aRegistry;
62         model = aModel;
63         location = aLocation;
64         if (model.getVendor() == null) {
65             model.setVendor(""); //$NON-NLS-1$
66
}
67         if ((model.getClassName() != null)
68                 && (model.getClassName().trim().length() == 0)) {
69             model.setClassName(null);
70         }
71         if ((model.getDocsPath() == null)
72                 || (model.getDocsPath().trim().length() == 0)) {
73             model.setDocsPath("docs"); //$NON-NLS-1$
74
}
75         if (model.getDocumentation() != null) {
76             doc = new DocumentationImpl(this, model.getDocumentation());
77         }
78         
79         attributes = new LinkedList JavaDoc();
80         fragments = new LinkedList JavaDoc();
81         pluginPrerequisites = new HashMap JavaDoc();
82         libraries = new HashMap JavaDoc();
83         extensionPoints = new HashMap JavaDoc();
84         extensions = new HashMap JavaDoc();
85         
86         processAttributes(null, model);
87         processPrerequisites(null, model);
88         processLibraries(null, model);
89         processExtensionPoints(null, model);
90         processExtensions(null, model);
91         
92         if (log.isDebugEnabled()) {
93             log.debug("object instantiated: " + this); //$NON-NLS-1$
94
}
95     }
96     
97     void registerFragment(final PluginFragmentImpl fragment)
98             throws ManifestProcessingException {
99         fragments.add(fragment);
100         processAttributes(fragment, fragment.getModel());
101         processPrerequisites(fragment, fragment.getModel());
102         processLibraries(fragment, fragment.getModel());
103         processExtensionPoints(fragment, fragment.getModel());
104         processExtensions(fragment, fragment.getModel());
105     }
106     
107     void unregisterFragment(final PluginFragmentImpl fragment) {
108         // removing attributes
109
for (Iterator JavaDoc it = attributes.iterator(); it.hasNext();) {
110             if (fragment.equals(((PluginAttribute) it.next())
111                     .getDeclaringPluginFragment())) {
112                 it.remove();
113             }
114         }
115         // removing prerequisites
116
for (Iterator JavaDoc it = pluginPrerequisites.entrySet().iterator();
117                 it.hasNext();) {
118             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
119             if (fragment.equals(((PluginPrerequisite) entry.getValue())
120                     .getDeclaringPluginFragment())) {
121                 it.remove();
122             }
123         }
124         // removing libraries
125
for (Iterator JavaDoc it = libraries.entrySet().iterator();
126                 it.hasNext();) {
127             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
128             if (fragment.equals(((Library) entry.getValue())
129                     .getDeclaringPluginFragment())) {
130                 it.remove();
131             }
132         }
133         // removing extension points
134
for (Iterator JavaDoc it = extensionPoints.entrySet().iterator();
135                 it.hasNext();) {
136             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
137             if (fragment.equals(((ExtensionPoint) entry.getValue())
138                     .getDeclaringPluginFragment())) {
139                 it.remove();
140             }
141         }
142         // removing extensions
143
for (Iterator JavaDoc it = extensions.entrySet().iterator();
144                 it.hasNext();) {
145             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
146             if (fragment.equals(((Extension) entry.getValue())
147                     .getDeclaringPluginFragment())) {
148                 it.remove();
149             }
150         }
151         fragments.remove(fragment);
152     }
153     
154     private void processAttributes(final PluginFragmentImpl fragment,
155             final ModelPluginManifest modelManifest)
156             throws ManifestProcessingException {
157         for (Iterator JavaDoc it = modelManifest.getAttributes().iterator();
158                 it.hasNext();) {
159             attributes.add(new PluginAttributeImpl(this, fragment,
160                     (ModelAttribute) it.next(), null));
161         }
162     }
163     
164     private void processPrerequisites(final PluginFragmentImpl fragment,
165             final ModelPluginManifest modelManifest)
166             throws ManifestProcessingException {
167         for (Iterator JavaDoc it = modelManifest.getPrerequisites().iterator();
168                 it.hasNext();) {
169             PluginPrerequisiteImpl pluginPrerequisite =
170                 new PluginPrerequisiteImpl(this, fragment,
171                         (ModelPrerequisite) it.next());
172             if (pluginPrerequisites.containsKey(
173                     pluginPrerequisite.getPluginId())) {
174                 throw new ManifestProcessingException(
175                         PluginRegistryImpl.PACKAGE_NAME,
176                         "duplicateImports", new Object JavaDoc[] { //$NON-NLS-1$
177
pluginPrerequisite.getPluginId(), getId()});
178             }
179             pluginPrerequisites.put(pluginPrerequisite.getPluginId(),
180                 pluginPrerequisite);
181         }
182     }
183     
184     private void processLibraries(final PluginFragmentImpl fragment,
185             final ModelPluginManifest modelManifest)
186             throws ManifestProcessingException {
187         for (Iterator JavaDoc it = modelManifest.getLibraries().iterator();
188                 it.hasNext();) {
189             LibraryImpl lib = new LibraryImpl(this, fragment,
190                     (ModelLibrary) it.next());
191             if (libraries.containsKey(lib.getId())) {
192                 throw new ManifestProcessingException(
193                         PluginRegistryImpl.PACKAGE_NAME,
194                         "duplicateLibraries", new Object JavaDoc[] { //$NON-NLS-1$
195
lib.getId(), getId()});
196             }
197             libraries.put(lib.getId(), lib);
198         }
199     }
200     
201     private void processExtensionPoints(final PluginFragmentImpl fragment,
202             final ModelPluginManifest modelManifest)
203             throws ManifestProcessingException {
204         for (Iterator JavaDoc it = modelManifest.getExtensionPoints().iterator();
205                 it.hasNext();) {
206             ExtensionPointImpl extensionPoint =
207                 new ExtensionPointImpl(this, fragment,
208                         (ModelExtensionPoint) it.next());
209             if (extensionPoints.containsKey(extensionPoint.getId())) {
210                 throw new ManifestProcessingException(
211                         PluginRegistryImpl.PACKAGE_NAME,
212                         "duplicateExtensionPoints", new Object JavaDoc[] { //$NON-NLS-1$
213
extensionPoint.getId(), getId()});
214             }
215             extensionPoints.put(extensionPoint.getId(), extensionPoint);
216         }
217     }
218     
219     private void processExtensions(final PluginFragmentImpl fragment,
220             final ModelPluginManifest modelManifest)
221             throws ManifestProcessingException {
222         for (Iterator JavaDoc it = modelManifest.getExtensions().iterator();
223                 it.hasNext();) {
224             ExtensionImpl extension = new ExtensionImpl(this, fragment,
225                     (ModelExtension) it.next());
226             if (extensions.containsKey(extension.getId())) {
227                 throw new ManifestProcessingException(
228                         PluginRegistryImpl.PACKAGE_NAME,
229                         "duplicateExtensions", new Object JavaDoc[] { //$NON-NLS-1$
230
extension.getId(), getId()});
231             }
232             if (!getId().equals(extension.getExtendedPluginId())
233                     && !pluginPrerequisites.containsKey(
234                             extension.getExtendedPluginId())) {
235                 throw new ManifestProcessingException(
236                         PluginRegistryImpl.PACKAGE_NAME,
237                         "pluginNotDeclaredInPrerequisites", new Object JavaDoc[] { //$NON-NLS-1$
238
extension.getExtendedPluginId(), extension.getId(),
239                             getId()});
240             }
241             extensions.put(extension.getId(), extension);
242         }
243         //extensions = Collections.unmodifiableMap(extensions);
244
}
245
246     /**
247      * @see org.java.plugin.registry.UniqueIdentity#getUniqueId()
248      */

249     public String JavaDoc getUniqueId() {
250         return registry.makeUniqueId(getId(), model.getVersion());
251     }
252
253     /**
254      * @see org.java.plugin.registry.PluginDescriptor#getVendor()
255      */

256     public String JavaDoc getVendor() {
257         return model.getVendor();
258     }
259
260     /**
261      * @see org.java.plugin.registry.PluginDescriptor#getVersion()
262      */

263     public Version getVersion() {
264         return model.getVersion();
265     }
266
267     /**
268      * @see org.java.plugin.registry.PluginDescriptor#getPrerequisites()
269      */

270     public Collection JavaDoc getPrerequisites() {
271         return Collections.unmodifiableCollection(pluginPrerequisites.values());
272     }
273
274     /**
275      * @see org.java.plugin.registry.PluginDescriptor#getPrerequisite(java.lang.String)
276      */

277     public PluginPrerequisite getPrerequisite(String JavaDoc id) {
278         return (PluginPrerequisite) pluginPrerequisites.get(id);
279     }
280
281     /**
282      * @see org.java.plugin.registry.PluginDescriptor#getExtensionPoints()
283      */

284     public Collection JavaDoc getExtensionPoints() {
285         return Collections.unmodifiableCollection(extensionPoints.values());
286     }
287
288     /**
289      * @see org.java.plugin.registry.PluginDescriptor#getExtensionPoint(java.lang.String)
290      */

291     public ExtensionPoint getExtensionPoint(String JavaDoc id) {
292         return (ExtensionPoint) extensionPoints.get(id);
293     }
294
295     /**
296      * @see org.java.plugin.registry.PluginDescriptor#getExtensions()
297      */

298     public Collection JavaDoc getExtensions() {
299         return Collections.unmodifiableCollection(extensions.values());
300     }
301
302     /**
303      * @see org.java.plugin.registry.PluginDescriptor#getExtension(java.lang.String)
304      */

305     public Extension getExtension(String JavaDoc id) {
306         return (Extension) extensions.get(id);
307     }
308
309     /**
310      * @see org.java.plugin.registry.PluginDescriptor#getLibraries()
311      */

312     public Collection JavaDoc getLibraries() {
313         return Collections.unmodifiableCollection(libraries.values());
314     }
315
316     /**
317      * @see org.java.plugin.registry.PluginDescriptor#getLibrary(java.lang.String)
318      */

319     public Library getLibrary(String JavaDoc id) {
320         return (Library) libraries.get(id);
321     }
322
323     /**
324      * @see org.java.plugin.registry.PluginDescriptor#getRegistry()
325      */

326     public PluginRegistry getRegistry() {
327         return registry;
328     }
329
330     /**
331      * @see org.java.plugin.registry.PluginDescriptor#getPluginClassName()
332      */

333     public String JavaDoc getPluginClassName() {
334         return model.getClassName();
335     }
336
337     /**
338      * @see java.lang.Object#toString()
339      */

340     public String JavaDoc toString() {
341         return "{PluginDescriptor: uid=" + getUniqueId() + "}"; //$NON-NLS-1$ //$NON-NLS-2$
342
}
343
344     /**
345      * @see org.java.plugin.registry.Documentable#getDocumentation()
346      */

347     public Documentation getDocumentation() {
348         return doc;
349     }
350
351     /**
352      * @see org.java.plugin.registry.PluginDescriptor#getFragments()
353      */

354     public Collection JavaDoc getFragments() {
355         return Collections.unmodifiableCollection(fragments);
356     }
357
358     /**
359      * @see org.java.plugin.registry.PluginDescriptor#getAttribute(java.lang.String)
360      */

361     public PluginAttribute getAttribute(final String JavaDoc id) {
362         PluginAttributeImpl result = null;
363         for (Iterator JavaDoc it = attributes.iterator(); it.hasNext();) {
364             PluginAttributeImpl attr = (PluginAttributeImpl) it.next();
365             if (attr.getId().equals(id)) {
366                 if (result == null) {
367                     result = attr;
368                 } else {
369                     throw new IllegalArgumentException JavaDoc(
370                         "more than one attribute with ID " + id //$NON-NLS-1$
371
+ " defined in plug-in " + getUniqueId()); //$NON-NLS-1$
372
}
373             }
374         }
375         return result;
376     }
377     
378     /**
379      * @see org.java.plugin.registry.PluginDescriptor#getAttributes()
380      */

381     public Collection JavaDoc getAttributes() {
382         return Collections.unmodifiableCollection(attributes);
383     }
384     
385     /**
386      * @see org.java.plugin.registry.PluginDescriptor#getAttributes(java.lang.String)
387      */

388     public Collection JavaDoc getAttributes(final String JavaDoc id) {
389         List JavaDoc result = new LinkedList JavaDoc();
390         for (Iterator JavaDoc it = attributes.iterator(); it.hasNext();) {
391             PluginAttributeImpl param = (PluginAttributeImpl) it.next();
392             if (param.getId().equals(id)) {
393                 result.add(param);
394             }
395         }
396         return Collections.unmodifiableList(result);
397     }
398
399     /**
400      * @see org.java.plugin.registry.PluginDescriptor#getDocsPath()
401      */

402     public String JavaDoc getDocsPath() {
403         return model.getDocsPath();
404     }
405
406     /**
407      * @see org.java.plugin.registry.PluginDescriptor#getLocation()
408      */

409     public URL JavaDoc getLocation() {
410         return location;
411     }
412
413     /**
414      * @see org.java.plugin.registry.xml.IdentityImpl#isEqualTo(
415      * org.java.plugin.registry.Identity)
416      */

417     protected boolean isEqualTo(final Identity idt) {
418         if (!(idt instanceof PluginDescriptorImpl)) {
419             return false;
420         }
421         PluginDescriptorImpl other = (PluginDescriptorImpl) idt;
422         return getUniqueId().equals(other.getUniqueId())
423             && getLocation().toExternalForm().equals(
424                     other.getLocation().toExternalForm());
425     }
426 }
427
Popular Tags