View Javadoc

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