1 package org
.argeo
.slc
.osgi
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.HashSet
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.argeo
.slc
.SlcException
;
12 import org
.springframework
.beans
.BeansException
;
13 import org
.springframework
.beans
.MutablePropertyValues
;
14 import org
.springframework
.beans
.factory
.config
.BeanFactoryPostProcessor
;
15 import org
.springframework
.beans
.factory
.config
.ConfigurableListableBeanFactory
;
16 import org
.springframework
.beans
.factory
.support
.BeanDefinitionRegistry
;
17 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
18 import org
.springframework
.core
.Ordered
;
19 import org
.springframework
.osgi
.service
.exporter
.support
.ExportContextClassLoader
;
20 import org
.springframework
.osgi
.service
.exporter
.support
.OsgiServiceFactoryBean
;
22 @SuppressWarnings(value
= { "unchecked" })
23 public class MultipleServiceExporterPostProcessor
implements
24 BeanFactoryPostProcessor
, Ordered
{
25 private final static Log log
= LogFactory
26 .getLog(MultipleServiceExporterPostProcessor
.class);
28 private List
<Class
> interfaces
= new ArrayList
<Class
>();
30 private Class osgiServiceFactoryClass
= OsgiServiceFactoryBean
.class;
32 private Boolean useServiceProviderContextClassLoader
= false;
34 private int order
= Ordered
.LOWEST_PRECEDENCE
;
36 public void postProcessBeanFactory(
37 ConfigurableListableBeanFactory beanFactory
) throws BeansException
{
38 if (!(beanFactory
instanceof BeanDefinitionRegistry
)) {
39 throw new SlcException("Can only work on "
40 + BeanDefinitionRegistry
.class);
43 long begin
= System
.currentTimeMillis();
45 // Merge all beans implementing these interfaces
46 Set
<String
> beanNames
= new HashSet
<String
>();
47 for (Class clss
: interfaces
) {
48 String
[] strs
= beanFactory
.getBeanNamesForType(clss
, true, false);
49 beanNames
.addAll(Arrays
.asList(strs
));
52 // Register service factory beans for them
53 for (String beanName
: beanNames
) {
54 MutablePropertyValues mpv
= new MutablePropertyValues();
55 mpv
.addPropertyValue("interfaces", interfaces
.toArray());
56 mpv
.addPropertyValue("targetBeanName", beanName
);
57 if (useServiceProviderContextClassLoader
)
58 mpv
.addPropertyValue("contextClassLoader",
59 ExportContextClassLoader
.SERVICE_PROVIDER
);
60 RootBeanDefinition bd
= new RootBeanDefinition(
61 osgiServiceFactoryClass
, mpv
);
63 String exporterBeanName
= "osgiService." + beanName
;
64 if (log
.isTraceEnabled())
65 log
.debug("Registering OSGi service exporter "
67 ((BeanDefinitionRegistry
) beanFactory
).registerBeanDefinition(
68 exporterBeanName
, bd
);
71 long end
= System
.currentTimeMillis();
72 if (log
.isTraceEnabled())
73 log
.debug("Multiple services exported in " + (end
- begin
)
78 public void setInterfaces(List
<Class
> interfaces
) {
79 this.interfaces
= interfaces
;
82 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass
) {
83 this.osgiServiceFactoryClass
= osgiServiceFactoryClass
;
86 public int getOrder() {
90 public void setOrder(int order
) {
94 public void setUseServiceProviderContextClassLoader(
95 Boolean useServiceProviderContextClassLoader
) {
96 this.useServiceProviderContextClassLoader
= useServiceProviderContextClassLoader
;