| 1 19 package org.java.plugin.registry.xml; 20 21 import java.net.URL ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 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 45 class PluginDescriptorImpl extends IdentityImpl implements PluginDescriptor { 46 private final PluginRegistry registry; 47 private final URL location; 48 private final ModelPluginDescriptor model; 49 private Map pluginPrerequisites; 50 private Map libraries; 51 private Map extensionPoints; 52 private Map extensions; 53 private Documentation doc; 54 private List fragments; 55 private List attributes; 56 57 PluginDescriptorImpl(final PluginRegistry aRegistry, 58 final ModelPluginDescriptor aModel, final URL 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(""); } 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"); } 75 if (model.getDocumentation() != null) { 76 doc = new DocumentationImpl(this, model.getDocumentation()); 77 } 78 79 attributes = new LinkedList (); 80 fragments = new LinkedList (); 81 pluginPrerequisites = new HashMap (); 82 libraries = new HashMap (); 83 extensionPoints = new HashMap (); 84 extensions = new HashMap (); 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); } 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 for (Iterator it = attributes.iterator(); it.hasNext();) { 110 if (fragment.equals(((PluginAttribute) it.next()) 111 .getDeclaringPluginFragment())) { 112 it.remove(); 113 } 114 } 115 for (Iterator it = pluginPrerequisites.entrySet().iterator(); 117 it.hasNext();) { 118 Map.Entry entry = (Map.Entry ) it.next(); 119 if (fragment.equals(((PluginPrerequisite) entry.getValue()) 120 .getDeclaringPluginFragment())) { 121 it.remove(); 122 } 123 } 124 for (Iterator it = libraries.entrySet().iterator(); 126 it.hasNext();) { 127 Map.Entry entry = (Map.Entry ) it.next(); 128 if (fragment.equals(((Library) entry.getValue()) 129 .getDeclaringPluginFragment())) { 130 it.remove(); 131 } 132 } 133 for (Iterator it = extensionPoints.entrySet().iterator(); 135 it.hasNext();) { 136 Map.Entry entry = (Map.Entry ) it.next(); 137 if (fragment.equals(((ExtensionPoint) entry.getValue()) 138 .getDeclaringPluginFragment())) { 139 it.remove(); 140 } 141 } 142 for (Iterator it = extensions.entrySet().iterator(); 144 it.hasNext();) { 145 Map.Entry entry = (Map.Entry ) 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 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 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 [] { 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 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 [] { 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 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 [] { 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 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 [] { 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 [] { extension.getExtendedPluginId(), extension.getId(), 239 getId()}); 240 } 241 extensions.put(extension.getId(), extension); 242 } 243 } 245 246 249 public String getUniqueId() { 250 return registry.makeUniqueId(getId(), model.getVersion()); 251 } 252 253 256 public String getVendor() { 257 return model.getVendor(); 258 } 259 260 263 public Version getVersion() { 264 return model.getVersion(); 265 } 266 267 270 public Collection getPrerequisites() { 271 return Collections.unmodifiableCollection(pluginPrerequisites.values()); 272 } 273 274 277 public PluginPrerequisite getPrerequisite(String id) { 278 return (PluginPrerequisite) pluginPrerequisites.get(id); 279 } 280 281 284 public Collection getExtensionPoints() { 285 return Collections.unmodifiableCollection(extensionPoints.values()); 286 } 287 288 291 public ExtensionPoint getExtensionPoint(String id) { 292 return (ExtensionPoint) extensionPoints.get(id); 293 } 294 295 298 public Collection getExtensions() { 299 return Collections.unmodifiableCollection(extensions.values()); 300 } 301 302 305 public Extension getExtension(String id) { 306 return (Extension) extensions.get(id); 307 } 308 309 312 public Collection getLibraries() { 313 return Collections.unmodifiableCollection(libraries.values()); 314 } 315 316 319 public Library getLibrary(String id) { 320 return (Library) libraries.get(id); 321 } 322 323 326 public PluginRegistry getRegistry() { 327 return registry; 328 } 329 330 333 public String getPluginClassName() { 334 return model.getClassName(); 335 } 336 337 340 public String toString() { 341 return "{PluginDescriptor: uid=" + getUniqueId() + "}"; } 343 344 347 public Documentation getDocumentation() { 348 return doc; 349 } 350 351 354 public Collection getFragments() { 355 return Collections.unmodifiableCollection(fragments); 356 } 357 358 361 public PluginAttribute getAttribute(final String id) { 362 PluginAttributeImpl result = null; 363 for (Iterator 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 ( 370 "more than one attribute with ID " + id + " defined in plug-in " + getUniqueId()); } 373 } 374 } 375 return result; 376 } 377 378 381 public Collection getAttributes() { 382 return Collections.unmodifiableCollection(attributes); 383 } 384 385 388 public Collection getAttributes(final String id) { 389 List result = new LinkedList (); 390 for (Iterator 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 402 public String getDocsPath() { 403 return model.getDocsPath(); 404 } 405 406 409 public URL getLocation() { 410 return location; 411 } 412 413 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 |