2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.osgi
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Arrays
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
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
;
38 @SuppressWarnings(value
= { "unchecked" })
39 public class MultipleServiceExporterPostProcessor
implements
40 BeanFactoryPostProcessor
, Ordered
{
41 private final static Log log
= LogFactory
42 .getLog(MultipleServiceExporterPostProcessor
.class);
44 private List
<Class
> interfaces
= new ArrayList
<Class
>();
46 private Class osgiServiceFactoryClass
= OsgiServiceFactoryBean
.class;
48 private Boolean useServiceProviderContextClassLoader
= false;
50 private int order
= Ordered
.LOWEST_PRECEDENCE
;
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);
59 long begin
= System
.currentTimeMillis();
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
));
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
);
79 String exporterBeanName
= "osgiService." + beanName
;
80 if (log
.isTraceEnabled())
81 log
.debug("Registering OSGi service exporter "
83 ((BeanDefinitionRegistry
) beanFactory
).registerBeanDefinition(
84 exporterBeanName
, bd
);
87 long end
= System
.currentTimeMillis();
88 if (log
.isTraceEnabled())
89 log
.debug("Multiple services exported in " + (end
- begin
)
94 public void setInterfaces(List
<Class
> interfaces
) {
95 this.interfaces
= interfaces
;
98 public void setOsgiServiceFactoryClass(Class osgiServiceFactoryClass
) {
99 this.osgiServiceFactoryClass
= osgiServiceFactoryClass
;
102 public int getOrder() {
106 public void setOrder(int order
) {
110 public void setUseServiceProviderContextClassLoader(
111 Boolean useServiceProviderContextClassLoader
) {
112 this.useServiceProviderContextClassLoader
= useServiceProviderContextClassLoader
;