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       * @param source       The XML descriptor source
25       * @param applications The application keys to limit modules to, null for only unspecified
26       * @throws com.atlassian.plugin.PluginParseException if there is a problem reading the descriptor from the XML {@link java.io.InputStream}.
27       */
28      public OsgiPluginXmlDescriptorParser(final InputStream source, final Set<Application> applications) throws PluginParseException {
29          super(checkNotNull(source, "The descriptor source must not be null"), applications);
30      }
31  
32      /**
33       * Creates a DescriptorParser that can read multiple XML files
34       *
35       * @param source              The descriptor stream
36       * @param supplementalSources streams that contain additional module descriptors
37       * @param applications        the application key to filter modules with, null for all unspecified
38       * @throws PluginParseException if there is a problem reading the descriptor from the XML {@link InputStream}.
39       * @since 3.2.16
40       */
41      public OsgiPluginXmlDescriptorParser(final InputStream source, final Iterable<InputStream> supplementalSources,
42                                           final Set<Application> applications) throws PluginParseException {
43          super(source, supplementalSources, applications);
44      }
45  
46  
47      /**
48       * Passes module descriptor elements back to the {@link OsgiPlugin}
49       *
50       * @param plugin                  The plugin
51       * @param element                 The module element
52       * @param moduleDescriptorFactory The module descriptor factory
53       * @return The module, or null if the module cannot be found
54       * @throws PluginParseException
55       */
56      @Override
57      protected ModuleDescriptor<?> createModuleDescriptor(final Plugin plugin, final Element element,
58                                                           final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException {
59          final ModuleDescriptor<?> descriptor = super.createModuleDescriptor(plugin, element, moduleDescriptorFactory);
60  
61          passModuleDescriptorToPlugin(plugin, element, descriptor);
62  
63          return descriptor;
64      }
65  
66      /**
67       * Passes module descriptor elements back to the {@link OsgiPlugin}
68       */
69      @Override
70      public ModuleDescriptor<?> addModule(final ModuleDescriptorFactory moduleDescriptorFactory, final Plugin plugin,
71                                           final Element module) {
72          final ModuleDescriptor<?> descriptor = super.addModule(moduleDescriptorFactory, plugin, module);
73  
74          passModuleDescriptorToPlugin(plugin, module, descriptor);
75  
76          return descriptor;
77      }
78  
79      /**
80       * Passes module descriptor elements back to the {@link OsgiPlugin}
81       */
82      private void passModuleDescriptorToPlugin(final Plugin plugin, final Element element, final ModuleDescriptor<?> descriptor) {
83          if (plugin instanceof OsgiPlugin) {
84              final String key;
85              if (descriptor == null) {
86                  key = element.attributeValue("key");
87              } else {
88                  key = descriptor.getKey();
89              }
90  
91              ((OsgiPlugin) plugin).addModuleDescriptorElement(key, element);
92          }
93      }
94  }