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.atlassian.plugin.hostcontainer.DefaultHostContainer;
7   
8   import java.util.Collections;
9   import java.util.Set;
10  
11  /**
12   * A single module descriptor factory for plugins to use when they want to expose just one plugin.  Uses
13   * {@link HostContainer} to optionally provide autowiring for new desciptor instances.
14   *
15   * @since 2.1
16   */
17  public class SingleModuleDescriptorFactory<T extends ModuleDescriptor<?>> implements ListableModuleDescriptorFactory
18  {
19      private final String type;
20      private final Class<T> moduleDescriptorClass;
21      private final HostContainer hostContainer;
22  
23      public SingleModuleDescriptorFactory(final String type, final Class<T> moduleDescriptorClass)
24      {
25          this(new DefaultHostContainer(), type, moduleDescriptorClass);
26      }
27  
28      /**
29       * Constructs an instance using a specific host container
30       * @param hostContainer The host container to use to create descriptor instances
31       * @param type The type of module
32       * @param moduleDescriptorClass The descriptor class
33       * @since 2.2.0
34       */
35      public SingleModuleDescriptorFactory(final HostContainer hostContainer, final String type, final Class<T> moduleDescriptorClass)
36      {
37          this.moduleDescriptorClass = moduleDescriptorClass;
38          this.type = type;
39          this.hostContainer = hostContainer;
40      }
41  
42      public ModuleDescriptor getModuleDescriptor(final String type) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException
43      {
44          T result = null;
45          if (this.type.equals(type))
46          {
47              // We can't use an autowired bean factory to create the instance because it would be loaded by this class's
48              // classloader, which will not have access to the spring instance in bundle space.
49              result = hostContainer.create(moduleDescriptorClass);
50          }
51          return result;
52      }
53  
54      public boolean hasModuleDescriptor(final String type)
55      {
56          return (this.type.equals(type));
57      }
58  
59      @SuppressWarnings("unchecked")
60      public Class<? extends ModuleDescriptor<?>> getModuleDescriptorClass(final String type)
61      {
62          return (this.type.equals(type) ? moduleDescriptorClass : null);
63      }
64  
65      @SuppressWarnings("unchecked")
66      public Set<Class<ModuleDescriptor<?>>> getModuleDescriptorClasses()
67      {
68          return Collections.singleton((Class<ModuleDescriptor<?>>) moduleDescriptorClass);
69      }
70  }