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 // Merge all beans implementing these interfaces
40 Set
<String
> beanNames
= new HashSet
<String
>();
41 for (Class clss
: interfaces
) {
42 String
[] strs
= beanFactory
.getBeanNamesForType(clss
, true, false);
43 beanNames
.addAll(Arrays
.asList(strs
));
46 // Register service factory beans for them
47 for (String beanName
: beanNames
) {
48 MutablePropertyValues mpv
= new MutablePropertyValues();
49 mpv
.addPropertyValue("interfaces", interfaces
.toArray());
50 mpv
.addPropertyValue("targetBeanName", beanName
);
51 RootBeanDefinition bd
= new RootBeanDefinition(
52 osgiServiceFactoryClass
, mpv
);
54 String exporterBeanName
= "osgiService." + beanName
;
55 if (log
.isDebugEnabled())
56 log
.debug("Registering OSGi service exporter "
58 ((BeanDefinitionRegistry
) beanFactory
).registerBeanDefinition(
59 exporterBeanName
, bd
);
63 public void setInterfaces(List
<Class
> interfaces
) {
64 this.interfaces
= interfaces
;
67 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass
) {
68 this.osgiServiceFactoryClass
= osgiServiceFactoryClass
;
72 public int getOrder() {
73 return Ordered
.LOWEST_PRECEDENCE
;