geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel ConfigTest.java MockEndpoint.java MockGBean.java
Date Sat, 17 Jan 2004 00:32:10 GMT
dain        2004/01/16 16:32:10

  Modified:    modules/kernel/src/java/org/apache/geronimo/gbean/jmx
                        GBeanMBeanOperation.java
               modules/kernel/src/test/org/apache/geronimo/kernel
                        ConfigTest.java MockEndpoint.java MockGBean.java
  Log:
  Added support for primitive parameter types.
  
  Revision  Changes    Path
  1.3       +145 -2    incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/gbean/jmx/GBeanMBeanOperation.java
  
  Index: GBeanMBeanOperation.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/gbean/jmx/GBeanMBeanOperation.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GBeanMBeanOperation.java	16 Jan 2004 23:31:21 -0000	1.2
  +++ GBeanMBeanOperation.java	17 Jan 2004 00:32:10 -0000	1.3
  @@ -57,9 +57,12 @@
   
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  +import java.lang.reflect.Array;
   import java.util.Collections;
   import java.util.List;
   import java.util.ArrayList;
  +import java.util.Map;
  +import java.util.HashMap;
   import javax.management.MBeanException;
   import javax.management.MBeanOperationInfo;
   import javax.management.MBeanParameterInfo;
  @@ -115,7 +118,7 @@
           for (int i = 0; i < types.length; i++) {
               String type = (String) parameterTypes.get(i);
               try {
  -                types[i] = classLoader.loadClass((String)parameterTypes.get(i));
  +                types[i] = loadClass((String)parameterTypes.get(i), classLoader);
               } catch (ClassNotFoundException e) {
                   throw new InvalidConfigurationException("Could not load operation parameter
class:" +
                           " name=" + operationInfo.getName() +
  @@ -179,5 +182,145 @@
           } finally {
               Thread.currentThread().setContextClassLoader(oldClassLoader);
           }
  +    }
  +
  +    /**
  +     * Load a class for the given name.
  +     *
  +     * <p>Handles loading primitive types as well as VM class and array syntax.
  +     *
  +     * @param className the name of the Class to be loaded
  +     * @param classLoader the class loader to load the Class object from
  +     * @return the Class object for the given name
  +     *
  +     * @throws ClassNotFoundException   if classloader could not locate the specified class
  +     */
  +    private static Class loadClass(final String className, final ClassLoader classLoader)
throws ClassNotFoundException {
  +        if (className == null) {
  +            throw new IllegalArgumentException("Class name is null");
  +        }
  +        if (classLoader == null) {
  +            throw new IllegalArgumentException("Class loader is null");
  +        }
  +
  +        // First just try to load
  +        try {
  +            return classLoader.loadClass(className);
  +        } catch (ClassNotFoundException ignore) {
  +            // handle special cases below
  +        }
  +
  +        Class type = null;
  +
  +        // Check if it is a primitive type
  +        type = getPrimitiveType(className);
  +        if (type != null) return type;
  +
  +        // Check if it is a vm primitive
  +        type = getVMPrimitiveType(className);
  +        if (type != null) return type;
  +
  +        // Handle VM class syntax (Lclassname;)
  +        if (className.charAt(0) == 'L' && className.charAt(className.length() -
1) == ';') {
  +            return classLoader.loadClass(className.substring(1, className.length() - 1));
  +        }
  +
  +        // Handle VM array syntax ([type)
  +        if (className.charAt(0) == '[') {
  +            int arrayDimension = className.lastIndexOf('[') + 1;
  +            String componentClassName = className.substring(arrayDimension, className.length());
  +            type = loadClass(componentClassName, classLoader);
  +
  +            int dim[] = new int[arrayDimension];
  +            java.util.Arrays.fill(dim, 0);
  +            return Array.newInstance(type, dim).getClass();
  +        }
  +
  +        // Handle user friendly type[] syntax
  +        if (className.endsWith("[]")) {
  +            // get the base component class name and the arrayDimensions
  +            int arrayDimension = 0;
  +            String componentClassName = className;
  +            while (componentClassName.endsWith("[]")) {
  +                componentClassName = componentClassName.substring(0, componentClassName.length()
- 2);
  +                arrayDimension++;
  +            }
  +
  +            // load the base type
  +            type = loadClass(componentClassName, classLoader);
  +
  +            // return the array type
  +            int[] dim = new int[arrayDimension];
  +            java.util.Arrays.fill(dim, 0);
  +            return Array.newInstance(type, dim).getClass();
  +        }
  +
  +        // Else we can not load (give up)
  +        throw new ClassNotFoundException(className);
  +    }
  +
  +    /** Primitive type name -> class map. */
  +    private static final Map PRIMITIVES = new HashMap();
  +
  +    /** Setup the primitives map. */
  +    static {
  +        PRIMITIVES.put("boolean", Boolean.TYPE);
  +        PRIMITIVES.put("byte", Byte.TYPE);
  +        PRIMITIVES.put("char", Character.TYPE);
  +        PRIMITIVES.put("short", Short.TYPE);
  +        PRIMITIVES.put("int", Integer.TYPE);
  +        PRIMITIVES.put("long", Long.TYPE);
  +        PRIMITIVES.put("float", Float.TYPE);
  +        PRIMITIVES.put("double", Double.TYPE);
  +        PRIMITIVES.put("void", Void.TYPE);
  +    }
  +
  +    /**
  +     * Get the primitive type for the given primitive name.
  +     *
  +     * @param name    Primitive type name (boolean, byte, int, ...)
  +     * @return        Primitive type or null.
  +     */
  +    private static Class getPrimitiveType(final String name) {
  +        return (Class) PRIMITIVES.get(name);
  +    }
  +
  +    /** VM primitive type name -> primitive type */
  +    private static final HashMap VM_PRIMITIVES = new HashMap();
  +
  +    /** Setup the vm primitives map. */
  +    static {
  +        VM_PRIMITIVES.put("B", byte.class);
  +        VM_PRIMITIVES.put("C", char.class);
  +        VM_PRIMITIVES.put("D", double.class);
  +        VM_PRIMITIVES.put("F", float.class);
  +        VM_PRIMITIVES.put("I", int.class);
  +        VM_PRIMITIVES.put("J", long.class);
  +        VM_PRIMITIVES.put("S", short.class);
  +        VM_PRIMITIVES.put("Z", boolean.class);
  +        VM_PRIMITIVES.put("V", void.class);
  +    }
  +
  +    /**
  +     * Get the primitive type for the given VM primitive name.
  +     *
  +     * <p>Mapping:
  +     * <pre>
  +     *   B - byte
  +     *   C - char
  +     *   D - double
  +     *   F - float
  +     *   I - int
  +     *   J - long
  +     *   S - short
  +     *   Z - boolean
  +     *   V - void
  +     * </pre>
  +     *
  +     * @param name    VM primitive type name (B, C, J, ...)
  +     * @return        Primitive type or null.
  +     */
  +    private static Class getVMPrimitiveType(final String name) {
  +        return (Class) VM_PRIMITIVES.get(name);
       }
   }
  
  
  
  1.7       +4 -1      incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/ConfigTest.java
  
  Index: ConfigTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/ConfigTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ConfigTest.java	17 Jan 2004 00:14:22 -0000	1.6
  +++ ConfigTest.java	17 Jan 2004 00:32:10 -0000	1.7
  @@ -119,6 +119,9 @@
           mbServer.setAttribute(gbeanName2, new Attribute("MutableInt", new Integer(44)));
           assertEquals(new Integer(44), mbServer.getAttribute(gbeanName2, "MutableInt"));
   
  +        mbServer.invoke(gbeanName2, "doSetMutableInt", new Object[] {new Integer(55)},
new String[] {"int"});
  +        assertEquals(new Integer(55), mbServer.getAttribute(gbeanName2, "MutableInt"));
  +
           assertEquals("no endpoint", mbServer.invoke(gbeanName1, "checkEndpoint", null,
null));
           assertEquals("endpointCheck", mbServer.invoke(gbeanName2, "checkEndpoint", null,
null));
   
  
  
  
  1.3       +3 -1      incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/MockEndpoint.java
  
  Index: MockEndpoint.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/MockEndpoint.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MockEndpoint.java	17 Jan 2004 00:14:22 -0000	1.2
  +++ MockEndpoint.java	17 Jan 2004 00:32:10 -0000	1.3
  @@ -12,6 +12,8 @@
   
       int getMutableInt();
   
  +    void doSetMutableInt(int mutableInt);
  +
       void setMutableInt(int mutableInt);
   
   }
  
  
  
  1.6       +6 -1      incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/MockGBean.java
  
  Index: MockGBean.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/MockGBean.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MockGBean.java	17 Jan 2004 00:14:22 -0000	1.5
  +++ MockGBean.java	17 Jan 2004 00:32:10 -0000	1.6
  @@ -90,6 +90,7 @@
           infoFactory.addOperation(new GOperationInfo("checkResource", new String[]{"java.lang.String"}));
           infoFactory.addOperation(new GOperationInfo("checkEndpoint"));
           infoFactory.addOperation(new GOperationInfo("doSomething", new String[]{"java.lang.String"}));
  +        infoFactory.addOperation(new GOperationInfo("doSetMutableInt", new String[] {"int"}));
           infoFactory.addEndpoint(new GEndpointInfo("MockEndpoint", MockEndpoint.class.getName()));
           infoFactory.setConstructor(new GConstructorInfo(new String[]{"Name", "FinalInt"},
new Class[]{String.class, Integer.TYPE}));
           GBEAN_INFO = infoFactory.getBeanInfo();
  @@ -110,6 +111,10 @@
   
       public int getMutableInt() {
           return mutableInt;
  +    }
  +
  +    public void doSetMutableInt(int mutableInt) {
  +        setMutableInt(mutableInt);
       }
   
       public void setMutableInt(int mutableInt) {
  
  
  

Mime
View raw message