]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java
Reactivate launcher
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / MultipleServiceExporterPostProcessor.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.osgi;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.Constants;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.context.ApplicationEvent;
31 import org.springframework.context.ApplicationListener;
32 import org.springframework.context.event.ContextRefreshedEvent;
33 import org.springframework.core.Ordered;
34
35 /** Publishes beans of the application context as OSGi services. */
36 @SuppressWarnings(value = { "unchecked", "rawtypes" })
37 public class MultipleServiceExporterPostProcessor implements
38 ApplicationListener, Ordered {
39 private final static Log log = LogFactory
40 .getLog(MultipleServiceExporterPostProcessor.class);
41
42 private List<Class> interfaces = new ArrayList<Class>();
43
44 private int order = Ordered.LOWEST_PRECEDENCE;
45
46 private BundleContext bundleContext = null;
47
48 // private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
49 // private Boolean useServiceProviderContextClassLoader = false;
50
51 public void onApplicationEvent(ApplicationEvent event) {
52 Map<String, Object> beans = new HashMap<String, Object>();
53 if (event instanceof ContextRefreshedEvent) {
54 if (bundleContext != null) {
55 for (Class clss : interfaces) {
56 ApplicationContext ac = ((ContextRefreshedEvent) event)
57 .getApplicationContext();
58 beans.putAll(ac.getBeansOfType(clss, false, false));
59 }
60
61 int count = 0;
62 for (String beanName : beans.keySet()) {
63 Object bean = beans.get(beanName);
64 List<String> classes = new ArrayList<String>();
65 for (Class clss : interfaces) {
66 if (clss.isAssignableFrom(bean.getClass())) {
67 classes.add(clss.getName());
68 }
69 }
70 Properties props = new Properties();
71 Bundle bundle = bundleContext.getBundle();
72 props.put(Constants.BUNDLE_SYMBOLICNAME,
73 bundle.getSymbolicName());
74 props.put(Constants.BUNDLE_VERSION, bundle.getVersion());
75 // retrocompatibility with pre-1.0:
76 props.put("org.springframework.osgi.bean.name", beanName);
77 bundleContext.registerService(
78 classes.toArray(new String[classes.size()]), bean,
79 props);
80 count++;
81 }
82 if (log.isTraceEnabled())
83 log.trace("Published " + count + " " + interfaces
84 + " as OSGi services from bundle "
85 + bundleContext.getBundle().getSymbolicName() + " "
86 + bundleContext.getBundle().getVersion());
87 // note: the services will be automatically unregistered when
88 // the bundle will be stopped
89 }
90 }
91 }
92
93 // public void postProcessBeanFactory(
94 // ConfigurableListableBeanFactory beanFactory) throws BeansException {
95 // if (!(beanFactory instanceof BeanDefinitionRegistry)) {
96 // throw new SlcException("Can only work on "
97 // + BeanDefinitionRegistry.class);
98 // }
99 //
100 // long begin = System.currentTimeMillis();
101 //
102 // // Merge all beans implementing these interfaces
103 // Set<String> beanNames = new HashSet<String>();
104 // for (Class clss : interfaces) {
105 // String[] strs = beanFactory.getBeanNamesForType(clss, true, false);
106 // beanNames.addAll(Arrays.asList(strs));
107 // }
108 //
109 // // Register service factory beans for them
110 // for (String beanName : beanNames) {
111 // MutablePropertyValues mpv = new MutablePropertyValues();
112 // mpv.addPropertyValue("interfaces", interfaces.toArray());
113 // mpv.addPropertyValue("targetBeanName", beanName);
114 // if (useServiceProviderContextClassLoader)
115 // mpv.addPropertyValue("contextClassLoader",
116 // ExportContextClassLoader.SERVICE_PROVIDER);
117 // RootBeanDefinition bd = new RootBeanDefinition(
118 // osgiServiceFactoryClass, mpv);
119 //
120 // String exporterBeanName = "osgiService." + beanName;
121 // if (log.isTraceEnabled())
122 // log.debug("Registering OSGi service exporter "
123 // + exporterBeanName);
124 // ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
125 // exporterBeanName, bd);
126 // }
127 //
128 // long end = System.currentTimeMillis();
129 // if (log.isTraceEnabled())
130 // log.debug("Multiple services exported in " + (end - begin)
131 // + " ms in bundle.");
132 //
133 // }
134
135 public void setInterfaces(List<Class> interfaces) {
136 this.interfaces = interfaces;
137 }
138
139 // public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass) {
140 // this.osgiServiceFactoryClass = osgiServiceFactoryClass;
141 // }
142
143 public int getOrder() {
144 return order;
145 }
146
147 public void setOrder(int order) {
148 this.order = order;
149 }
150
151 // public void setUseServiceProviderContextClassLoader(
152 // Boolean useServiceProviderContextClassLoader) {
153 // this.useServiceProviderContextClassLoader =
154 // useServiceProviderContextClassLoader;
155 // }
156
157 public void setBundleContext(BundleContext bundleContext) {
158 this.bundleContext = bundleContext;
159 }
160 }