cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1205586 - in /cxf/branches/2.4.x-fixes: ./ common/common/src/main/java/org/apache/cxf/common/util/ rt/core/src/main/java/org/apache/cxf/bus/blueprint/
Date Wed, 23 Nov 2011 20:53:11 GMT
Author: dkulp
Date: Wed Nov 23 20:53:10 2011
New Revision: 1205586

URL: http://svn.apache.org/viewvc?rev=1205586&view=rev
Log:
Merged revisions 1205521 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1205521 | dkulp | 2011-11-23 13:16:08 -0500 (Wed, 23 Nov 2011) | 4 lines
  
  Update blueprint to support both Aries blueprint 0.3 and an upcoming
  0.4.1+.   (0.4.0 is not usable)
  When possible, use the ComponentMetadata directly to avoid direct
  dependency on Aries for that.
........

Added:
    cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BundleDelegatingClassLoader.java
      - copied unchanged from r1205521, cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BundleDelegatingClassLoader.java
Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
    cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBeanLocator.java
    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/blueprint/ConfigurerImpl.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java?rev=1205586&r1=1205585&r2=1205586&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
(original)
+++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
Wed Nov 23 20:53:10 2011
@@ -28,6 +28,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.net.*;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -226,4 +227,42 @@ public final class ReflectionUtil {
             return beanInfo.getPropertyDescriptors();
         }
     }
+
+    /**
+     * Try to find a method we can use.   If the object implements a public  
+     * interface that has the public version of that method, we'll use the interface
+     * defined method in case the actual instance class is not public 
+     */
+    public static Method findMethod(Class<?> cls,
+                                    String name,
+                                    Class<?> ... params) {
+        if (cls == null) {
+            return null;
+        }
+        for (Class<?> cs : cls.getInterfaces()) {
+            if (Modifier.isPublic(cs.getModifiers())) {
+                Method m = findMethod(cs, name, params);
+                if (m != null && Modifier.isPublic(m.getModifiers())) {
+                    return m;
+                }
+            }
+        }
+        try {
+            Method m = cls.getDeclaredMethod(name, params);
+            if (m != null && Modifier.isPublic(m.getModifiers())) {
+                return m;
+            }
+        } catch (Exception e) {
+            //ignore
+        }
+        Method m = findMethod(cls.getSuperclass(), name, params);
+        if (m == null) {
+            try {
+                m = cls.getMethod(name, params);
+            } catch (Exception e) {
+                //ignore
+            }
+        }
+        return m;
+    }
 }

Modified: cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBeanLocator.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBeanLocator.java?rev=1205586&r1=1205585&r2=1205586&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBeanLocator.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/BlueprintBeanLocator.java
Wed Nov 23 20:53:10 2011
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.bus.blueprint;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -27,16 +28,18 @@ import java.util.List;
 import java.util.Set;
 import java.util.logging.Logger;
 
-import org.apache.aries.blueprint.ExtendedBlueprintContainer;
-import org.apache.aries.blueprint.container.BeanRecipe;
-import org.apache.aries.blueprint.di.ExecutionContext;
-import org.apache.aries.blueprint.di.Recipe;
+import org.apache.aries.blueprint.ExtendedBeanMetadata;
 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.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.container.NoSuchComponentException;
+import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.blueprint.reflect.BeanProperty;
+import org.osgi.service.blueprint.reflect.ComponentMetadata;
 
 /**
  * 
@@ -44,14 +47,14 @@ import org.osgi.service.blueprint.contai
 public class BlueprintBeanLocator implements ConfiguredBeanLocator {
     private static final Logger LOG = LogUtils.getL7dLogger(BlueprintBeanLocator.class);
     ConfiguredBeanLocator orig;
-    ExtendedBlueprintContainer container;
+    BlueprintContainer container;
     BundleContext context;
     
     public BlueprintBeanLocator(ConfiguredBeanLocator orig, 
                                 BlueprintContainer cont, 
                                 BundleContext context) {
         this.orig = orig;
-        this.container = (ExtendedBlueprintContainer)cont;
+        this.container = cont;
         this.context = context;
         if (orig instanceof ExtensionManagerImpl) {
             List<String> names = new ArrayList<String>(container.getComponentIds());
@@ -59,22 +62,54 @@ public class BlueprintBeanLocator implem
         }
     }
     
+    static Class<?> getClassForMetaData(BlueprintContainer container, ComponentMetadata
cmd) {
+        Class<?> cls = null;
+        if (cmd instanceof BeanMetadata) {
+            BeanMetadata bm = (BeanMetadata)cmd;
+            if (bm instanceof ExtendedBeanMetadata) {
+                cls = ((ExtendedBeanMetadata)bm).getRuntimeClass();
+            } 
+            if (cls == null) {
+                try {
+                    Method m = ReflectionUtil.findMethod(container.getClass(), "loadClass",
String.class);
+                    cls = (Class)ReflectionUtil.setAccessible(m).invoke(container, bm.getClassName());
+                } catch (Exception e) {
+                    //ignore
+                }
+            }
+        }
+        return cls;
+    }
+    private Class<?> getClassForMetaData(ComponentMetadata cmd) {
+        return getClassForMetaData(container, cmd);
+    }
+    private ComponentMetadata getComponentMetadata(String id) {
+        try {
+            return container.getComponentMetadata(id);
+        } catch (NoSuchComponentException nsce) {
+            return null;
+        }
+    }
     
+    public <T> T getBeanOfType(String name, Class<T> type) {
+        
+        ComponentMetadata cmd = getComponentMetadata(name);
+        Class<?> cls = getClassForMetaData(cmd);
+        if (cls != null && type.isAssignableFrom(cls)) {
+            return type.cast(container.getComponentInstance(name));
+        }
+        return orig.getBeanOfType(name, type);
+    }
+
     /** {@inheritDoc}*/
     public List<String> getBeanNamesOfType(Class<?> type) {
         Set<String> names = new LinkedHashSet<String>();
-        ExecutionContext origContext 
-            = ExecutionContext.Holder.setContext((ExecutionContext)container.getRepository());
-        try {
-            for (String s : container.getComponentIds()) {
-                Recipe r = container.getRepository().getRecipe(s);
-                if (r instanceof BeanRecipe && ((BeanRecipe)r).getType() != null
-                    && type.isAssignableFrom(((BeanRecipe)r).getType())) {
-                    names.add(s);
-                }
+        for (String s : container.getComponentIds()) {
+            ComponentMetadata cmd = container.getComponentMetadata(s);
+            Class<?> cls = getClassForMetaData(cmd);
+            if (cls != null && type.isAssignableFrom(cls)) {
+                names.add(s);
             }
-        } finally {
-            ExecutionContext.Holder.setContext(origContext);
         }
         names.addAll(orig.getBeanNamesOfType(type));
         return new ArrayList<String>(names);
@@ -84,19 +119,12 @@ public class BlueprintBeanLocator implem
     public <T> Collection<? extends T> getBeansOfType(Class<T> type) {
         List<T> list = new ArrayList<T>();
         
-        ExecutionContext origContext 
-            = ExecutionContext.Holder.setContext((ExecutionContext)container.getRepository());
-        try {
-            for (String s : container.getComponentIds()) {
-                Recipe r = container.getRepository().getRecipe(s);
-                if (r instanceof BeanRecipe && ((BeanRecipe)r).getType() != null
-                    && type.isAssignableFrom(((BeanRecipe)r).getType())) {
-                    
-                    list.add(type.cast(container.getComponentInstance(s)));
-                } 
+        for (String s : container.getComponentIds()) {
+            ComponentMetadata cmd = container.getComponentMetadata(s);
+            Class<?> cls = getClassForMetaData(cmd);
+            if (cls != null && type.isAssignableFrom(cls)) {
+                list.add(type.cast(container.getComponentInstance(s)));
             }
-        } finally {
-            ExecutionContext.Holder.setContext(origContext);
         }
         list.addAll(orig.getBeansOfType(type));
         if (list.isEmpty()) {
@@ -121,51 +149,39 @@ public class BlueprintBeanLocator implem
     public <T> boolean loadBeansOfType(Class<T> type, BeanLoaderListener<T>
listener) {
         List<String> names = new ArrayList<String>();
         boolean loaded = false;
-        ExecutionContext origContext 
-            = ExecutionContext.Holder.setContext((ExecutionContext)container.getRepository());
-        try {
-            for (String s : container.getComponentIds()) {
-                Recipe r = container.getRepository().getRecipe(s);
-                if (r instanceof BeanRecipe && ((BeanRecipe)r).getType() != null
-                    && type.isAssignableFrom(((BeanRecipe)r).getType())) {
-                    names.add(s);
+        for (String s : container.getComponentIds()) {
+            ComponentMetadata cmd = container.getComponentMetadata(s);
+            Class<?> cls = getClassForMetaData(cmd);
+            if (cls != null && type.isAssignableFrom(cls)) {
+                names.add(s);
+            }
+        }
+        Collections.reverse(names);
+        for (String s : names) {
+            ComponentMetadata cmd = container.getComponentMetadata(s);
+            Class<?> beanType = getClassForMetaData(cmd);
+            Class<? extends T> t = beanType.asSubclass(type);
+            if (listener.loadBean(s, t)) {
+                Object o = container.getComponentInstance(s);
+                if (listener.beanLoaded(s, type.cast(o))) {
+                    return true;
                 }
+                loaded = true;
             }
-            Collections.reverse(names);
-            for (String s : names) {
-                BeanRecipe r = (BeanRecipe)container.getRepository().getRecipe(s);
-                Class<?> beanType = r.getType();
-                Class<? extends T> t = beanType.asSubclass(type);
-                if (listener.loadBean(s, t)) {
-                    Object o = container.getComponentInstance(s);
-                    if (listener.beanLoaded(s, type.cast(o))) {
-                        return true;
-                    }
-                    loaded = true;
-                }
-            }
-        } finally {
-            ExecutionContext.Holder.setContext(origContext);
         }
         return loaded || orig.loadBeansOfType(type, listener);
     }
 
     public boolean hasConfiguredPropertyValue(String beanName, String propertyName, String
value) {
-        ExecutionContext origContext 
-            = ExecutionContext.Holder.setContext((ExecutionContext)container.getRepository());
-        try {
-            Recipe r = container.getRepository().getRecipe(beanName);
-            if (r instanceof BeanRecipe) {
-                BeanRecipe br = (BeanRecipe)r;
-                Object o = br.getProperty(propertyName);
-                if (o == null) {
-                    return false;
+        ComponentMetadata cmd = getComponentMetadata(beanName);
+        if (cmd instanceof BeanMetadata) {
+            BeanMetadata br = (BeanMetadata)cmd;
+            for (BeanProperty s : br.getProperties()) {
+                if (propertyName.equals(s.getName())) {
+                    return true;
                 }
-                //TODO - need to check the values of the property
-                return false;
             }
-        } finally {
-            ExecutionContext.Holder.setContext(origContext);
+            return false;
         }
         return orig.hasConfiguredPropertyValue(beanName, propertyName, value);
     }

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=1205586&r1=1205585&r2=1205586&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 Nov 23 20:53:10 2011
@@ -20,7 +20,6 @@
 package org.apache.cxf.bus.blueprint;
 
 
-import org.apache.aries.blueprint.utils.BundleDelegatingClassLoader;
 import org.apache.cxf.bus.extension.ExtensionManagerBus;
 import org.apache.cxf.configuration.ConfiguredBeanLocator;
 import org.apache.cxf.configuration.Configurer;

Modified: cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/ConfigurerImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/ConfigurerImpl.java?rev=1205586&r1=1205585&r2=1205586&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/ConfigurerImpl.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/bus/blueprint/ConfigurerImpl.java
Wed Nov 23 20:53:10 2011
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.bus.blueprint;
 
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -29,20 +30,21 @@ import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.aries.blueprint.ExtendedBlueprintContainer;
-import org.apache.aries.blueprint.container.BeanRecipe;
-import org.apache.aries.blueprint.di.Recipe;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.configuration.Configurable;
 import org.apache.cxf.configuration.Configurer;
 import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.container.NoSuchComponentException;
+import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.blueprint.reflect.ComponentMetadata;
 
 /**
  * 
  */
 public class ConfigurerImpl implements Configurer {
     private static final Logger LOG = LogUtils.getL7dLogger(ConfigurerImpl.class);
-    ExtendedBlueprintContainer container;
+    BlueprintContainer container;
     
     private final Map<String, List<MatcherHolder>> wildCardBeanDefinitions
         = new HashMap<String, List<MatcherHolder>>();
@@ -58,7 +60,7 @@ public class ConfigurerImpl implements C
     
     
     public ConfigurerImpl(BlueprintContainer con) {
-        container = (ExtendedBlueprintContainer)con;
+        container = con;
         initializeWildcardMap();
     }
     private boolean isWildcardBeanName(String bn) {
@@ -69,19 +71,19 @@ public class ConfigurerImpl implements C
     private void initializeWildcardMap() {
         for (String s : container.getComponentIds()) {
             if (isWildcardBeanName(s)) {
-                Recipe r = container.getRepository().getRecipe(s);
-                if (r instanceof BeanRecipe) {
-                    Class c = ((BeanRecipe)r).getType();
+                ComponentMetadata cmd = container.getComponentMetadata(s);
+                Class<?> cls = BlueprintBeanLocator.getClassForMetaData(container,
cmd);
+                if (cls != null) {
                     String orig = s;
                     if (s.charAt(0) == '*') {
                         //old wildcard
                         s = "." + s.replaceAll("\\.", "\\."); 
                     }
                     Matcher matcher = Pattern.compile(s).matcher("");
-                    List<MatcherHolder> m = wildCardBeanDefinitions.get(c.getName());
+                    List<MatcherHolder> m = wildCardBeanDefinitions.get(cls.getName());
                     if (m == null) {
                         m = new ArrayList<MatcherHolder>();
-                        wildCardBeanDefinitions.put(c.getName(), m);
+                        wildCardBeanDefinitions.put(cls.getName(), m);
                     }
                     MatcherHolder holder = new MatcherHolder(orig, matcher);
                     m.add(holder);
@@ -109,9 +111,42 @@ public class ConfigurerImpl implements C
             configureWithWildCard(bn, beanInstance);
         }
         
-        Recipe r = container.getRepository().getRecipe(bn);
-        if (r instanceof BeanRecipe) {
-            ((BeanRecipe)r).setProperties(beanInstance);
+        
+        Method m = ReflectionUtil.findMethod(container.getClass(), "injectBeanInstance",
+                                             BeanMetadata.class, Object.class);
+        try {
+            if (m != null) {
+                //Aries blueprint 0.4.1+
+                ComponentMetadata cm = null;
+                try {
+                    cm = container.getComponentMetadata(bn);
+                } catch (NoSuchComponentException nsce) {
+                    cm = null;
+                }
+                if (cm instanceof BeanMetadata) {
+                    ReflectionUtil.setAccessible(m);
+                    m.invoke(container, (BeanMetadata)cm, beanInstance);
+                }
+            } else {
+                //Aries blueprint 0.3.x
+                m = ReflectionUtil.findMethod(container.getClass(), "getRepository");
+                Object o = ReflectionUtil.setAccessible(m).invoke(container);
+                m = ReflectionUtil.findMethod(container.getClass(), "getRecipe", String.class);
+                o = ReflectionUtil.setAccessible(m).invoke(o, bn);  //returns the recipe
+                m = ReflectionUtil.findMethod(o.getClass(), "setProperties", Object.class);
+                if (m != null) {
+                    ReflectionUtil.setAccessible(m).invoke(o, beanInstance);
+                }
+            }
+        } catch (InvocationTargetException ite) {
+            Throwable t = ite.getCause();
+            if (t instanceof RuntimeException) {
+                throw (RuntimeException)t;
+            } else {
+                throw new RuntimeException(t);
+            }
+        } catch (Exception ex) {
+            LOG.log(Level.FINE, "Could not configure object " + bn, ex);
         }
     }
     private void configureWithWildCard(String bn, Object beanInstance) {



Mime
View raw message