]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java
Start improving Ant
[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.ExportContextClassLoader;
21 import org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean;
22
23 @SuppressWarnings(value = { "unchecked" })
24 public class MultipleServiceExporterPostProcessor implements
25 BeanFactoryPostProcessor, PriorityOrdered {
26 private final static Log log = LogFactory
27 .getLog(MultipleServiceExporterPostProcessor.class);
28
29 private List<Class> interfaces = new ArrayList<Class>();
30
31 private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
32
33 private Boolean useServiceProviderContextClassLoader = false;
34
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);
40 }
41
42 long begin = System.currentTimeMillis();
43
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));
49 }
50
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);
61
62 String exporterBeanName = "osgiService." + beanName;
63 if (log.isTraceEnabled())
64 log.debug("Registering OSGi service exporter "
65 + exporterBeanName);
66 ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
67 exporterBeanName, bd);
68 }
69
70 long end = System.currentTimeMillis();
71 if (log.isTraceEnabled())
72 log.debug("Multiple services exported in " + (end - begin)
73 + " ms in bundle.");
74
75 }
76
77 public void setInterfaces(List<Class> interfaces) {
78 this.interfaces = interfaces;
79 }
80
81 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass) {
82 this.osgiServiceFactoryClass = osgiServiceFactoryClass;
83 }
84
85 public int getOrder() {
86 return Ordered.LOWEST_PRECEDENCE;
87 }
88
89 public void setUseServiceProviderContextClassLoader(
90 Boolean useServiceProviderContextClassLoader) {
91 this.useServiceProviderContextClassLoader = useServiceProviderContextClassLoader;
92 }
93
94 }