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   
9   import org.springframework.beans.factory.BeanFactoryAware;
10  import org.springframework.beans.factory.BeanFactory;
11  import org.springframework.beans.BeansException;
12  
13  /**
14   * Host component provider that uses <code>plugin:available="true"</code> attributes in Spring XML bean configuration
15   * elements to determine which host components to provide to plugins.
16   */
17  public class SpringXmlHostComponentProvider implements HostComponentProvider, BeanFactoryAware
18  {
19      private BeanFactory beanFactory;
20      private List<String> beans;
21  
22      public void setRegistrations(List<String> names)
23      {
24          this.beans = names;
25      }
26      public void provide(ComponentRegistrar registrar)
27      {
28          for (String name : beans)
29          {
30              Object bean = beanFactory.getBean(name);
31              registrar.register(findInterfaces(bean.getClass())).forInstance(bean)
32                      .withName(name);
33          }
34      }
35  
36      public void setBeanFactory(BeanFactory beanFactory) throws BeansException
37      {
38          this.beanFactory = beanFactory;
39      }
40  
41      Class[] findInterfaces(Class cls)
42      {
43          List<Class> validInterfaces = new ArrayList<Class>();
44          Class[] allInterfaces = cls.getInterfaces();
45          for (Class inf : allInterfaces)
46          {
47              if (!inf.getName().startsWith("org.springframework") && !inf.getName().startsWith("java."))
48                  validInterfaces.add(inf);
49          }
50          return validInterfaces.toArray(new Class[0]);
51      }
52  }