File: PluginDescriptorImpl.java

package info (click to toggle)
libjpf-java 1.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,280 kB
  • sloc: java: 13,449; xml: 337; makefile: 17
file content (419 lines) | stat: -rw-r--r-- 16,339 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.registry.xml;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.java.plugin.registry.Documentation;
import org.java.plugin.registry.Extension;
import org.java.plugin.registry.ExtensionPoint;
import org.java.plugin.registry.Identity;
import org.java.plugin.registry.Library;
import org.java.plugin.registry.ManifestProcessingException;
import org.java.plugin.registry.PluginAttribute;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginFragment;
import org.java.plugin.registry.PluginPrerequisite;
import org.java.plugin.registry.PluginRegistry;
import org.java.plugin.registry.Version;

/**
 * @version $Id: PluginDescriptorImpl.java,v 1.4 2007/03/03 17:16:25 ddimon Exp $
 */
class PluginDescriptorImpl extends IdentityImpl implements PluginDescriptor {
    private final PluginRegistry registry;
    private final ModelPluginDescriptor model;
    private Map<String, PluginPrerequisite> pluginPrerequisites;
    private Map<String, Library> libraries;
    private Map<String, ExtensionPoint> extensionPoints;
    private Map<String, Extension> extensions;
    private Documentation<PluginDescriptor> doc;
    private List<PluginFragment> fragments;
    private List<PluginAttribute> attributes;

    PluginDescriptorImpl(final PluginRegistry aRegistry,
            final ModelPluginDescriptor aModel)
            throws ManifestProcessingException {
        super(aModel.getId());
        registry = aRegistry;
        model = aModel;
        if (model.getVendor() == null) {
            model.setVendor(""); //$NON-NLS-1$
        }
        if ((model.getClassName() != null)
                && (model.getClassName().trim().length() == 0)) {
            model.setClassName(null);
        }
        if ((model.getDocsPath() == null)
                || (model.getDocsPath().trim().length() == 0)) {
            model.setDocsPath("docs"); //$NON-NLS-1$
        }
        if (model.getDocumentation() != null) {
            doc = new DocumentationImpl<PluginDescriptor>(this,
                    model.getDocumentation());
        }
        
        attributes = new LinkedList<PluginAttribute>();
        fragments = new LinkedList<PluginFragment>();
        pluginPrerequisites = new HashMap<String, PluginPrerequisite>();
        libraries = new HashMap<String, Library>();
        extensionPoints = new HashMap<String, ExtensionPoint>();
        extensions = new HashMap<String, Extension>();
        
        processAttributes(null, model);
        processPrerequisites(null, model);
        processLibraries(null, model);
        processExtensionPoints(null, model);
        processExtensions(null, model);
        
        if (log.isDebugEnabled()) {
            log.debug("object instantiated: " + this); //$NON-NLS-1$
        }
    }
    
    void registerFragment(final PluginFragmentImpl fragment)
            throws ManifestProcessingException {
        fragments.add(fragment);
        processAttributes(fragment, fragment.getModel());
        processPrerequisites(fragment, fragment.getModel());
        processLibraries(fragment, fragment.getModel());
        processExtensionPoints(fragment, fragment.getModel());
        processExtensions(fragment, fragment.getModel());
    }
    
    void unregisterFragment(final PluginFragmentImpl fragment) {
        // removing attributes
        for (Iterator<PluginAttribute> it = attributes.iterator(); it.hasNext();) {
            if (fragment.equals(it.next()
                    .getDeclaringPluginFragment())) {
                it.remove();
            }
        }
        // removing prerequisites
        for (Iterator<Entry<String, PluginPrerequisite>> it =
            pluginPrerequisites.entrySet().iterator(); it.hasNext();) {
            Entry<String, PluginPrerequisite> entry = it.next();
            if (fragment.equals(entry.getValue()
                    .getDeclaringPluginFragment())) {
                it.remove();
            }
        }
        // removing libraries
        for (Iterator<Entry<String, Library>> it = libraries.entrySet().iterator();
                it.hasNext();) {
            Entry<String, Library> entry = it.next();
            if (fragment.equals(entry.getValue()
                    .getDeclaringPluginFragment())) {
                it.remove();
            }
        }
        // removing extension points
        for (Iterator<Entry<String, ExtensionPoint>> it = extensionPoints.entrySet().iterator();
                it.hasNext();) {
            Entry<String, ExtensionPoint> entry = it.next();
            if (fragment.equals(entry.getValue()
                    .getDeclaringPluginFragment())) {
                it.remove();
            }
        }
        // removing extensions
        for (Iterator<Entry<String, Extension>> it = extensions.entrySet().iterator();
                it.hasNext();) {
            Entry<String, Extension> entry = it.next();
            if (fragment.equals(entry.getValue()
                    .getDeclaringPluginFragment())) {
                it.remove();
            }
        }
        fragments.remove(fragment);
    }
    
    private void processAttributes(final PluginFragmentImpl fragment,
            final ModelPluginManifest modelManifest)
            throws ManifestProcessingException {
        for (ModelAttribute modelAttribute : modelManifest.getAttributes()) {
            attributes.add(new PluginAttributeImpl(this, fragment, modelAttribute, null));
        }
    }
    
    private void processPrerequisites(final PluginFragmentImpl fragment,
            final ModelPluginManifest modelManifest)
            throws ManifestProcessingException {
        for (ModelPrerequisite modelPrerequisite : modelManifest.getPrerequisites()) {
            PluginPrerequisiteImpl pluginPrerequisite =
                new PluginPrerequisiteImpl(this, fragment, modelPrerequisite);
            if (pluginPrerequisites.containsKey(
                    pluginPrerequisite.getPluginId())) {
                throw new ManifestProcessingException(
                        PluginRegistryImpl.PACKAGE_NAME,
                        "duplicateImports", new Object[] { //$NON-NLS-1$
                            pluginPrerequisite.getPluginId(), getId()});
            }
            pluginPrerequisites.put(pluginPrerequisite.getPluginId(),
                pluginPrerequisite);
        }
    }
    
    private void processLibraries(final PluginFragmentImpl fragment,
            final ModelPluginManifest modelManifest)
            throws ManifestProcessingException {
        for (ModelLibrary modelLibrary : modelManifest.getLibraries()) {
            LibraryImpl lib = new LibraryImpl(this, fragment, modelLibrary);
            if (libraries.containsKey(lib.getId())) {
                throw new ManifestProcessingException(
                        PluginRegistryImpl.PACKAGE_NAME,
                        "duplicateLibraries", new Object[] { //$NON-NLS-1$
                            lib.getId(), getId()});
            }
            libraries.put(lib.getId(), lib);
        }
    }
    
    private void processExtensionPoints(final PluginFragmentImpl fragment,
            final ModelPluginManifest modelManifest)
            throws ManifestProcessingException {
        for (ModelExtensionPoint modelExtensionPoint : modelManifest.getExtensionPoints()) {
            ExtensionPointImpl extensionPoint =
                new ExtensionPointImpl(this, fragment, modelExtensionPoint);
            if (extensionPoints.containsKey(extensionPoint.getId())) {
                throw new ManifestProcessingException(
                        PluginRegistryImpl.PACKAGE_NAME,
                        "duplicateExtensionPoints", new Object[] { //$NON-NLS-1$
                            extensionPoint.getId(), getId()});
            }
            extensionPoints.put(extensionPoint.getId(), extensionPoint);
        }
    }
    
    private void processExtensions(final PluginFragmentImpl fragment,
            final ModelPluginManifest modelManifest)
            throws ManifestProcessingException {
        for (ModelExtension modelExtension : modelManifest.getExtensions()) {
            ExtensionImpl extension = new ExtensionImpl(this, fragment, modelExtension);
            if (extensions.containsKey(extension.getId())) {
                throw new ManifestProcessingException(
                        PluginRegistryImpl.PACKAGE_NAME,
                        "duplicateExtensions", new Object[] { //$NON-NLS-1$
                            extension.getId(), getId()});
            }
            if (!getId().equals(extension.getExtendedPluginId())
                    && !pluginPrerequisites.containsKey(
                            extension.getExtendedPluginId())) {
                throw new ManifestProcessingException(
                        PluginRegistryImpl.PACKAGE_NAME,
                        "pluginNotDeclaredInPrerequisites", new Object[] { //$NON-NLS-1$
                            extension.getExtendedPluginId(), extension.getId(),
                            getId()});
            }
            extensions.put(extension.getId(), extension);
        }
        //extensions = Collections.unmodifiableMap(extensions);
    }

    /**
     * @see org.java.plugin.registry.UniqueIdentity#getUniqueId()
     */
    public String getUniqueId() {
        return registry.makeUniqueId(getId(), model.getVersion());
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getVendor()
     */
    public String getVendor() {
        return model.getVendor();
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getVersion()
     */
    public Version getVersion() {
        return model.getVersion();
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getPrerequisites()
     */
    public Collection<PluginPrerequisite> getPrerequisites() {
        return Collections.unmodifiableCollection(pluginPrerequisites.values());
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getPrerequisite(java.lang.String)
     */
    public PluginPrerequisite getPrerequisite(final String id) {
        return pluginPrerequisites.get(id);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getExtensionPoints()
     */
    public Collection<ExtensionPoint> getExtensionPoints() {
        return Collections.unmodifiableCollection(extensionPoints.values());
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getExtensionPoint(java.lang.String)
     */
    public ExtensionPoint getExtensionPoint(final String id) {
        return extensionPoints.get(id);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getExtensions()
     */
    public Collection<Extension> getExtensions() {
        return Collections.unmodifiableCollection(extensions.values());
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getExtension(java.lang.String)
     */
    public Extension getExtension(final String id) {
        return extensions.get(id);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getLibraries()
     */
    public Collection<Library> getLibraries() {
        return Collections.unmodifiableCollection(libraries.values());
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getLibrary(java.lang.String)
     */
    public Library getLibrary(final String id) {
        return libraries.get(id);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getRegistry()
     */
    public PluginRegistry getRegistry() {
        return registry;
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getPluginClassName()
     */
    public String getPluginClassName() {
        return model.getClassName();
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "{PluginDescriptor: uid=" + getUniqueId() + "}"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * @see org.java.plugin.registry.Documentable#getDocumentation()
     */
    public Documentation<PluginDescriptor> getDocumentation() {
        return doc;
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getFragments()
     */
    public Collection<PluginFragment> getFragments() {
        return Collections.unmodifiableCollection(fragments);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getAttribute(java.lang.String)
     */
    public PluginAttribute getAttribute(final String id) {
        PluginAttributeImpl result = null;
        for (PluginAttribute attribute : attributes) {
            PluginAttributeImpl attr = (PluginAttributeImpl) attribute;
            if (attr.getId().equals(id)) {
                if (result == null) {
                    result = attr;
                } else {
                    throw new IllegalArgumentException(
                        "more than one attribute with ID " + id //$NON-NLS-1$
                        + " defined in plug-in " + getUniqueId()); //$NON-NLS-1$
                }
            }
        }
        return result;
    }
    
    /**
     * @see org.java.plugin.registry.PluginDescriptor#getAttributes()
     */
    public Collection<PluginAttribute> getAttributes() {
        return Collections.unmodifiableCollection(attributes);
    }
    
    /**
     * @see org.java.plugin.registry.PluginDescriptor#getAttributes(java.lang.String)
     */
    public Collection<PluginAttribute> getAttributes(final String id) {
        List<PluginAttribute> result = new LinkedList<PluginAttribute>();
        for (PluginAttribute attribute : attributes) {
            PluginAttributeImpl param = (PluginAttributeImpl) attribute;
            if (param.getId().equals(id)) {
                result.add(param);
            }
        }
        return Collections.unmodifiableList(result);
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getDocsPath()
     */
    public String getDocsPath() {
        return model.getDocsPath();
    }

    /**
     * @see org.java.plugin.registry.PluginDescriptor#getLocation()
     */
    public URL getLocation() {
        return model.getLocation();
    }

    /**
     * @see org.java.plugin.registry.xml.IdentityImpl#isEqualTo(
     *      org.java.plugin.registry.Identity)
     */
    @Override
    protected boolean isEqualTo(final Identity idt) {
        if (!(idt instanceof PluginDescriptorImpl)) {
            return false;
        }
        PluginDescriptorImpl other = (PluginDescriptorImpl) idt;
        return getUniqueId().equals(other.getUniqueId())
            && getLocation().toExternalForm().equals(
                    other.getLocation().toExternalForm());
    }
}