View Javadoc
1   package com.atlassian.plugin.osgi.external;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.PluginParseException;
5   import com.atlassian.plugin.hostcontainer.HostContainer;
6   import com.google.common.collect.ImmutableSet;
7   
8   import java.util.Set;
9   
10  /**
11   * A single module descriptor factory for plugins to use when they want to expose just one plugin. Uses
12   * {@link HostContainer} to optionally provide autowiring for new descriptor instances.
13   *
14   * @since 2.1
15   */
16  public class SingleModuleDescriptorFactory<T extends ModuleDescriptor> implements ListableModuleDescriptorFactory {
17      private final String type;
18      private final Class<T> moduleDescriptorClass;
19      private final HostContainer hostContainer;
20  
21      /**
22       * Constructs an instance using a specific host container
23       *
24       * @param hostContainer         The host container to use to create descriptor instances
25       * @param type                  The type of module
26       * @param moduleDescriptorClass The descriptor class
27       * @since 2.2.0
28       */
29      public SingleModuleDescriptorFactory(final HostContainer hostContainer, final String type, final Class<T> moduleDescriptorClass) {
30          this.moduleDescriptorClass = moduleDescriptorClass;
31          this.type = type;
32          this.hostContainer = hostContainer;
33      }
34  
35      @Override
36      public ModuleDescriptor getModuleDescriptor(final String type) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException {
37          T result = null;
38          if (this.type.equals(type)) {
39              result = hostContainer.create(moduleDescriptorClass);
40          }
41          return result;
42      }
43  
44      @Override
45      public boolean hasModuleDescriptor(final String type) {
46          return (this.type.equals(type));
47      }
48  
49      @Override
50      public Class<? extends ModuleDescriptor> getModuleDescriptorClass(final String type) {
51          return (this.type.equals(type) ? moduleDescriptorClass : null);
52      }
53  
54      /**
55       * @since 3.0.0
56       */
57      @Override
58      public Iterable<String> getModuleDescriptorKeys() {
59          return ImmutableSet.of(type);
60      }
61  
62      @Override
63      public Set<Class<? extends ModuleDescriptor>> getModuleDescriptorClasses() {
64          return ImmutableSet.of(moduleDescriptorClass);
65      }
66  
67      public HostContainer getHostContainer() {
68          return hostContainer;
69      }
70  }