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/10/13 17:52:24 UTC

svn commit: r1182923 - /cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBeanLocator.java

Author: dkulp
Date: Thu Oct 13 15:52:23 2011
New Revision: 1182923

URL: http://svn.apache.org/viewvc?rev=1182923&view=rev
Log:
Fix issues when using Felix it cannot find the services from the context

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

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=1182923&r1=1182922&r2=1182923&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 Thu Oct 13 15:52:23 2011
@@ -30,9 +30,13 @@ import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.bus.extension.ExtensionManagerImpl;
+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;
@@ -47,6 +51,8 @@ import org.springframework.context.Confi
  * 
  */
 public class SpringBeanLocator implements ConfiguredBeanLocator {
+    private static final Logger LOG = LogUtils.getL7dLogger(SpringBeanLocator.class);
+    
     ApplicationContext context;
     ConfiguredBeanLocator orig;
     Set<String> passThroughs = new HashSet<String>();
@@ -142,12 +148,15 @@ public class SpringBeanLocator implement
         try {
             //use a little reflection to allow this to work without the spring-dm jars
             //for the non-osgi cases
+            Class<?> contextClass = findContextClass(bundleContext.getClass());
 
-            Object o = bundleContext.getClass()
-                .getMethod("getServiceReference", String.class).invoke(bundleContext, type.getName());
+            Method m = contextClass.getMethod("getServiceReference", String.class);
+            ReflectionUtil.setAccessible(m);
+            Object o = m.invoke(bundleContext, type.getName());
             if (o != null) {
-                o = bundleContext.getClass().getMethod("getService", ServiceReference.class)
-                    .invoke(bundleContext, o);
+                m = contextClass.getMethod("getService", ServiceReference.class);
+                ReflectionUtil.setAccessible(m);
+                o = m.invoke(bundleContext, o);
                 lst.add(type.cast(o));
             }
         } catch (NoSuchMethodException e) {
@@ -155,7 +164,27 @@ public class SpringBeanLocator implement
             //not using OSGi
         } catch (Throwable e) {
             //ignore
+            LOG.log(Level.WARNING, "Could not get service for " + type.getName(), e);
+        }
+    }
+    private Class<?> findContextClass(Class<?> cls) {
+        for (Class<?> c : cls.getInterfaces()) {
+            if (c.getName().equals("org.osgi.framework.BundleContext")) {
+                return c;
+            }
         }
+        for (Class<?> c : cls.getInterfaces()) {
+            Class<?> c2 = findContextClass(c);
+            if (c2 != null) {
+                return c2;
+            }
+        }
+        Class<?> c2 = findContextClass(cls.getSuperclass());
+        if (c2 != null) {
+            return c2;
+        }
+        
+        return cls;
     }