1 package com.atlassian.plugin.spring;
2
3 import com.atlassian.plugin.osgi.hostcomponents.ComponentRegistrar;
4 import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
5 import org.springframework.beans.BeansException;
6 import org.springframework.beans.factory.BeanFactory;
7 import org.springframework.beans.factory.BeanFactoryAware;
8 import org.springframework.beans.factory.BeanIsAbstractException;
9 import org.springframework.beans.factory.ListableBeanFactory;
10 import org.springframework.stereotype.Component;
11 import org.springframework.aop.support.AopUtils;
12 import org.springframework.core.annotation.AnnotationUtils;
13 import org.apache.commons.lang.ClassUtils;
14
15
16
17
18
19 @Component("hostComponentProvider")
20 public class SpringHostComponentProvider implements HostComponentProvider, BeanFactoryAware
21 {
22 private BeanFactory beanFactory = null;
23
24 public void setBeanFactory(BeanFactory beanFactory) throws BeansException
25 {
26 this.beanFactory = beanFactory;
27 }
28
29 public void provide(ComponentRegistrar registrar)
30 {
31 ListableBeanFactory lbf;
32 if (beanFactory instanceof ListableBeanFactory) {
33 lbf = (ListableBeanFactory) beanFactory;
34 String[] names = lbf.getBeanDefinitionNames();
35
36 for (String name : names)
37 {
38 try
39 {
40 if (lbf.isSingleton(name))
41 {
42
43 Object bean = lbf.getBean(name);
44 Class cls = AopUtils.getTargetClass(bean);
45
46 AvailableToPlugins annotation = AnnotationUtils.findAnnotation(cls, AvailableToPlugins.class);
47 Class[] ifs;
48 if (annotation != null) {
49 if (annotation.value() != Void.class) {
50 ifs = new Class[] { annotation.value() };
51 } else {
52 ifs = (Class[]) ClassUtils.getAllInterfaces(bean.getClass()).toArray(new Class[0]);
53 }
54 registrar.register(ifs).forInstance(bean).withName(name).withContextClassLoaderStrategy(annotation.contextClassLoaderStrategy());
55 }
56 }
57 } catch (BeanIsAbstractException ex)
58 {
59
60 }
61 }
62 }
63 }
64 }