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
.OsgiServiceFactoryBean
;
22 @SuppressWarnings(value
= { "unchecked" })
23 public class MultipleServiceExporterPostProcessor
implements
24 BeanFactoryPostProcessor
, PriorityOrdered
{
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 public void postProcessBeanFactory(
33 ConfigurableListableBeanFactory beanFactory
) throws BeansException
{
34 if (!(beanFactory
instanceof BeanDefinitionRegistry
)) {
35 throw new SlcException("Can only work on "
36 + BeanDefinitionRegistry
.class);
39 long begin
= System
.currentTimeMillis();
41 // Merge all beans implementing these interfaces
42 Set
<String
> beanNames
= new HashSet
<String
>();
43 for (Class clss
: interfaces
) {
44 String
[] strs
= beanFactory
.getBeanNamesForType(clss
, true, false);
45 beanNames
.addAll(Arrays
.asList(strs
));
48 // Register service factory beans for them
49 for (String beanName
: beanNames
) {
50 MutablePropertyValues mpv
= new MutablePropertyValues();
51 mpv
.addPropertyValue("interfaces", interfaces
.toArray());
52 mpv
.addPropertyValue("targetBeanName", beanName
);
53 RootBeanDefinition bd
= new RootBeanDefinition(
54 osgiServiceFactoryClass
, mpv
);
56 String exporterBeanName
= "osgiService." + beanName
;
57 if (log
.isTraceEnabled())
58 log
.debug("Registering OSGi service exporter "
60 ((BeanDefinitionRegistry
) beanFactory
).registerBeanDefinition(
61 exporterBeanName
, bd
);
64 long end
= System
.currentTimeMillis();
65 if (log
.isTraceEnabled())
66 log
.debug("Multiple services exported in " + (end
- begin
)
71 public void setInterfaces(List
<Class
> interfaces
) {
72 this.interfaces
= interfaces
;
75 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass
) {
76 this.osgiServiceFactoryClass
= osgiServiceFactoryClass
;
79 public int getOrder() {
80 return Ordered
.LOWEST_PRECEDENCE
;