You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2011/12/07 22:29:50 UTC

svn commit: r1211645 - in /cxf/trunk: api/src/main/java/org/apache/cxf/feature/ rt/core/src/main/java/org/apache/cxf/bus/ rt/core/src/main/java/org/apache/cxf/bus/blueprint/ rt/core/src/main/java/org/apache/cxf/bus/osgi/ rt/core/src/main/java/org/apach...

Author: dkulp
Date: Wed Dec  7 21:29:49 2011
New Revision: 1211645

URL: http://svn.apache.org/viewvc?rev=1211645&view=rev
Log:
[CXF-3962,CXF-3901] Make sure the bus.initialize() is called when it
really is initialized.   Have OSGi pull Features and Listeners from the
OSGi service registry at Bus startup.

Added:
    cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java   (with props)
    cxf/trunk/api/src/main/java/org/apache/cxf/feature/WrappedFeature.java
      - copied, changed from r1211547, cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java
Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/CXFBusImpl.java
    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/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/bus/spring/SpringBus.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/endpoint/ServerImpl.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java?rev=1211645&r1=1211644&r2=1211645&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java Wed Dec  7 21:29:49 2011
@@ -34,7 +34,7 @@ import org.apache.cxf.interceptor.Interc
  * If you're simply adding interceptors to a Server, Client, or Bus, this allows you to add
  * them easily.
  */
-public abstract class AbstractFeature {
+public abstract class AbstractFeature implements Feature {
     public void initialize(Server server, Bus bus) {
         initializeProvider(server.getEndpoint(), bus);
     }

Added: cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java?rev=1211645&view=auto
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java (added)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java Wed Dec  7 21:29:49 2011
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.feature;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.interceptor.InterceptorProvider;
+
+/**
+ * A Feature is something that is able to customize a Server, Client, or Bus, typically
+ * adding capabilities. For instance, there may be a LoggingFeature which configures
+ * one of the above to log each of their messages.
+ * <p>
+ * By default the initialize methods all delegate to initializeProvider(InterceptorProvider). 
+ * If you're simply adding interceptors to a Server, Client, or Bus, this allows you to add
+ * them easily.
+ */
+public interface Feature {
+    void initialize(Server server, Bus bus);
+    
+    void initialize(Client client, Bus bus);
+    
+    void initialize(InterceptorProvider interceptorProvider, Bus bus);
+    
+    void initialize(Bus bus);
+}

Propchange: cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/api/src/main/java/org/apache/cxf/feature/Feature.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: cxf/trunk/api/src/main/java/org/apache/cxf/feature/WrappedFeature.java (from r1211547, cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/feature/WrappedFeature.java?p2=cxf/trunk/api/src/main/java/org/apache/cxf/feature/WrappedFeature.java&p1=cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java&r1=1211547&r2=1211645&rev=1211645&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/feature/AbstractFeature.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/feature/WrappedFeature.java Wed Dec  7 21:29:49 2011
@@ -18,8 +18,6 @@
  */
 package org.apache.cxf.feature;
 
-import java.util.List;
-
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.Server;
@@ -34,45 +32,26 @@ import org.apache.cxf.interceptor.Interc
  * If you're simply adding interceptors to a Server, Client, or Bus, this allows you to add
  * them easily.
  */
-public abstract class AbstractFeature {
+public class WrappedFeature extends AbstractFeature implements Feature {
+    final Feature wrapped;
+    public WrappedFeature(Feature f) {
+        wrapped = f;
+    }
+    
     public void initialize(Server server, Bus bus) {
-        initializeProvider(server.getEndpoint(), bus);
+        wrapped.initialize(server, bus);
     }
     
     public void initialize(Client client, Bus bus) {
-        initializeProvider(client, bus);
+        wrapped.initialize(client, bus);
     }
     
     public void initialize(InterceptorProvider interceptorProvider, Bus bus) {
-        initializeProvider(interceptorProvider, bus);
+        wrapped.initialize(interceptorProvider, bus);
     }
     
     public void initialize(Bus bus) {
-        initializeProvider(bus, bus);
+        wrapped.initialize(bus);
     }
     
-    protected void initializeProvider(InterceptorProvider provider, Bus bus) {
-        
-    }
-
-    /**
-     * Convenience method to extract a feature by type from an active list.
-     * 
-     * @param features the given feature list
-     * @param type the feature type required
-     * @return the feature of the specified type if active
-     */
-    public static <T> T getActive(List<AbstractFeature> features,
-                                  Class<T> type) {
-        T active = null;
-        if (features != null) {
-            for (AbstractFeature feature : features) {
-                if (type.isInstance(feature)) {
-                    active = type.cast(feature);
-                    break;
-                }
-            }
-        }
-        return active;
-    }
 }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/CXFBusImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/CXFBusImpl.java?rev=1211645&r1=1211644&r2=1211645&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/CXFBusImpl.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/CXFBusImpl.java Wed Dec  7 21:29:49 2011
@@ -55,8 +55,8 @@ public class CXFBusImpl extends Abstract
     protected final Map<Class, Object> extensions;
     protected String id;
     private BusState state;      
-    private Collection<AbstractFeature> features;
-    private Map<String, Object> properties = new ConcurrentHashMap<String, Object>();
+    private final Collection<AbstractFeature> features = new CopyOnWriteArrayList<AbstractFeature>();
+    private final Map<String, Object> properties = new ConcurrentHashMap<String, Object>();
     
     public CXFBusImpl() {
         this(null);
@@ -74,7 +74,6 @@ public class CXFBusImpl extends Abstract
         
         CXFBusFactory.possiblySetDefaultBus(this);
         if (FORCE_LOGGING) {
-            features = new CopyOnWriteArrayList<AbstractFeature>();
             features.add(new LoggingFeature());
         }
     }
@@ -174,8 +173,13 @@ public class CXFBusImpl extends Abstract
     protected void doInitializeInternal() {
         initializeFeatures();
     }
+
+    protected void loadAdditionalFeatures() {
+        
+    }
     
     protected void initializeFeatures() {
+        loadAdditionalFeatures();
         if (features != null) {
             for (AbstractFeature f : features) {
                 f.initialize(this);
@@ -226,7 +230,8 @@ public class CXFBusImpl extends Abstract
     }
 
     public synchronized void setFeatures(Collection<AbstractFeature> features) {
-        this.features = new CopyOnWriteArrayList<AbstractFeature>(features);
+        this.features.clear();
+        this.features.addAll(features);
         if (FORCE_LOGGING) {
             this.features.add(new LoggingFeature());
         }

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=1211645&r1=1211644&r2=1211645&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 Dec  7 21:29:49 2011
@@ -23,7 +23,12 @@ package org.apache.cxf.bus.blueprint;
 import org.apache.cxf.bus.extension.ExtensionManagerBus;
 import org.apache.cxf.configuration.ConfiguredBeanLocator;
 import org.apache.cxf.configuration.Configurer;
+import org.apache.cxf.feature.AbstractFeature;
+import org.apache.cxf.feature.Feature;
+import org.apache.cxf.feature.WrappedFeature;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 
 /**
@@ -39,6 +44,26 @@ public class BlueprintBus extends Extens
         super(null, null, BlueprintBus.class.getClassLoader());
     }
     
+    public void loadAdditionalFeatures() {
+        super.loadAdditionalFeatures();
+        try {
+            ServiceReference refs[] = context.getServiceReferences(Feature.class.getName(), null);
+            if (refs == null) {
+                return;
+            }
+            for (ServiceReference ref : refs) {
+                Feature feature = (Feature)context.getService(ref);
+                if (feature instanceof AbstractFeature) {
+                    this.getFeatures().add((AbstractFeature)feature);
+                } else {
+                    this.getFeatures().add(new WrappedFeature(feature));
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            //ignore
+        }
+    }
+    
     public void setBundleContext(BundleContext c) {
         context = c;
         super.setExtension(new BundleDelegatingClassLoader(c.getBundle(), 

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=1211645&r1=1211644&r2=1211645&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 Dec  7 21:29:49 2011
@@ -45,6 +45,10 @@ import org.apache.cxf.buslifecycle.BusLi
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.configuration.ConfiguredBeanLocator;
 import org.apache.cxf.configuration.ConfiguredBeanLocator.BeanLoaderListener;
+import org.apache.cxf.endpoint.ClientLifeCycleListener;
+import org.apache.cxf.endpoint.ClientLifeCycleManager;
+import org.apache.cxf.endpoint.ServerLifeCycleListener;
+import org.apache.cxf.endpoint.ServerLifeCycleManager;
 import org.apache.cxf.workqueue.AutomaticWorkQueueImpl;
 import org.apache.cxf.workqueue.WorkQueueManager;
 import org.osgi.framework.Bundle;
@@ -52,6 +56,7 @@ import org.osgi.framework.BundleActivato
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.SynchronousBundleListener;
@@ -199,13 +204,56 @@ public class OSGiExtensionLocator implem
                 && args[0] instanceof BundleContext) {
                 defaultContext = (BundleContext)args[0];
             }
-            bus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this);
+            BusLifeCycleManager manager = bus.getExtension(BusLifeCycleManager.class);
+            manager.registerLifeCycleListener(this);
             final ConfiguredBeanLocator cbl = bus.getExtension(ConfiguredBeanLocator.class);
             if (cbl instanceof ExtensionManagerImpl) {
                 // wire in the OSGi things
                 bus.setExtension(new OSGiBeanLocator(cbl, defaultContext), 
                                  ConfiguredBeanLocator.class);
             }
+            
+            try {
+                ServiceReference refs[] = defaultContext
+                    .getServiceReferences(BusLifeCycleListener.class.getName(), null);
+                if (refs != null) {
+                    for (ServiceReference ref : refs) {
+                        BusLifeCycleListener listener 
+                            = (BusLifeCycleListener)defaultContext.getService(ref);
+                        manager.registerLifeCycleListener(listener);
+                    }
+                }
+            } catch (InvalidSyntaxException e) {
+                //ignore
+            }
+            try {
+                ServiceReference refs[] = defaultContext
+                    .getServiceReferences(ClientLifeCycleListener.class.getName(), null);
+                if (refs != null) {
+                    ClientLifeCycleManager clcm = bus.getExtension(ClientLifeCycleManager.class);
+                    for (ServiceReference ref : refs) {
+                        ClientLifeCycleListener listener 
+                            = (ClientLifeCycleListener)defaultContext.getService(ref);
+                        clcm.registerListener(listener);
+                    }
+                }
+            } catch (InvalidSyntaxException e) {
+                //ignore
+            }
+            try {
+                ServiceReference refs[] = defaultContext
+                    .getServiceReferences(ServerLifeCycleListener.class.getName(), null);
+                if (refs != null) {
+                    ServerLifeCycleManager clcm = bus.getExtension(ServerLifeCycleManager.class);
+                    for (ServiceReference ref : refs) {
+                        ServerLifeCycleListener listener 
+                            = (ServerLifeCycleListener)defaultContext.getService(ref);
+                        clcm.registerListener(listener);
+                    }
+                }
+            } catch (InvalidSyntaxException e) {
+                //ignore
+            }
         }
         private Version getBundleVersion(Bundle bundle) {
             Dictionary headers = bundle.getHeaders();

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=1211645&r1=1211644&r2=1211645&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 Dec  7 21:29:49 2011
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.bus.spring;
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -38,7 +39,6 @@ import org.apache.cxf.bus.extension.Exte
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.configuration.ConfiguredBeanLocator;
-import org.osgi.framework.ServiceReference;
 import org.springframework.beans.Mergeable;
 import org.springframework.beans.PropertyValue;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -154,7 +154,7 @@ public class SpringBeanLocator implement
             ReflectionUtil.setAccessible(m);
             Object o = m.invoke(bundleContext, type.getName());
             if (o != null) {
-                m = contextClass.getMethod("getService", ServiceReference.class);
+                m = contextClass.getMethod("getService", m.getReturnType());
                 ReflectionUtil.setAccessible(m);
                 o = m.invoke(bundleContext, o);
                 lst.add(type.cast(o));
@@ -253,4 +253,39 @@ public class SpringBeanLocator implement
         return orig.hasConfiguredPropertyValue(beanName, propertyName, searchValue);
     }
 
+    public <T> List<T> getOSGiServices(Class<T> type) {
+        List<T> lst = new ArrayList<T>();
+        if (!osgi) {
+            return lst;
+        }
+        
+        Class<?> contextClass = findContextClass(bundleContext.getClass());
+        try {
+            Method m = contextClass.getMethod("getServiceReference", String.class);
+            Class<?> servRefClass = m.getReturnType();
+            m = contextClass.getMethod("getServiceReferences", String.class, String.class);
+            
+            Object o = ReflectionUtil.setAccessible(m).invoke(bundleContext, type.getName(), null);
+            if (o != null) {
+                m = contextClass.getMethod("getService", servRefClass);
+                ReflectionUtil.setAccessible(m);
+                for (int x = 0; x < Array.getLength(o); x++) {
+                    Object ref = Array.get(o, x);
+                    Object o2 = m.invoke(bundleContext, ref);
+                    if (o2 != null) {
+                        lst.add(type.cast(o2));
+                    }
+                }
+            }
+        } catch (NoSuchMethodException e) {
+            //not using OSGi apparently
+            e.printStackTrace();
+        } catch (Throwable e) {
+            //ignore
+            e.printStackTrace();
+            LOG.log(Level.FINE, "Could not get services for " + type.getName(), e);
+        }
+        return lst;
+    }
+
 }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java?rev=1211645&r1=1211644&r2=1211645&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java Wed Dec  7 21:29:49 2011
@@ -19,11 +19,17 @@
 
 package org.apache.cxf.bus.spring;
 
+import java.util.List;
+
+import org.apache.cxf.bus.BusState;
 import org.apache.cxf.bus.extension.ExtensionManagerBus;
 import org.apache.cxf.buslifecycle.BusLifeCycleManager;
 import org.apache.cxf.configuration.ConfiguredBeanLocator;
 import org.apache.cxf.configuration.Configurer;
 import org.apache.cxf.configuration.spring.ConfigurerImpl;
+import org.apache.cxf.feature.AbstractFeature;
+import org.apache.cxf.feature.Feature;
+import org.apache.cxf.feature.WrappedFeature;
 import org.apache.cxf.resource.ResourceManager;
 import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
@@ -46,6 +52,22 @@ public class SpringBus extends Extension
     public SpringBus() {
     }
     
+    public void loadAdditionalFeatures() {
+        super.loadAdditionalFeatures();
+        ConfiguredBeanLocator loc = getExtension(ConfiguredBeanLocator.class);
+        if (loc instanceof SpringBeanLocator) {
+            SpringBeanLocator sloc = (SpringBeanLocator)loc;
+            List<Feature> features = sloc.getOSGiServices(Feature.class);
+            for (Feature feature : features) {
+                if (feature instanceof AbstractFeature) {
+                    this.getFeatures().add((AbstractFeature)feature);
+                } else {
+                    this.getFeatures().add(new WrappedFeature(feature));
+                }
+            }
+        }
+    }
+    
     /** {@inheritDoc}*/
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         ctx = (AbstractApplicationContext)applicationContext;
@@ -71,6 +93,9 @@ public class SpringBus extends Extension
         if (!(loc instanceof SpringBeanLocator)) {
             setExtension(new SpringBeanLocator(applicationContext, this), ConfiguredBeanLocator.class);
         }
+        if (getState() != BusState.RUNNING) {
+            initialize();
+        }
     }
 
     public void onApplicationEvent(ApplicationEvent event) {
@@ -89,7 +114,9 @@ public class SpringBus extends Extension
         
         if (doIt) {
             if (event instanceof ContextRefreshedEvent) {
-                initialize();
+                if (getState() != BusState.RUNNING) {
+                    initialize();
+                }
             } else if (event instanceof ContextClosedEvent) {
                 getExtension(BusLifeCycleManager.class).postShutdown();
             }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/endpoint/ServerImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/endpoint/ServerImpl.java?rev=1211645&r1=1211644&r2=1211645&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/endpoint/ServerImpl.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/endpoint/ServerImpl.java Wed Dec  7 21:29:49 2011
@@ -136,7 +136,12 @@ public class ServerImpl implements Serve
             LOG.fine("register the server to serverRegistry ");
             serverRegistry.register(this);
         }
-        
+        if (slcMgr == null) {
+            slcMgr = bus.getExtension(ServerLifeCycleManager.class);
+            if (slcMgr != null && mep != null) {
+                slcMgr.registerListener(mep);
+            }
+        }
         if (slcMgr != null) {
             slcMgr.startServer(this);
         }