incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r979336 - in /incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services: CgLibProxyFactory.java JdkProxyFactory.java ProxyFactory.java ServiceHelper.java
Date Mon, 26 Jul 2010 16:04:24 GMT
Author: gawor
Date: Mon Jul 26 16:04:24 2010
New Revision: 979336

URL: http://svn.apache.org/viewvc?rev=979336&view=rev
Log:
ARIES-356: Make sure classloader for cglib proxies can load cglib classes. Also refector the
jdk/cglib proxy code into separate classes.

Added:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
  (with props)
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
  (with props)
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
  (with props)
Modified:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java?rev=979336&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
Mon Jul 26 16:04:24 2010
@@ -0,0 +1,97 @@
+/*
+ * 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.aries.jndi.services;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Callable;
+
+import net.sf.cglib.proxy.Dispatcher;
+import net.sf.cglib.proxy.Enhancer;
+
+import org.apache.aries.util.BundleToClassLoaderAdapter;
+import org.osgi.framework.Bundle;
+
+public class CgLibProxyFactory implements ProxyFactory {
+
+    public Object createProxy(final Bundle bundle,
+                              final Class[] classes,
+                              final Callable<Object> dispatcher) {
+        Enhancer e = new Enhancer();
+        e.setClassLoader(new CgLibClassLoader(bundle));
+        e.setSuperclass(getTargetClass(classes));
+        e.setInterfaces(getInterfaces(classes));
+        e.setInterceptDuringConstruction(false);
+        e.setCallback(new Dispatcher() {
+            public Object loadObject() throws Exception {
+                return dispatcher.call();
+            }
+        });
+        e.setUseFactory(false);
+        return e.create();
+    }
+
+    private static Class<?>[] getInterfaces(Class<?>[] classes) {
+        Set<Class<?>> interfaces = new HashSet<Class<?>>();
+        for (Class<?> clazz : classes) {
+            if (clazz.isInterface()) {
+                interfaces.add(clazz);
+            }
+        }
+        return interfaces.toArray(new Class[interfaces.size()]);
+    }
+
+    protected Class<?> getTargetClass(Class<?>[] interfaceNames) {
+        // Only allow class proxying if specifically asked to
+        Class<?> root = Object.class;
+        for (Class<?> clazz : interfaceNames) {
+            if (!clazz.isInterface()) {
+                if (root.isAssignableFrom(clazz)) {
+                    root = clazz;
+                } else if (clazz.isAssignableFrom(root)) {
+                    // nothing to do, root is correct
+                } else {
+                    throw new IllegalArgumentException("Classes " + root.getClass().getName()
+                                                       + " and " + clazz.getName()
+                                                       + " are not in the same hierarchy");
+                }
+            }
+        }
+        return root;
+    }
+
+    private static class CgLibClassLoader extends BundleToClassLoaderAdapter {
+
+        private ClassLoader cgLibClassLoader;
+
+        public CgLibClassLoader(Bundle bundle) {
+            super(bundle);
+            this.cgLibClassLoader = Enhancer.class.getClassLoader();
+        }
+
+        @Override
+        public Class<?> loadClass(final String name) throws ClassNotFoundException
{
+            if (name.startsWith("net.sf.cglib")) {
+                return cgLibClassLoader.loadClass(name);
+            } else {
+                return super.loadClass(name);
+            }
+        }
+    }
+}

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java?rev=979336&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
Mon Jul 26 16:04:24 2010
@@ -0,0 +1,47 @@
+/*
+ * 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.aries.jndi.services;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.concurrent.Callable;
+
+import org.apache.aries.util.BundleToClassLoaderAdapter;
+import org.osgi.framework.Bundle;
+
+public class JdkProxyFactory implements ProxyFactory {
+
+    public Object createProxy(final Bundle bundle,
+                              final Class[] classes,
+                              final Callable<Object> dispatcher) {
+        return Proxy.newProxyInstance(new BundleToClassLoaderAdapter(bundle), classes,
+                new InvocationHandler() {
+                    public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
+                        try {
+                            return method.invoke(dispatcher.call(), args);
+                        } catch (InvocationTargetException ite) {
+                            throw ite.getTargetException();
+                        }
+                    }
+                });
+    }
+
+}

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java?rev=979336&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
Mon Jul 26 16:04:24 2010
@@ -0,0 +1,29 @@
+/*
+ * 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.aries.jndi.services;
+
+import java.util.concurrent.Callable;
+
+import org.osgi.framework.Bundle;
+
+public interface ProxyFactory {
+
+    Object createProxy(Bundle bundle, Class[] classes, Callable<Object> dispatcher);
+
+}

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java?rev=979336&r1=979335&r2=979336&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
Mon Jul 26 16:04:24 2010
@@ -19,32 +19,21 @@
 package org.apache.aries.jndi.services;
 
 import java.lang.ref.WeakReference;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.lang.reflect.Proxy;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Comparator;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import javax.naming.NamingException;
 
-import net.sf.cglib.proxy.Dispatcher;
-import net.sf.cglib.proxy.Enhancer;
 import org.apache.aries.jndi.url.OsgiName;
-import org.apache.aries.util.BundleToClassLoaderAdapter;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -345,8 +334,9 @@ public final class ServiceHelper
     // wrapping the service provider bundle. The class is actually defined
     // on this adapter.
 
+    Class[] classArray = clazz.toArray(new Class[clazz.size()]);
     try {
-      return proxyFactory.createProxy(new BundleToClassLoaderAdapter(serviceProviderBundle),
toClassArray(clazz), ih);
+      return proxyFactory.createProxy(serviceProviderBundle, classArray, ih);
     } catch (IllegalArgumentException e) {
       throw e;
     } catch (RuntimeException e) {
@@ -452,87 +442,5 @@ public final class ServiceHelper
     }
     return proxyFactory;
   }
-
-  private static Class<?>[] getInterfaces(Class<?>[] classes) {
-    Set<Class<?>> interfaces = new HashSet<Class<?>>();
-    for (Class<?> clazz : classes) {
-      if (clazz.isInterface()) {
-        interfaces.add(clazz);
-      }
-    }
-    return toClassArray(interfaces);
-  }
-
-  private static Class<?>[] toClassArray(Collection<Class<?>> classes)
{
-    return classes.toArray(new Class[classes.size()]);
-  }
-
-  public static interface ProxyFactory {
-
-    Object createProxy(ClassLoader classLoader, Class[] classes, Callable<Object> dispatcher);
-
-    boolean proxiesClasses();
-  }
-
-  public static class JdkProxyFactory implements ProxyFactory {
-
-    public Object createProxy(final ClassLoader classLoader, final Class[] classes, final
Callable<Object> dispatcher) {
-      return Proxy.newProxyInstance(classLoader, classes, new InvocationHandler() {
-        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
-          try {
-            return method.invoke(dispatcher.call(), args);
-          } catch (InvocationTargetException ite) {
-            throw ite.getTargetException();
-          }
-        }
-      });
-    }
-
-    public boolean proxiesClasses() {
-      return false;
-    }
-
-  }
-
-  public static class CgLibProxyFactory implements ProxyFactory {
-
-    public Object createProxy(final ClassLoader classLoader, final Class[] classes, final
Callable<Object> dispatcher) {
-      Enhancer e = new Enhancer();
-      e.setClassLoader(classLoader);
-      e.setSuperclass(getTargetClass(classes));
-      e.setInterfaces(getInterfaces(classes));
-      e.setInterceptDuringConstruction(false);
-      e.setCallback(new Dispatcher() {
-        public Object loadObject() throws Exception {
-          return dispatcher.call();
-        }
-      });
-      e.setUseFactory(false);
-      return e.create();
-    }
-
-    public boolean proxiesClasses() {
-      return true;
-    }
-
-    protected Class<?> getTargetClass(Class<?>[] interfaceNames) {
-      // Only allow class proxying if specifically asked to
-      Class<?> root = Object.class;
-      for (Class<?> clazz : interfaceNames) {
-        if (!clazz.isInterface()) {
-          if (root.isAssignableFrom(clazz)) {
-            root = clazz;
-          } else if (clazz.isAssignableFrom(root)) {
-            //nothing to do, root is correct
-          } else {
-            throw new IllegalArgumentException("Classes " + root.getClass().getName() + "
and " + clazz.getName() + " are not in the same hierarchy");
-          }
-        }
-      }
-      return root;
-    }
-
-  }
-
-
+ 
 }



Mime
View raw message