]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/MultipleServiceExporterPostProcessor.java
Introduce relative resource sets
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / MultipleServiceExporterPostProcessor.java
index b9e39f86cc6d87ce95b72a5206d1193d41e9f47f..f5d0a3853c46e70e34159b37bae3c30b970d05c8 100644 (file)
@@ -6,6 +6,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 +15,23 @@ 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.core.PriorityOrdered;
+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, PriorityOrdered {
+       private final static Log log = LogFactory
+                       .getLog(MultipleServiceExporterPostProcessor.class);
+
        private List<Class> interfaces = new ArrayList<Class>();
 
        private Class osgiServiceFactoryClass = OsgiServiceFactoryBean.class;
 
+       private Boolean useServiceProviderContextClassLoader = false;
+
        public void postProcessBeanFactory(
                        ConfigurableListableBeanFactory beanFactory) throws BeansException {
                if (!(beanFactory instanceof BeanDefinitionRegistry)) {
@@ -29,6 +39,8 @@ public class MultipleServiceExporterPostProcessor implements
                                        + BeanDefinitionRegistry.class);
                }
 
+               long begin = System.currentTimeMillis();
+
                // Merge all beans implementing these interfaces
                Set<String> beanNames = new HashSet<String>();
                for (Class clss : interfaces) {
@@ -41,11 +53,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<Class> interfaces) {
@@ -56,4 +82,13 @@ public class MultipleServiceExporterPostProcessor implements
                this.osgiServiceFactoryClass = osgiServiceFactoryClass;
        }
 
+       public int getOrder() {
+               return Ordered.LOWEST_PRECEDENCE;
+       }
+
+       public void setUseServiceProviderContextClassLoader(
+                       Boolean useServiceProviderContextClassLoader) {
+               this.useServiceProviderContextClassLoader = useServiceProviderContextClassLoader;
+       }
+
 }