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   /**
10   * Single module descriptor factory for plugins to use when they want to expose just one plugin.  Has the added benefit
11   * of using the available beanfactory to autowire new module descriptors
12   * @since 2.1
13   */
14  public class SingleModuleDescriptorFactory<T extends ModuleDescriptor> implements ModuleDescriptorFactory, BeanFactoryAware
15  {
16      private final String type;
17      private final Class<T> moduleDescriptorClass;
18      private AutowireCapableBeanFactory beanFactory;
19  
20      public SingleModuleDescriptorFactory(String type, Class<T> moduleDescriptorClass)
21      {
22          this.moduleDescriptorClass = moduleDescriptorClass;
23          this.type = type;
24      }
25  
26      public ModuleDescriptor getModuleDescriptor(String type) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException
27      {
28          T result = null;
29          if (this.type.equals(type))
30          {
31              if (beanFactory != null)
32              {
33                  result = (T) beanFactory.createBean(moduleDescriptorClass);
34              }
35              else
36              {
37                  result = moduleDescriptorClass.newInstance();
38              }
39          }
40          return result;
41      }
42  
43      public boolean hasModuleDescriptor(String type)
44      {
45          return (this.type.equals(type));
46      }
47  
48      public Class<? extends ModuleDescriptor> getModuleDescriptorClass(String type)
49      {
50          return (this.type.equals(type) ? moduleDescriptorClass : null);
51      }
52  
53      public void setBeanFactory(BeanFactory beanFactory) throws BeansException
54      {
55          if (beanFactory instanceof AutowireCapableBeanFactory)
56              this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
57      }
58  }