You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/07/06 22:33:35 UTC

svn commit: r1143550 - /james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java

Author: norman
Date: Wed Jul  6 20:33:34 2011
New Revision: 1143550

URL: http://svn.apache.org/viewvc?rev=1143550&view=rev
Log:
Make sure the right BeanFactory is used for register the beans (still work in progress). See JAMES-855

Modified:
    james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java

Modified: james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java?rev=1143550&r1=1143549&r2=1143550&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java (original)
+++ james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java Wed Jul  6 20:33:34 2011
@@ -30,16 +30,21 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.BeanFactoryAware;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.context.ApplicationContext;
 import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.osgi.service.exporter.OsgiServicePropertiesResolver;
 import org.springframework.osgi.service.exporter.support.BeanNameServicePropertiesResolver;
 
 /**
@@ -53,9 +58,9 @@ import org.springframework.osgi.service.
 public abstract class AbstractServiceTracker implements BeanFactoryAware, BundleListener, BundleContextAware, InitializingBean, DisposableBean {
 
     private BundleContext context;
-    private BeanFactory factory;
     private String configuredClass;
     private final List<ServiceRegistration> reg = new ArrayList<ServiceRegistration>();
+    private BeanFactory factory;
 
     @Override
     public void setBeanFactory(BeanFactory factory) throws BeansException {
@@ -89,7 +94,7 @@ public abstract class AbstractServiceTra
 
                     String className = file.replaceAll("/", ".").replaceAll(".class", "").replaceFirst(".", "");
                     if (className.equals(configuredClass)) {
-                        
+                        BeanFactory bFactory = getBeanFactory(b.getBundleContext());
                         // Get the right service properties from the resolver
                         Properties p = new Properties();
 
@@ -102,12 +107,12 @@ public abstract class AbstractServiceTra
                         Class<?> clazz = getServiceClass();
                         
                         // Create the definition and register it
-                        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
+                        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) bFactory;
                         BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(className).getBeanDefinition();
                         registry.registerBeanDefinition(getComponentName(), def);
 
                         // register the bean as service in the BundleContext
-                        reg.add(b.getBundleContext().registerService(clazz.getName(), factory.getBean(getComponentName(), clazz), p));
+                        reg.add(b.getBundleContext().registerService(clazz.getName(), bFactory.getBean(getComponentName(), clazz), p));
                     }
                 }
             }
@@ -133,6 +138,23 @@ public abstract class AbstractServiceTra
 
     }
 
+    private static AutowireCapableBeanFactory getBeanFactory(final BundleContext bundleContext) {
+        final String filter = "(" + OsgiServicePropertiesResolver.BEAN_NAME_PROPERTY_KEY + "=" + bundleContext.getBundle().getSymbolicName() + ")";
+        final ServiceReference[] applicationContextRefs;       
+        try {
+            applicationContextRefs = bundleContext.getServiceReferences(ApplicationContext.class.getName(), filter);
+        } catch (final InvalidSyntaxException e) {
+            throw new RuntimeException(e);
+        }
+       
+        if(applicationContextRefs.length != 1) {
+            return null;
+        }
+       
+        return ((ApplicationContext) bundleContext.getService(applicationContextRefs[0])).getAutowireCapableBeanFactory();
+       
+    }
+
     @Override
     public void afterPropertiesSet() throws Exception {
         ConfigurationProvider confProvider = factory.getBean(ConfigurationProvider.class);



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org