Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7F73D86B9 for ; Wed, 7 Sep 2011 15:17:21 +0000 (UTC) Received: (qmail 11774 invoked by uid 500); 7 Sep 2011 15:17:21 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 11715 invoked by uid 500); 7 Sep 2011 15:17:20 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 11708 invoked by uid 99); 7 Sep 2011 15:17:20 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Sep 2011 15:17:20 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Sep 2011 15:17:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AFFF9238897D for ; Wed, 7 Sep 2011 15:16:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1166218 - in /cxf/branches/2.4.x-fixes: ./ rt/core/src/main/java/org/apache/cxf/bus/blueprint/ rt/core/src/main/java/org/apache/cxf/bus/extension/ rt/core/src/main/java/org/apache/cxf/bus/osgi/ rt/core/src/main/java/org/apache/cxf/bus/spri... Date: Wed, 07 Sep 2011 15:16:54 -0000 To: commits@cxf.apache.org From: dkulp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110907151654.AFFF9238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dkulp Date: Wed Sep 7 15:16:53 2011 New Revision: 1166218 URL: http://svn.apache.org/viewvc?rev=1166218&view=rev Log: Merged revisions 1166215 via svnmerge from https://svn.apache.org/repos/asf/cxf/trunk ........ r1166215 | dkulp | 2011-09-07 11:11:03 -0400 (Wed, 07 Sep 2011) | 1 line Get the CXF Bus objects registered into the OSGi service registry ........ Modified: cxf/branches/2.4.x-fixes/ (props changed) cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java cxf/branches/2.4.x-fixes/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java Propchange: cxf/branches/2.4.x-fixes/ ('svn:mergeinfo' removed) Propchange: cxf/branches/2.4.x-fixes/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBus.java Wed Sep 7 15:16:53 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/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java Wed Sep 7 15:16:53 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 namespaces = new ArrayList(); @@ -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/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java Wed Sep 7 15:16:53 2011 @@ -103,7 +103,7 @@ public class ExtensionManagerImpl implem } for (Map.Entry ext : ExtensionRegistry.getRegisteredExtensions().entrySet()) { - if (all.containsKey(ext.getKey())) { + if (!all.containsKey(ext.getKey())) { all.put(ext.getKey(), ext.getValue()); } } Modified: cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java Wed Sep 7 15:16:53 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> extensions = new ConcurrentHashMap>(); 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 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/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java Wed Sep 7 15:16:53 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 passThroughs = new HashSet(); + 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 cls = (Class)m.getReturnType(); + b.setExtension(bundleContext, cls); + } catch (Throwable t) { + //ignore + osgi = false; + } + } + + /** {@inheritDoc}*/ public List getBeanNamesOfType(Class type) { Set s = new LinkedHashSet(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/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManager.java Wed Sep 7 15:16:53 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 listeners; + private final CopyOnWriteArrayList 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/branches/2.4.x-fixes/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java?rev=1166218&r1=1166217&r2=1166218&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java (original) +++ cxf/branches/2.4.x-fixes/rt/core/src/test/java/org/apache/cxf/buslifecycle/CXFBusLifeCycleManagerTest.java Wed Sep 7 15:16:53 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();