View Javadoc

1   package com.atlassian.plugin.spring.pluginns;
2   
3   import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
4   import com.atlassian.plugin.osgi.hostcomponents.ComponentRegistrar;
5   
6   import java.util.List;
7   import java.util.ArrayList;
8   import java.util.Map;
9   import java.util.Collections;
10  
11  import org.springframework.beans.factory.BeanFactoryAware;
12  import org.springframework.beans.factory.BeanFactory;
13  import org.springframework.beans.factory.HierarchicalBeanFactory;
14  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
15  import org.springframework.beans.BeansException;
16  
17  /**
18   * Host component provider that uses <code>plugin:available="true"</code> attributes in Spring XML bean configuration
19   * elements to determine which host components to provide to plugins.
20   */
21  public class SpringXmlHostComponentProvider implements HostComponentProvider, BeanFactoryAware
22  {
23      public static final String HOST_COMPONENT_PROVIDER = "hostComponentProvider";
24      private BeanFactory beanFactory;
25      private List<String> beans;
26      private Map<String,Class[]> interfaces = Collections.emptyMap();
27  
28      public void setRegistrations(List<String> names)
29      {
30          this.beans = names;
31      }
32  
33      public void setInterfaces(Map<String,Class[]> interfaces)
34      {
35          this.interfaces = interfaces;
36      }
37  
38      public void provide(ComponentRegistrar registrar)
39      {
40          for (String name : beans)
41          {
42              Object bean = beanFactory.getBean(name);
43              Class[] beanInterfaces = interfaces.get(name);
44              if (beanInterfaces == null)
45              {
46                  beanInterfaces = findInterfaces(bean.getClass());
47              }
48              registrar.register(beanInterfaces).forInstance(bean)
49                      .withName(name);
50          }
51          if (beanFactory instanceof HierarchicalBeanFactory)
52          {
53              HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) beanFactory;
54              if (hbf.getParentBeanFactory() != null)
55              {
56                  HostComponentProvider provider = (HostComponentProvider) hbf.getParentBeanFactory().getBean(HOST_COMPONENT_PROVIDER);
57                  if (provider != null)
58                      provider.provide(registrar);
59              }
60          }
61      }
62  
63      public void setBeanFactory(BeanFactory beanFactory) throws BeansException
64      {
65          this.beanFactory = beanFactory;
66      }
67  
68      Class[] findInterfaces(Class cls)
69      {
70          List<Class> validInterfaces = new ArrayList<Class>();
71          Class[] allInterfaces = cls.getInterfaces();
72          for (Class inf : allInterfaces)
73          {
74              if (!inf.getName().startsWith("org.springframework") && !inf.getName().startsWith("java."))
75                  validInterfaces.add(inf);
76          }
77          return validInterfaces.toArray(new Class[0]);
78      }
79  }