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
.core
.PriorityOrdered
;
20 import org
.springframework
.osgi
.service
.exporter
.support
.ExportContextClassLoader
;
21 import org
.springframework
.osgi
.service
.exporter
.support
.OsgiServiceFactoryBean
;
23 @SuppressWarnings(value
= { "unchecked" })
24 public class MultipleServiceExporterPostProcessor
implements
25 BeanFactoryPostProcessor
, PriorityOrdered
{
26 private final static Log log
= LogFactory
27 .getLog(MultipleServiceExporterPostProcessor
.class);
29 private List
<Class
> interfaces
= new ArrayList
<Class
>();
31 private Class osgiServiceFactoryClass
= OsgiServiceFactoryBean
.class;
33 private Boolean useServiceProviderContextClassLoader
= false;
35 public void postProcessBeanFactory(
36 ConfigurableListableBeanFactory beanFactory
) throws BeansException
{
37 if (!(beanFactory
instanceof BeanDefinitionRegistry
)) {
38 throw new SlcException("Can only work on "
39 + BeanDefinitionRegistry
.class);
42 long begin
= System
.currentTimeMillis();
44 // Merge all beans implementing these interfaces
45 Set
<String
> beanNames
= new HashSet
<String
>();
46 for (Class clss
: interfaces
) {
47 String
[] strs
= beanFactory
.getBeanNamesForType(clss
, true, false);
48 beanNames
.addAll(Arrays
.asList(strs
));
51 // Register service factory beans for them
52 for (String beanName
: beanNames
) {
53 MutablePropertyValues mpv
= new MutablePropertyValues();
54 mpv
.addPropertyValue("interfaces", interfaces
.toArray());
55 mpv
.addPropertyValue("targetBeanName", beanName
);
56 if (useServiceProviderContextClassLoader
)
57 mpv
.addPropertyValue("contextClassLoader",
58 ExportContextClassLoader
.SERVICE_PROVIDER
);
59 RootBeanDefinition bd
= new RootBeanDefinition(
60 osgiServiceFactoryClass
, mpv
);
62 String exporterBeanName
= "osgiService." + beanName
;
63 if (log
.isTraceEnabled())
64 log
.debug("Registering OSGi service exporter "
66 ((BeanDefinitionRegistry
) beanFactory
).registerBeanDefinition(
67 exporterBeanName
, bd
);
70 long end
= System
.currentTimeMillis();
71 if (log
.isTraceEnabled())
72 log
.debug("Multiple services exported in " + (end
- begin
)
77 public void setInterfaces(List
<Class
> interfaces
) {
78 this.interfaces
= interfaces
;
81 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass
) {
82 this.osgiServiceFactoryClass
= osgiServiceFactoryClass
;
85 public int getOrder() {
86 return Ordered
.LOWEST_PRECEDENCE
;
89 public void setUseServiceProviderContextClassLoader(
90 Boolean useServiceProviderContextClassLoader
) {
91 this.useServiceProviderContextClassLoader
= useServiceProviderContextClassLoader
;