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       * Creates a DescriptorParser that can read multiple XML files
37       *
38       * @param source The descriptor stream
39       * @param supplementalSources  streams that contain additional module descriptors
40       * @param applications the application key to filter modules with, null for all unspecified
41       * @throws PluginParseException if there is a problem reading the descriptor from the XML {@link InputStream}.
42       * @since 3.2.16
43       */
44      public OsgiPluginXmlDescriptorParser(final InputStream source, final Iterable<InputStream> supplementalSources,  final Set<Application> applications) throws PluginParseException
45      {
46          super(source, supplementalSources, applications);
47      }
48  
49  
50      /**
51       * Passes module descriptor elements back to the {@link OsgiPlugin}
52       *
53       * @param plugin                  The plugin
54       * @param element                 The module element
55       * @param moduleDescriptorFactory The module descriptor factory
56       * @return The module, or null if the module cannot be found
57       * @throws PluginParseException
58       */
59      @Override
60      protected ModuleDescriptor<?> createModuleDescriptor(final Plugin plugin, final Element element, final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
61      {
62          final ModuleDescriptor<?> descriptor = super.createModuleDescriptor(plugin, element, moduleDescriptorFactory);
63          final String key = (descriptor != null ? descriptor.getKey() : element.attributeValue("key"));
64          ((OsgiPlugin) plugin).addModuleDescriptorElement(key, element);
65          return descriptor;
66      }
67  }