View Javadoc

1   package com.atlassian.plugin.osgi.external;
2   
3   import com.atlassian.plugin.*;
4   import org.springframework.beans.factory.BeanFactoryAware;
5   import org.springframework.beans.factory.BeanFactory;
6   import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
7   import org.springframework.beans.BeansException;
8   
9   import java.util.Set;
10  import java.util.Collections;
11  
12  /**
13   * Single module descriptor factory for plugins to use when they want to expose just one plugin. Does not support
14   * autowiring module descriptors. 
15   *
16   * @since 2.1
17   */
18  public class SingleModuleDescriptorFactory<T extends ModuleDescriptor> implements ListableModuleDescriptorFactory
19  {
20      private final String type;
21      private final Class<T> moduleDescriptorClass;
22  
23      public SingleModuleDescriptorFactory(String type, Class<T> moduleDescriptorClass)
24      {
25          this.moduleDescriptorClass = moduleDescriptorClass;
26          this.type = type;
27      }
28  
29      public ModuleDescriptor getModuleDescriptor(String type) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException
30      {
31          T result = null;
32          if (this.type.equals(type))
33          {
34              // We can't use an autowired bean factory to create the instance because it would be loaded by this class's
35              // classloader, which will not have access to the spring instance in bundle space.
36              result = moduleDescriptorClass.newInstance();
37          }
38          return result;
39      }
40  
41      public boolean hasModuleDescriptor(String type)
42      {
43          return (this.type.equals(type));
44      }
45  
46      public Class<? extends ModuleDescriptor> getModuleDescriptorClass(String type)
47      {
48          return (this.type.equals(type) ? moduleDescriptorClass : null);
49      }
50  
51      public Set<Class<ModuleDescriptor<?>>> getModuleDescriptorClasses()
52      {
53          return Collections.singleton((Class<ModuleDescriptor<?>>)moduleDescriptorClass);
54      }
55  }