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       * @param source The XML descriptor source
29       * @param modulesToIgnore A list of module types to ignore
30       * 
31       * @throws com.atlassian.plugin.PluginParseException
32       *          if there is a problem reading the descriptor from the XML {@link java.io.InputStream}.
33       */
34      public OsgiPluginXmlDescriptorParser(InputStream source, String... modulesToIgnore) throws PluginParseException
35      {
36          super(source);
37          Validate.notNull(source, "The descriptor source must not be null");
38          this.modulesToIgnore = new HashSet<String>(Arrays.asList(modulesToIgnore));
39      }
40  
41      /**
42       * Ignores matched modules and passes module descriptor elements back to the {@link OsgiPlugin}
43       * @param plugin The plugin
44       * @param element The module element
45       * @param moduleDescriptorFactory The module descriptor factory
46       * @return The module, or null if the module cannot be found or is being ignored
47       * @throws PluginParseException
48       */
49      @Override
50      protected ModuleDescriptor createModuleDescriptor(Plugin plugin, Element element, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
51      {
52          ModuleDescriptor descriptor = null;
53          if (!modulesToIgnore.contains(element.getName()))
54          {
55              descriptor = super.createModuleDescriptor(plugin, element, moduleDescriptorFactory);
56              String key = (descriptor != null ? descriptor.getKey() : element.attributeValue("key"));
57              ((OsgiPlugin)plugin).addModuleDescriptorElement(key, element);
58          }
59          return descriptor;
60      }
61  }