cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1166215 - in /cxf/trunk/rt/core/src: main/java/org/apache/cxf/bus/blueprint/ main/java/org/apache/cxf/bus/extension/ main/java/org/apache/cxf/bus/osgi/ main/java/org/apache/cxf/bus/spring/ main/java/org/apache/cxf/buslifecycle/ test/java/o...
Date Wed, 07 Sep 2011 15:11:04 GMT
Author: dkulp
Date: Wed Sep  7 15:11:03 2011
New Revision: 1166215

URL: http://svn.apache.org/viewvc?rev=1166215&view=rev
Log:
Get the CXF Bus objects registered into the OSGi service registry

Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.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/buslifecycle/CXFBusLifeCycleManager.java
    cxf/trunk/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java Wed Sep
 7 15:11:03 2011
@@ -45,6 +45,7 @@ public class BlueprintBus extends Extens
         super.setExtension(new BundleDelegatingClassLoader(c.getBundle(), 
                                                            this.getClass().getClassLoader()),
                            ClassLoader.class);
+        super.setExtension(c, BundleContext.class);
     }
     public void setBlueprintContainer(BlueprintContainer con) {
         container = con;

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java Wed Sep  7
15:11:03 2011
@@ -31,6 +31,7 @@ public class Extension {
     protected String className;
     protected ClassLoader classloader;
     protected Class<?> clazz;
+    protected Class<?> intf;
     protected String interfaceName;
     protected boolean deferred;
     protected Collection<String> namespaces = new ArrayList<String>();
@@ -38,6 +39,20 @@ public class Extension {
     
     public Extension() {
     }
+    
+    public Extension(Class<?> cls, Class<?> inf) {
+        clazz = cls;
+        intf = inf;
+        interfaceName = inf.getName();
+        className = cls.getName();
+        classloader = cls.getClassLoader();
+    }
+    public Extension(Class<?> cls) {
+        clazz = cls;
+        className = cls.getName();
+        classloader = cls.getClassLoader();
+    }
+    
     public Extension(Extension ext) {
         className = ext.className;
         interfaceName = ext.interfaceName;
@@ -45,6 +60,7 @@ public class Extension {
         namespaces = ext.namespaces;
         obj = ext.obj;
         clazz = ext.clazz;
+        intf = ext.intf;
         classloader = ext.classloader;
     }
     
@@ -59,6 +75,7 @@ public class Extension {
         Extension ext = new Extension(this);
         ext.obj = null;
         ext.clazz = null;
+        ext.intf = null;
         return ext;
     }
     
@@ -162,26 +179,31 @@ public class Extension {
     }
     
     public Class loadInterface(ClassLoader cl) {
-        Class<?> cls = null;
+        if (intf != null) {
+            return intf;
+        }
         if (classloader != null) {
             try {
-                return classloader.loadClass(interfaceName);
+                intf = classloader.loadClass(interfaceName);
+                if (intf != null) {
+                    return intf;
+                }
             } catch (ClassNotFoundException nex) {
                 //ignore, fall into the stuff below
             }
         }                
 
         try {
-            cls = cl.loadClass(interfaceName);
+            intf = cl.loadClass(interfaceName);
         } catch (ClassNotFoundException ex) {
             try {
                 // using the extension classloader as a fallback
-                cls = this.getClass().getClassLoader().loadClass(interfaceName);
+                intf = this.getClass().getClassLoader().loadClass(interfaceName);
             } catch (ClassNotFoundException nex) {
                 throw new ExtensionException(nex);
             }
         }
-        return cls;
+        return intf;
     }
     
     

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
Wed Sep  7 15:11:03 2011
@@ -103,7 +103,7 @@ public class ExtensionManagerImpl implem
         }
         
         for (Map.Entry<String, Extension> ext : ExtensionRegistry.getRegisteredExtensions().entrySet())
{
-            if (all.containsKey(ext.getKey())) {
+            if (!all.containsKey(ext.getKey())) {
                 all.put(ext.getKey(), ext.getValue());
             }
         }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java Wed
Sep  7 15:11:03 2011
@@ -22,22 +22,31 @@ package org.apache.cxf.bus.osgi;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Collections;
+import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.List;
+import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.logging.Logger;
 
+import org.apache.cxf.Bus;
 import org.apache.cxf.bus.extension.Extension;
 import org.apache.cxf.bus.extension.ExtensionFragmentParser;
 import org.apache.cxf.bus.extension.ExtensionRegistry;
+import org.apache.cxf.buslifecycle.BusLifeCycleListener;
+import org.apache.cxf.buslifecycle.BusLifeCycleManager;
 import org.apache.cxf.common.logging.LogUtils;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.framework.Version;
 
 /**
  * 
@@ -47,6 +56,7 @@ public class OSGiExtensionLocator implem
     private ConcurrentMap<Long, List<Extension>> extensions 
         = new ConcurrentHashMap<Long, List<Extension>>();
     private long id;
+    private Extension listener;
 
     /** {@inheritDoc}*/
     public void bundleChanged(BundleEvent event) {
@@ -65,6 +75,7 @@ public class OSGiExtensionLocator implem
     public void start(BundleContext context) throws Exception {
         context.addBundleListener(this);
         id = context.getBundle().getBundleId();
+        registerBusListener();
         for (Bundle bundle : context.getBundles()) {
             if ((bundle.getState() == Bundle.RESOLVED 
                 || bundle.getState() == Bundle.STARTING 
@@ -79,11 +90,19 @@ public class OSGiExtensionLocator implem
     /** {@inheritDoc}*/
     public void stop(BundleContext context) throws Exception {
         context.removeBundleListener(this);
+        unregisterBusListener();
         while (!extensions.isEmpty()) {
             unregister(extensions.keySet().iterator().next());
         }
     }
-
+    private void registerBusListener() {
+        listener = new Extension(OSGIBusListener.class);
+        ExtensionRegistry.addExtensions(Collections.singletonList(listener));
+    }
+    private void unregisterBusListener() {
+        ExtensionRegistry.removeExtensions(Collections.singletonList(listener));
+        listener = null;
+    }
     
     protected void register(final Bundle bundle) throws IOException {
         List<Extension> list = extensions.get(bundle.getBundleId());
@@ -116,6 +135,43 @@ public class OSGiExtensionLocator implem
             ExtensionRegistry.removeExtensions(list);
         }
     }
+    
+    public static class OSGIBusListener implements BusLifeCycleListener {
+        public static final String CONTEXT_SYMBOLIC_NAME_PROPERTY = "cxf.context.symbolicname";
+        public static final String CONTEXT_VERSION_PROPERTY = "cxf.context.version";
+        public static final String CONTEXT_NAME_PROPERTY = "cxf.bus.id";
+        
+        Bus bus;
+        BundleContext context;
+        ServiceRegistration service;
+ 
+        public OSGIBusListener(Bus b) {
+            bus = b;
+            bus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this);
+        }
+        private Version getBundleVersion(Bundle bundle) {
+            Dictionary headers = bundle.getHeaders();
+            String version = (String) headers.get(Constants.BUNDLE_VERSION);
+            return (version != null) ? Version.parseVersion(version) : Version.emptyVersion;
+        }
+ 
+        public void initComplete() {
+            context = bus.getExtension(BundleContext.class);
+            Properties props = new Properties();
+            props.put(CONTEXT_SYMBOLIC_NAME_PROPERTY, context.getBundle().getSymbolicName());
+            props.put(CONTEXT_VERSION_PROPERTY, getBundleVersion(context.getBundle()));
+            props.put(CONTEXT_NAME_PROPERTY, bus.getId());
+
+            service = context.registerService(Bus.class.getName(), bus, props);
+        }
+        public void preShutdown() {
+        }
+        public void postShutdown() {
+            service.unregister();
+            service = null;
+        }
+    }
+    
     public class OSGiExtension extends Extension {
         final Bundle bundle;
         public OSGiExtension(Extension e, Bundle b) {

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=1166215&r1=1166214&r2=1166215&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 Wed Sep
 7 15:11:03 2011
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.bus.spring;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -48,6 +49,7 @@ public class SpringBeanLocator implement
     ApplicationContext context;
     ConfiguredBeanLocator orig;
     Set<String> passThroughs = new HashSet<String>();
+    Object bundleContext;
     boolean osgi = true;
     
     public SpringBeanLocator(ApplicationContext ctx, Bus bus) {
@@ -74,8 +76,26 @@ public class SpringBeanLocator implement
             
             ((ExtensionManagerImpl)orig).removeBeansOfNames(names);
         }
+        
+        loadOSGIContext(bus);
     }
 
+    private void loadOSGIContext(Bus b) {
+        try {
+            //use a little reflection to allow this to work without the spring-dm jars
+            //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);
+        } catch (Throwable t) {
+            //ignore
+            osgi = false;
+        }
+    }
+    
+    
     /** {@inheritDoc}*/
     public List<String> getBeanNamesOfType(Class<?> type) {
         Set<String> s = new LinkedHashSet<String>(Arrays.asList(context.getBeanNamesForType(type,
@@ -109,11 +129,12 @@ public class SpringBeanLocator implement
         try {
             //use a little reflection to allow this to work without the spring-dm jars
             //for the non-osgi cases
-            Object bc = context.getClass().getMethod("getBundleContext").invoke(context);
-            Object o = bc.getClass()
-                .getMethod("getServiceReference", String.class).invoke(bc, type.getName());
+
+            Object o = bundleContext.getClass()
+                .getMethod("getServiceReference", String.class).invoke(bundleContext, type.getName());
             if (o != null) {
-                o = bc.getClass().getMethod("getService", ServiceReference.class).invoke(bc,
o);
+                o = bundleContext.getClass().getMethod("getService", ServiceReference.class)
+                    .invoke(bundleContext, o);
                 lst.add(type.cast(o));
             }
         } catch (NoSuchMethodException e) {

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java
Wed Sep  7 15:11:03 2011
@@ -19,7 +19,6 @@
 
 package org.apache.cxf.buslifecycle;
 
-import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.annotation.Resource;
@@ -30,7 +29,7 @@ import org.apache.cxf.common.injection.N
 @NoJSR250Annotations(unlessNull = "bus")
 public class CXFBusLifeCycleManager implements BusLifeCycleManager {
 
-    private final List<BusLifeCycleListener> listeners;
+    private final CopyOnWriteArrayList<BusLifeCycleListener> listeners;
     private Bus bus;
     private boolean preShutdownCalled;
     private boolean postShutdownCalled;
@@ -56,7 +55,7 @@ public class CXFBusLifeCycleManager impl
      * org.apache.cxf.buslifecycle.BusLifeCycleListener)
      */
     public void registerLifeCycleListener(BusLifeCycleListener listener) {
-        listeners.add(listener);
+        listeners.addIfAbsent(listener);
         
     }
 
@@ -77,7 +76,6 @@ public class CXFBusLifeCycleManager impl
     }
     
     public void preShutdown() {
-        // TODO inverse order of registration?
         if (!preShutdownCalled) { 
             preShutdownCalled = true;
             for (BusLifeCycleListener listener : listeners) {
@@ -92,7 +90,6 @@ public class CXFBusLifeCycleManager impl
         }
         if (!postShutdownCalled) {
             postShutdownCalled = true;
-            // TODO inverse order of registration?
             for (BusLifeCycleListener listener : listeners) {
                 listener.postShutdown();
             }

Modified: cxf/trunk/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java?rev=1166215&r1=1166214&r2=1166215&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java
(original)
+++ cxf/trunk/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java
Wed Sep  7 15:11:03 2011
@@ -76,37 +76,6 @@ public class CXFBusLifeCycleManagerTest 
     }
     
     @Test
-    public void testDuplicateRegistration() {
-        
-        BusLifeCycleListener listener1 = EasyMock.createMock(BusLifeCycleListener.class);
-        CXFBusLifeCycleManager mgr = new CXFBusLifeCycleManager();
-
-        mgr.registerLifeCycleListener(listener1);
-        mgr.registerLifeCycleListener(listener1);
-
-        EasyMock.reset(listener1);
-        listener1.initComplete();
-        EasyMock.expectLastCall().times(2);
-        EasyMock.replay(listener1);
-        mgr.initComplete();
-        EasyMock.verify(listener1);
-
-        EasyMock.reset(listener1);
-        listener1.preShutdown();
-        EasyMock.expectLastCall().times(2);
-        EasyMock.replay(listener1);
-        mgr.preShutdown();
-        EasyMock.verify(listener1);
-
-        EasyMock.reset(listener1);
-        listener1.postShutdown();
-        EasyMock.expectLastCall().times(2);
-        EasyMock.replay(listener1);
-        mgr.postShutdown();
-        EasyMock.verify(listener1);
-    }
-    
-    @Test
     public void testMultipleListeners() {
        
         IMocksControl ctrl = EasyMock.createStrictControl();



Mime
View raw message