]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java
Implement ref spec
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / MultipleServiceExporterPostProcessor.java
1 package org.argeo.slc.osgi;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
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;
21
22 @SuppressWarnings(value = { "unchecked" })
23 public class MultipleServiceExporterPostProcessor implements
24 BeanFactoryPostProcessor, PriorityOrdered {
25 private final static Log log = LogFactory
26 .getLog(MultipleServiceExporterPostProcessor.class);
27
28 private List<Class> interfaces = new ArrayList<Class>();
29
30 private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
31
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);
37 }
38
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));
44 }
45
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);
53
54 String exporterBeanName = "osgiService." + beanName;
55 if (log.isTraceEnabled())
56 log.debug("Registering OSGi service exporter "
57 + exporterBeanName);
58 ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
59 exporterBeanName, bd);
60 }
61 }
62
63 public void setInterfaces(List<Class> interfaces) {
64 this.interfaces = interfaces;
65 }
66
67 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass) {
68 this.osgiServiceFactoryClass = osgiServiceFactoryClass;
69 }
70
71 public int getOrder() {
72 return Ordered.LOWEST_PRECEDENCE;
73 }
74
75 }