View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.Application;
4   import com.atlassian.plugin.ModuleDescriptor;
5   import com.atlassian.plugin.ModuleDescriptorFactory;
6   import com.atlassian.plugin.Plugin;
7   import com.atlassian.plugin.PluginParseException;
8   import com.atlassian.plugin.parsers.XmlDescriptorParser;
9   
10  import org.apache.commons.lang.Validate;
11  import org.dom4j.Element;
12  
13  import java.io.InputStream;
14  
15  /**
16   * Descriptor parser that handles special tasks for osgi plugins such as recording the
17   * originating module descriptor elements.  Must only be used with {@link OsgiPlugin} instances.
18   *
19   * @since 2.1.2
20   */
21  public class OsgiPluginXmlDescriptorParser extends XmlDescriptorParser
22  {
23      /**
24       * @param source          The XML descriptor source
25       * @param applications The application keys to limit modules to, null for only unspecified
26       * @throws com.atlassian.plugin.PluginParseException
27       *          if there is a problem reading the descriptor from the XML {@link java.io.InputStream}.
28       */
29      public OsgiPluginXmlDescriptorParser(final InputStream source, final Application... applications) throws PluginParseException
30      {
31          super(source, applications);
32          Validate.notNull(source, "The descriptor source must not be null");
33      }
34  
35      /**
36       * Passes module descriptor elements back to the {@link OsgiPlugin}
37       *
38       * @param plugin                  The plugin
39       * @param element                 The module element
40       * @param moduleDescriptorFactory The module descriptor factory
41       * @return The module, or null if the module cannot be found
42       * @throws PluginParseException
43       */
44      @Override
45      protected ModuleDescriptor<?> createModuleDescriptor(final Plugin plugin, final Element element, final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
46      {
47          final ModuleDescriptor<?> descriptor = super.createModuleDescriptor(plugin, element, moduleDescriptorFactory);
48          final String key = (descriptor != null ? descriptor.getKey() : element.attributeValue("key"));
49          ((OsgiPlugin) plugin).addModuleDescriptorElement(key, element);
50          return descriptor;
51      }
52  }