cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1341169 - in /cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring: BusApplicationContext.java SpringBeanLocator.java SpringBusFactory.java
Date Mon, 21 May 2012 19:52:01 GMT
Author: dkulp
Date: Mon May 21 19:52:00 2012
New Revision: 1341169

URL: http://svn.apache.org/viewvc?rev=1341169&view=rev
Log:
[CXF-4331] Add methods to SpringBusFactory to make it easier to use it
within an OSGi application.

Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java?rev=1341169&r1=1341168&r2=1341169&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java Mon
May 21 19:52:00 2012
@@ -46,6 +46,7 @@ import org.apache.cxf.interceptor.Fault;
 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 import org.springframework.beans.factory.xml.BeansDtdResolver;
 import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver;
+import org.springframework.beans.factory.xml.NamespaceHandlerResolver;
 import org.springframework.beans.factory.xml.PluggableSchemaResolver;
 import org.springframework.beans.factory.xml.ResourceEntityResolver;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -65,7 +66,7 @@ public class BusApplicationContext exten
 
     private static final Logger LOG = LogUtils.getL7dLogger(BusApplicationContext.class);
     
-    private DefaultNamespaceHandlerResolver nsHandlerResolver;
+    private NamespaceHandlerResolver nsHandlerResolver;
     private boolean includeDefaults;
     private String[] cfgFiles;
     private URL[] cfgFileURLs;
@@ -89,12 +90,17 @@ public class BusApplicationContext exten
     }
     
     public BusApplicationContext(URL url, boolean include, ApplicationContext parent) {
-        this(new URL[] {url}, include, parent);
+        this(new URL[] {url}, include, parent, null);
     } 
     public BusApplicationContext(String[] cf, boolean include, ApplicationContext parent)
{
+        this(cf, include, parent, null);
+    }
+    public BusApplicationContext(String[] cf, boolean include, 
+                                 ApplicationContext parent, NamespaceHandlerResolver res)
{
         super(new String[0], false, parent);
         cfgFiles = cf;
         includeDefaults = include;
+        nsHandlerResolver = res;
         try {
             AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>()
{
                 public Boolean run() throws Exception {
@@ -110,11 +116,17 @@ public class BusApplicationContext exten
             throw new Fault(e);
         }
     }
-    
-    public BusApplicationContext(URL[] url, boolean include, ApplicationContext parent) {
+    public BusApplicationContext(URL[] url, boolean include,
+                                 ApplicationContext parent) {
+        this(url, include, parent, null);
+    }
+    public BusApplicationContext(URL[] url, boolean include,
+                                 ApplicationContext parent,
+                                 NamespaceHandlerResolver res) {
         super(new String[0], false, parent);
         cfgFileURLs = url;
         includeDefaults = include;
+        nsHandlerResolver = res;
         try {
             AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>()
{
                 public Boolean run() throws Exception {
@@ -297,6 +309,7 @@ public class BusApplicationContext exten
             // Create a new XmlBeanDefinitionReader for the given BeanFactory.
         XmlBeanDefinitionReader beanDefinitionReader = 
             new ControlledValidationXmlBeanDefinitionReader(beanFactory);
+        beanDefinitionReader.setNamespaceHandlerResolver(nsHandlerResolver);
         
         // Configure the bean definition reader with this context's
         // resource loading environment.

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java?rev=1341169&r1=1341168&r2=1341169&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java Mon May
21 19:52:00 2012
@@ -59,29 +59,34 @@ public class SpringBeanLocator implement
     Object bundleContext;
     boolean osgi = true;
     
+    public SpringBeanLocator(ApplicationContext ctx) {
+        this(ctx, null);
+    }
     public SpringBeanLocator(ApplicationContext ctx, Bus bus) {
         context = ctx;
-        orig = bus.getExtension(ConfiguredBeanLocator.class);
-        if (orig instanceof ExtensionManagerImpl) {
-            List<String> names = new ArrayList<String>();
-            for (String s : ctx.getBeanDefinitionNames()) {
-                ConfigurableApplicationContext ctxt = (ConfigurableApplicationContext)context;
-                BeanDefinition def = ctxt.getBeanFactory().getBeanDefinition(s);
-                String cn =  def.getBeanClassName();
-                if (OldSpringSupport.class.getName().equals(cn)) {
-                    passThroughs.add(s);
-                    for (String s2 : ctx.getAliases(s)) {
-                        passThroughs.add(s2);
-                    }
-                } else {
-                    names.add(s);
-                    for (String s2 : ctx.getAliases(s)) {
-                        names.add(s2);
+        if (bus != null) {
+            orig = bus.getExtension(ConfiguredBeanLocator.class);
+            if (orig instanceof ExtensionManagerImpl) {
+                List<String> names = new ArrayList<String>();
+                for (String s : ctx.getBeanDefinitionNames()) {
+                    ConfigurableApplicationContext ctxt = (ConfigurableApplicationContext)context;
+                    BeanDefinition def = ctxt.getBeanFactory().getBeanDefinition(s);
+                    String cn =  def.getBeanClassName();
+                    if (OldSpringSupport.class.getName().equals(cn)) {
+                        passThroughs.add(s);
+                        for (String s2 : ctx.getAliases(s)) {
+                            passThroughs.add(s2);
+                        }
+                    } else {
+                        names.add(s);
+                        for (String s2 : ctx.getAliases(s)) {
+                            names.add(s2);
+                        }
                     }
                 }
+                
+                ((ExtensionManagerImpl)orig).removeBeansOfNames(names);
             }
-            
-            ((ExtensionManagerImpl)orig).removeBeansOfNames(names);
         }
         
         loadOSGIContext(bus);
@@ -93,9 +98,11 @@ public class SpringBeanLocator implement
             //for the non-osgi cases
             Method m = context.getClass().getMethod("getBundleContext");
             bundleContext = m.invoke(context);
-            @SuppressWarnings("unchecked")
-            Class<Object> cls = (Class<Object>)m.getReturnType();
-            b.setExtension(bundleContext, cls);
+            if (b != null) {
+                @SuppressWarnings("unchecked")
+                Class<Object> cls = (Class<Object>)m.getReturnType();
+                b.setExtension(bundleContext, cls);
+            }
         } catch (Throwable t) {
             //ignore
             osgi = false;

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java?rev=1341169&r1=1341168&r2=1341169&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java Mon May
21 19:52:00 2012
@@ -22,6 +22,7 @@ package org.apache.cxf.bus.spring;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -33,7 +34,9 @@ import org.apache.cxf.common.logging.Log
 import org.apache.cxf.common.util.SystemPropertyAction;
 import org.apache.cxf.configuration.Configurer;
 import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.xml.NamespaceHandlerResolver;
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.core.io.Resource;
 
 public class SpringBusFactory extends BusFactory {
@@ -41,6 +44,7 @@ public class SpringBusFactory extends Bu
     private static final Logger LOG = LogUtils.getL7dLogger(SpringBusFactory.class);
     
     private final ApplicationContext context;
+    private NamespaceHandlerResolver resolver;
 
     public SpringBusFactory() {
         this.context = null;
@@ -48,11 +52,32 @@ public class SpringBusFactory extends Bu
 
     public SpringBusFactory(ApplicationContext context) {
         this.context = context;
+        this.resolver = tryFindNamespaceHandler(context);
+    }
+    public SpringBusFactory(NamespaceHandlerResolver r) {
+        context = null;
+        this.resolver = r;
     }
     
+    private static NamespaceHandlerResolver tryFindNamespaceHandler(ApplicationContext ctx)
{
+        try {
+            SpringBeanLocator sbl = new SpringBeanLocator(ctx);
+            List<NamespaceHandlerResolver> r = sbl.getOSGiServices(NamespaceHandlerResolver.class);
+            if (r != null && !r.isEmpty()) {
+                return r.get(0);
+            }
+        } catch (Throwable t) {
+            //ignore
+        }
+        return null;
+    }
+
     public ApplicationContext getApplicationContext() {
         return context;
     }
+    public void setNamespaceHandlerResolver(NamespaceHandlerResolver r) {
+        resolver = r;
+    }
         
     public Bus createBus() {
         return createBus((String)null);
@@ -73,11 +98,13 @@ public class SpringBusFactory extends Bu
         return createBus(cfgFiles, defaultBusNotExists());
     }
         
-    private Bus finishCreatingBus(BusApplicationContext bac) {
+    protected Bus finishCreatingBus(ConfigurableApplicationContext bac) {
         final Bus bus = (Bus)bac.getBean(Bus.DEFAULT_BUS_ID);
 
-        bus.setExtension(bac, BusApplicationContext.class);
-
+        bus.setExtension(bac, ApplicationContext.class);
+        if (bac instanceof BusApplicationContext) {
+            bus.setExtension((BusApplicationContext)bac, BusApplicationContext.class);
+        }
         possiblySetDefaultBus(bus);
         
         initializeBus(bus);        
@@ -125,9 +152,9 @@ public class SpringBusFactory extends Bu
         }
     }
     
-    private BusApplicationContext createApplicationContext(String cfgFiles[], boolean includeDefaults)
{
+    protected ConfigurableApplicationContext createApplicationContext(String cfgFiles[],
boolean includeDefaults) {
         try {      
-            return new BusApplicationContext(cfgFiles, includeDefaults, context);
+            return new BusApplicationContext(cfgFiles, includeDefaults, context, resolver);
         } catch (BeansException ex) {
             LogUtils.log(LOG, Level.WARNING, "INITIAL_APP_CONTEXT_CREATION_FAILED_MSG", ex,
(Object[])null);
             ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
@@ -161,14 +188,18 @@ public class SpringBusFactory extends Bu
     
     public Bus createBus(URL[] urls, boolean includeDefaults) {
         try {      
-            return finishCreatingBus(new BusApplicationContext(urls, includeDefaults, context));
+            return finishCreatingBus(createAppContext(urls, includeDefaults));
         } catch (BeansException ex) {
             LogUtils.log(LOG, Level.WARNING, "APP_CONTEXT_CREATION_FAILED_MSG", ex, (Object[])null);
             throw new RuntimeException(ex);
         }
     }
+    
+    protected ConfigurableApplicationContext createAppContext(URL[] urls, boolean includeDefaults)
{
+        return new BusApplicationContext(urls, includeDefaults, context, resolver);
+    }
 
-    void registerApplicationContextLifeCycleListener(Bus bus, BusApplicationContext bac)
{
+    void registerApplicationContextLifeCycleListener(Bus bus, ConfigurableApplicationContext
bac) {
         BusLifeCycleManager lm = bus.getExtension(BusLifeCycleManager.class);
         if (null != lm) {
             lm.registerLifeCycleListener(new BusApplicationContextLifeCycleListener(bac));
@@ -176,9 +207,9 @@ public class SpringBusFactory extends Bu
     } 
 
     static class BusApplicationContextLifeCycleListener implements BusLifeCycleListener {
-        private BusApplicationContext bac;
+        private ConfigurableApplicationContext bac;
 
-        BusApplicationContextLifeCycleListener(BusApplicationContext b) {
+        BusApplicationContextLifeCycleListener(ConfigurableApplicationContext b) {
             bac = b;
         }
 



Mime
View raw message