View Javadoc

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