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   import org.dom4j.Element;
10  
11  import java.io.InputStream;
12  import java.util.Set;
13  
14  import static com.google.common.base.Preconditions.checkNotNull;
15  
16  /**
17   * Descriptor parser that handles special tasks for osgi plugins such as recording the
18   * originating module descriptor elements.  Must only be used with {@link OsgiPlugin} instances.
19   *
20   * @since 2.1.2
21   */
22  public class OsgiPluginXmlDescriptorParser extends XmlDescriptorParser
23  {
24      /**
25       * @param source          The XML descriptor source
26       * @param applications The application keys to limit modules to, null for only unspecified
27       * @throws com.atlassian.plugin.PluginParseException
28       *          if there is a problem reading the descriptor from the XML {@link java.io.InputStream}.
29       */
30      public OsgiPluginXmlDescriptorParser(final InputStream source, final Set<Application> applications) throws PluginParseException
31      {
32          super(checkNotNull(source, "The descriptor source must not be null"), applications);
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  }