X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.osgi%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fosgi%2FMultipleServiceExporterPostProcessor.java;h=d457c3b4b8a5234c99ac7119c19460492bd962e7;hb=d00482eedbd988497bf33fc202bb2b2b1282f30b;hp=b9e39f86cc6d87ce95b72a5206d1193d41e9f47f;hpb=ee6c3543a0ff9403420ce6a9c647723269f14331;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java index b9e39f86c..d457c3b4b 100644 --- a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java +++ b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.osgi; import java.util.ArrayList; @@ -6,6 +22,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; @@ -13,15 +31,24 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.core.Ordered; +import org.springframework.osgi.service.exporter.support.ExportContextClassLoader; import org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean; @SuppressWarnings(value = { "unchecked" }) public class MultipleServiceExporterPostProcessor implements - BeanFactoryPostProcessor { + BeanFactoryPostProcessor, Ordered { + private final static Log log = LogFactory + .getLog(MultipleServiceExporterPostProcessor.class); + private List interfaces = new ArrayList(); private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class; + private Boolean useServiceProviderContextClassLoader = false; + + private int order = Ordered.LOWEST_PRECEDENCE; + public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof BeanDefinitionRegistry)) { @@ -29,6 +56,8 @@ public class MultipleServiceExporterPostProcessor implements + BeanDefinitionRegistry.class); } + long begin = System.currentTimeMillis(); + // Merge all beans implementing these interfaces Set beanNames = new HashSet(); for (Class clss : interfaces) { @@ -41,11 +70,25 @@ public class MultipleServiceExporterPostProcessor implements MutablePropertyValues mpv = new MutablePropertyValues(); mpv.addPropertyValue("interfaces", interfaces.toArray()); mpv.addPropertyValue("targetBeanName", beanName); + if (useServiceProviderContextClassLoader) + mpv.addPropertyValue("contextClassLoader", + ExportContextClassLoader.SERVICE_PROVIDER); RootBeanDefinition bd = new RootBeanDefinition( osgiServiceFactoryClass, mpv); + + String exporterBeanName = "osgiService." + beanName; + if (log.isTraceEnabled()) + log.debug("Registering OSGi service exporter " + + exporterBeanName); ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition( - "osgiService." + beanName, bd); + exporterBeanName, bd); } + + long end = System.currentTimeMillis(); + if (log.isTraceEnabled()) + log.debug("Multiple services exported in " + (end - begin) + + " ms in bundle."); + } public void setInterfaces(List interfaces) { @@ -56,4 +99,17 @@ public class MultipleServiceExporterPostProcessor implements this.osgiServiceFactoryClass = osgiServiceFactoryClass; } + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; + } + + public void setUseServiceProviderContextClassLoader( + Boolean useServiceProviderContextClassLoader) { + this.useServiceProviderContextClassLoader = useServiceProviderContextClassLoader; + } + }