geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r390512 - in /geronimo/branches/1.1/modules/kernel/src: java/org/apache/geronimo/kernel/basic/BasicProxyManager.java test/org/apache/geronimo/kernel/GBeanTest.java
Date Fri, 31 Mar 2006 21:57:04 GMT
Author: dain
Date: Fri Mar 31 13:57:02 2006
New Revision: 390512

URL: http://svn.apache.org/viewcvs?rev=390512&view=rev
Log:
Basic proxy manager will no longer attempt to create an auto-proxy using a base class without
a default constructor.
Basic proxy manager now thows an IllegalArgumentException if a caller attempts to create a
proxy or proxy factory using a class without a default constructor.

Modified:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/basic/BasicProxyManager.java
    geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/basic/BasicProxyManager.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/basic/BasicProxyManager.java?rev=390512&r1=390511&r2=390512&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/basic/BasicProxyManager.java
(original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/basic/BasicProxyManager.java
Fri Mar 31 13:57:02 2006
@@ -40,6 +40,8 @@
 import java.util.Map;
 import java.util.Collections;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Modifier;
 
 /**
  * Creates proxies that communicate directly with a Kernel located in the same
@@ -68,6 +70,9 @@
      */
     public ProxyFactory createProxyFactory(Class type) {
         if (type == null) throw new NullPointerException("type is null");
+        if (!type.isInterface() && !hasDefaultConstructor(type)) {
+            throw new IllegalArgumentException("Type class does not have a default constructor
" + type.getName());
+        }
 
         ClassLoader classLoader = type.getClassLoader();
         if(classLoader == null) {
@@ -131,15 +136,18 @@
                 return null;
             }
             String[] names = (String[]) interfaces.toArray(new String[0]);
-            List intfs = new ArrayList();
+            List types = new ArrayList();
             for (int i = 0; i < names.length; i++) {
                 try {
-                    intfs.add(classLoader.loadClass(names[i]));
+                    Class type = classLoader.loadClass(names[i]);
+                    if (type.isInterface() || hasDefaultConstructor(type)) {
+                        types.add(type);
+                    }
                 } catch (ClassNotFoundException e) {
                     log.warn("Could not load interface "+names[i]+" in provided ClassLoader
for "+target.getKeyProperty("name"));
                 }
             }
-            return createProxyFactory((Class[]) intfs.toArray(new Class[intfs.size()]), classLoader).createProxy(target);
+            return createProxyFactory((Class[]) types.toArray(new Class[types.size()]), classLoader).createProxy(target);
         } catch (GBeanNotFoundException e) {
             throw new IllegalArgumentException("Could not get GBeanInfo for target object:
" + target);
         }
@@ -157,15 +165,18 @@
                 return null;
             }
             String[] names = (String[]) interfaces.toArray(new String[0]);
-            List intfs = new ArrayList();
+            List types = new ArrayList();
             for (int i = 0; i < names.length; i++) {
                 try {
-                    intfs.add(classLoader.loadClass(names[i]));
+                    Class type = classLoader.loadClass(names[i]);
+                    if (type.isInterface() || hasDefaultConstructor(type)) {
+                        types.add(type);
+                    }
                 } catch (ClassNotFoundException e) {
                     log.warn("Could not load interface "+names[i]+" in provided ClassLoader
for "+target);
                 }
             }
-            return createProxyFactory((Class[]) intfs.toArray(new Class[intfs.size()]), classLoader).createProxy(target);
+            return createProxyFactory((Class[]) types.toArray(new Class[types.size()]), classLoader).createProxy(target);
         } catch (GBeanNotFoundException e) {
             throw new IllegalArgumentException("Could not get GBeanInfo for target object:
" + target);
         }
@@ -317,5 +328,21 @@
     private AbstractName getAbstractName(ObjectName objectName, Kernel kernel) throws GBeanNotFoundException
{
         GBeanData gBeanData = kernel.getGBeanData(objectName);
         return gBeanData.getAbstractName();
+    }
+
+    public static boolean hasDefaultConstructor(Class type) {
+        if (!Modifier.isPublic(type.getModifiers())) {
+            return false;
+        }
+        Constructor[] constructors = type.getDeclaredConstructors();
+        for (int i = 0; i < constructors.length; i++) {
+            Constructor constructor = constructors[i];
+            boolean accessible = Modifier.isPublic(constructor.getModifiers()) ||
+                    Modifier.isProtected(constructor.getModifiers());
+            if (accessible && constructor.getParameterTypes().length == 0) {
+                return true;
+            }
+        }
+        return false;
     }
 }

Modified: geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java?rev=390512&r1=390511&r2=390512&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java
(original)
+++ geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java
Fri Mar 31 13:57:02 2006
@@ -21,20 +21,21 @@
 import java.net.URLClassLoader;
 import java.util.Set;
 
+import junit.extensions.TestSetup;
+import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
-import junit.framework.Test;
-import junit.extensions.TestSetup;
 import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.gbean.AbstractNameQuery;
 import org.apache.geronimo.gbean.GBeanData;
 import org.apache.geronimo.gbean.GBeanInfo;
+import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.kernel.jmx.KernelDelegate;
+import org.apache.geronimo.kernel.jmx.MBeanServerDelegate;
 import org.apache.geronimo.kernel.management.State;
 import org.apache.geronimo.kernel.proxy.ProxyFactory;
 import org.apache.geronimo.kernel.proxy.ProxyManager;
 import org.apache.geronimo.kernel.repository.Artifact;
-import org.apache.geronimo.kernel.jmx.KernelDelegate;
-import org.apache.geronimo.kernel.jmx.MBeanServerDelegate;
 import org.apache.log4j.ConsoleAppender;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
@@ -514,6 +515,34 @@
         }
     }
 
+    public void testProxyClass() throws Exception {
+        ClassLoader cl = getClass().getClassLoader();
+        ClassLoader myCl = new URLClassLoader(new URL[0], cl);
+
+        GBeanData gbean = buildGBeanData("name", "test", ClassGBean.getGBeanInfo());
+        gbean.setAttribute("name", "Test");
+        kernel.loadGBean(gbean, myCl);
+        kernel.startGBean(gbean.getAbstractName());
+        ProxyManager mgr = kernel.getProxyManager();
+
+        Object test = mgr.createProxy(gbean.getAbstractName(), Named.class);
+        assertTrue(test instanceof Named);
+        assertFalse(test instanceof ClassGBean);
+        assertEquals("Test", ((Named)test).getName());
+
+        test = mgr.createProxy(gbean.getAbstractName(), myCl);
+        assertTrue(test instanceof Named);
+        assertFalse(test instanceof ClassGBean);
+        assertEquals("Test", ((Named)test).getName());
+
+        try {
+            mgr.createProxy(gbean.getAbstractName(), ClassGBean.class);
+            fail("expected an IllegalArgumentException");
+        } catch (IllegalArgumentException ignored) {
+            // expected
+        }
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout("%p [%t]
%m %n")));
@@ -533,5 +562,34 @@
     private GBeanData buildGBeanData(String name, String type, GBeanInfo info) {
         AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test",
"foo", "1", "car"), name, type);
         return new GBeanData(abstractName, info);
+    }
+
+    public static interface Named {
+        String getName();
+    }
+
+    public static class ClassGBean implements Named {
+        private final String name;
+
+        public ClassGBean(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        private static final GBeanInfo GBEAN_INFO;
+
+        static {
+            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ClassGBean.class);
+            infoFactory.setConstructor(new String[]{"name"});
+
+            GBEAN_INFO = infoFactory.getBeanInfo();
+        }
+
+        public static GBeanInfo getGBeanInfo() {
+            return GBEAN_INFO;
+        }
     }
 }



Mime
View raw message