View Javadoc

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