2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.osgi
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
22 import java
.util
.Properties
;
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
;
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);
42 private List
<Class
> interfaces
= new ArrayList
<Class
>();
44 private int order
= Ordered
.LOWEST_PRECEDENCE
;
46 private BundleContext bundleContext
= null;
48 // private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
49 // private Boolean useServiceProviderContextClassLoader = false;
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));
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());
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
,
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
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);
100 // long begin = System.currentTimeMillis();
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));
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);
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);
128 // long end = System.currentTimeMillis();
129 // if (log.isTraceEnabled())
130 // log.debug("Multiple services exported in " + (end - begin)
131 // + " ms in bundle.");
135 public void setInterfaces(List
<Class
> interfaces
) {
136 this.interfaces
= interfaces
;
139 // public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass) {
140 // this.osgiServiceFactoryClass = osgiServiceFactoryClass;
143 public int getOrder() {
147 public void setOrder(int order
) {
151 // public void setUseServiceProviderContextClassLoader(
152 // Boolean useServiceProviderContextClassLoader) {
153 // this.useServiceProviderContextClassLoader =
154 // useServiceProviderContextClassLoader;
157 public void setBundleContext(BundleContext bundleContext
) {
158 this.bundleContext
= bundleContext
;