]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java
Introduce bundle register
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / MultipleServiceExporterPostProcessor.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.osgi;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.SlcException;
28 import org.springframework.beans.BeansException;
29 import org.springframework.beans.MutablePropertyValues;
30 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
31 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
32 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
33 import org.springframework.beans.factory.support.RootBeanDefinition;
34 import org.springframework.core.Ordered;
35 import org.springframework.osgi.service.exporter.support.ExportContextClassLoader;
36 import org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean;
37
38 @SuppressWarnings(value = { "unchecked" })
39 public class MultipleServiceExporterPostProcessor implements
40 BeanFactoryPostProcessor, Ordered {
41 private final static Log log = LogFactory
42 .getLog(MultipleServiceExporterPostProcessor.class);
43
44 private List<Class> interfaces = new ArrayList<Class>();
45
46 private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
47
48 private Boolean useServiceProviderContextClassLoader = false;
49
50 private int order = Ordered.LOWEST_PRECEDENCE;
51
52 public void postProcessBeanFactory(
53 ConfigurableListableBeanFactory beanFactory) throws BeansException {
54 if (!(beanFactory instanceof BeanDefinitionRegistry)) {
55 throw new SlcException("Can only work on "
56 + BeanDefinitionRegistry.class);
57 }
58
59 long begin = System.currentTimeMillis();
60
61 // Merge all beans implementing these interfaces
62 Set<String> beanNames = new HashSet<String>();
63 for (Class clss : interfaces) {
64 String[] strs = beanFactory.getBeanNamesForType(clss, true, false);
65 beanNames.addAll(Arrays.asList(strs));
66 }
67
68 // Register service factory beans for them
69 for (String beanName : beanNames) {
70 MutablePropertyValues mpv = new MutablePropertyValues();
71 mpv.addPropertyValue("interfaces", interfaces.toArray());
72 mpv.addPropertyValue("targetBeanName", beanName);
73 if (useServiceProviderContextClassLoader)
74 mpv.addPropertyValue("contextClassLoader",
75 ExportContextClassLoader.SERVICE_PROVIDER);
76 RootBeanDefinition bd = new RootBeanDefinition(
77 osgiServiceFactoryClass, mpv);
78
79 String exporterBeanName = "osgiService." + beanName;
80 if (log.isTraceEnabled())
81 log.debug("Registering OSGi service exporter "
82 + exporterBeanName);
83 ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
84 exporterBeanName, bd);
85 }
86
87 long end = System.currentTimeMillis();
88 if (log.isTraceEnabled())
89 log.debug("Multiple services exported in " + (end - begin)
90 + " ms in bundle.");
91
92 }
93
94 public void setInterfaces(List<Class> interfaces) {
95 this.interfaces = interfaces;
96 }
97
98 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass) {
99 this.osgiServiceFactoryClass = osgiServiceFactoryClass;
100 }
101
102 public int getOrder() {
103 return order;
104 }
105
106 public void setOrder(int order) {
107 this.order = order;
108 }
109
110 public void setUseServiceProviderContextClassLoader(
111 Boolean useServiceProviderContextClassLoader) {
112 this.useServiceProviderContextClassLoader = useServiceProviderContextClassLoader;
113 }
114
115 }