geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r677814 - in /geronimo/server/trunk: framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/ framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/ plugins/connector/geronimo-connector-builder/src/main...
Date Fri, 18 Jul 2008 03:55:12 GMT
Author: gawor
Date: Thu Jul 17 20:55:11 2008
New Revision: 677814

URL: http://svn.apache.org/viewvc?rev=677814&view=rev
Log:
Improve connector deployment and dynamic gbean handling (GERONIMO-4131)

Added:
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java
  (with props)
Modified:
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java
    geronimo/server/trunk/plugins/connector/geronimo-connector-builder/src/main/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilder.java

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java?rev=677814&r1=677813&r2=677814&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java
Thu Jul 17 20:55:11 2008
@@ -37,6 +37,19 @@
  * @version $Rev$ $Date$
  */
 public class DynamicGBeanDelegate implements DynamicGBean {
+    
+    private static final Map<Class, Class> TYPE_LOOKUP = new HashMap<Class, Class>();
+    static {
+        TYPE_LOOKUP.put(Byte.class, byte.class);
+        TYPE_LOOKUP.put(Integer.class, int.class);
+        TYPE_LOOKUP.put(Short.class, short.class);
+        TYPE_LOOKUP.put(Long.class, long.class);
+        TYPE_LOOKUP.put(Float.class, float.class);
+        TYPE_LOOKUP.put(Double.class, double.class);
+        TYPE_LOOKUP.put(Boolean.class, boolean.class);
+        TYPE_LOOKUP.put(Character.class, char.class);
+    }
+    
     protected final Map getters = new HashMap();
     protected final Map setters = new HashMap();
     protected final Map operations = new HashMap();
@@ -89,12 +102,23 @@
         if (!(method.getParameterTypes().length == 1 && method.getReturnType() ==
Void.TYPE)) {
             throw new IllegalArgumentException("Method must take one parameter and not return
anything " + method);
         }
-        setters.put(name, new Operation(target, method));
+        Class type = method.getParameterTypes()[0];
+        Operation operation = new Operation(target, method);
+        addSetter(name, type, operation);        
         // we want to be user-friendly so we put the attribute name in
         // the Map in both lower-case and upper-case
-        setters.put(Introspector.decapitalize(name), new Operation(target, method));
+        addSetter(Introspector.decapitalize(name), type, operation);    
     }
 
+    private void addSetter(String name, Class type, Operation operation) {
+        Map<Class, Operation> operations = (Map<Class, Operation>)setters.get(name);
+        if (operations == null) {
+            operations = new HashMap<Class, Operation>();
+            setters.put(name, operations);
+        }
+        operations.put(type, operation);
+    }
+    
     public void addOperation(Object target, Method method) {
         Class[] parameters = method.getParameterTypes();
         String[] types = new String[parameters.length];
@@ -127,13 +151,45 @@
     }
 
     public void setAttribute(String name, Object value) throws Exception {
-        Operation operation = (Operation) setters.get(name);
-        if (operation == null) {
-            throw new IllegalArgumentException(targetClass.getName() + ": no setter for "
+ name);
+        Map<Class, Operation> operations = (Map<Class, Operation>)setters.get(name);
+        if (operations == null) {
+            throw new IllegalArgumentException(targetClass.getName() + ": no setters for
" + name);
+        }        
+        Operation operation = null;                   
+        if (operations.size() == 1) {
+            operation = operations.values().iterator().next();
+        } else if (value == null) {
+            // TODO: use better algorithm?
+            operation = operations.values().iterator().next();
+        } else if (value != null) {
+            Class valueType = value.getClass();
+            // lookup using the specific type
+            operation = operations.get(valueType);
+            if (operation == null) {
+                // if not found, check all setters if they accept the given type  
+                operation = findOperation(operations, valueType);
+                if (operation == null && TYPE_LOOKUP.containsKey(valueType)) {
+                    // if not found, check all setters if they accept the primitive type
+                    operation = findOperation(operations, TYPE_LOOKUP.get(valueType));
+                }
+                if (operation == null) {
+                    throw new IllegalArgumentException(targetClass.getName() + ": no setter
for " + name + " of type " + valueType);
+                }
+            }            
         }
+        
         operation.invoke(new Object[]{value});
     }
 
+    private Operation findOperation(Map<Class, Operation> operations, Class type) {
+        for (Map.Entry<Class, Operation> entry : operations.entrySet()) {
+            if (entry.getKey().isAssignableFrom(type)) {
+                return entry.getValue();                
+            }                    
+        }
+        return null;
+    }
+    
     public Object invoke(String name, Object[] arguments, String[] types) throws Exception
{
         GOperationSignature signature = new GOperationSignature(name, types);
         Operation operation = (Operation) operations.get(signature);

Added: geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java?rev=677814&view=auto
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java
(added)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java
Thu Jul 17 20:55:11 2008
@@ -0,0 +1,130 @@
+/**
+ *  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.geronimo.gbean;
+
+import java.io.Serializable;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
+ */
+public class DynamicGBeanDelegateTest extends TestCase {
+    
+    public void testSimple() throws Exception {
+        DynamicGBeanDelegate delegate = new DynamicGBeanDelegate();
+        Bean bean = new Bean();
+        delegate.addAll(bean);
+        
+        Integer value;
+        
+        delegate.setAttribute("foo", 1);
+        value = (Integer)delegate.getAttribute("foo");
+        assertEquals(1, value.intValue());
+        
+        delegate.setAttribute("foo", new Integer(2));
+        value = (Integer)delegate.getAttribute("foo");
+        assertEquals(2, value.intValue());
+        
+        delegate.setAttribute("foo", "3");
+        value = (Integer)delegate.getAttribute("foo");
+        assertEquals(3, value.intValue());
+    }
+   
+    public void testPolymorphism() throws Exception {
+        DynamicGBeanDelegate delegate = new DynamicGBeanDelegate();
+        Bean bean = new Bean();
+        delegate.addAll(bean);
+        
+        Bean expected1 = new Bean();
+        delegate.setAttribute("bean", expected1);
+        Bean actual1 = (Bean)delegate.getAttribute("bean");
+        assertEquals(expected1, actual1);
+        
+        String expected2 = new String();
+        delegate.setAttribute("bean", expected2);
+        String actual2 = (String)delegate.getAttribute("bean");
+        assertEquals(expected2, actual2);
+    }
+    
+    public void testBoxing() throws Exception {
+        DynamicGBeanDelegate delegate = new DynamicGBeanDelegate();
+        Bean bean = new Bean();
+        delegate.addAll(bean);
+        
+        Integer value;
+        
+        delegate.setAttribute("bar", 1);
+        value = (Integer)delegate.getAttribute("bar");
+        assertEquals(1, value.intValue());
+        
+        delegate.setAttribute("bar", new Integer(2));
+        value = (Integer)delegate.getAttribute("bar");
+        assertEquals(2, value.intValue());
+        
+        delegate.setAttribute("bar", "3");
+        value = (Integer)delegate.getAttribute("bar");
+        assertEquals(3, value.intValue());
+    }
+    
+    private static class Bean implements Serializable {
+        private int foo;
+        private int bar;
+        private Serializable bean;
+        
+        public void setFoo(String s) {
+            setFoo(Integer.parseInt(s));
+        }
+        
+        public void setFoo(Integer i) {
+            setFoo(i.intValue());
+        }
+        
+        public void setFoo(int i) {
+            this.foo = i;
+        }
+        
+        public int getFoo() {
+            return this.foo;
+        }
+                
+        public void setBean(Bean b) {
+            this.bean = b;
+        }
+        
+        public void setBean(Serializable s) {
+            this.bean = s;
+        }
+        
+        public Serializable getBean() {
+            return this.bean;
+        }
+        
+        public void setBar(String b) {
+            setBar(Integer.parseInt(b));
+        }
+        
+        public void setBar(int b) {
+            this.bar = b;
+        }
+        
+        public int getBar() {
+            return this.bar;
+        }        
+    }
+}
+

Propchange: geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/gbean/DynamicGBeanDelegateTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: geronimo/server/trunk/plugins/connector/geronimo-connector-builder/src/main/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/connector/geronimo-connector-builder/src/main/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilder.java?rev=677814&r1=677813&r2=677814&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/connector/geronimo-connector-builder/src/main/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilder.java
(original)
+++ geronimo/server/trunk/plugins/connector/geronimo-connector-builder/src/main/java/org/apache/geronimo/connector/deployment/ConnectorModuleBuilder.java
Thu Jul 17 20:55:11 2008
@@ -138,6 +138,27 @@
         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector-1.1", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2");
     }
 
+    private static final Map<String, Class> TYPE_LOOKUP = new HashMap<String, Class>();
+    static {
+        TYPE_LOOKUP.put("byte", Byte.class);
+        TYPE_LOOKUP.put(Byte.class.getName(), Byte.class);
+        TYPE_LOOKUP.put("int", Integer.class);
+        TYPE_LOOKUP.put(Integer.class.getName(), Integer.class);
+        TYPE_LOOKUP.put("short", Short.class);
+        TYPE_LOOKUP.put(Short.class.getName(), Short.class);
+        TYPE_LOOKUP.put("long", Long.class);
+        TYPE_LOOKUP.put(Long.class.getName(), Long.class);
+        TYPE_LOOKUP.put("float", Float.class);
+        TYPE_LOOKUP.put(Float.class.getName(), Float.class);
+        TYPE_LOOKUP.put("double", Double.class);
+        TYPE_LOOKUP.put(Double.class.getName(), Double.class);
+        TYPE_LOOKUP.put("boolean", Boolean.class);
+        TYPE_LOOKUP.put(Boolean.class.getName(), Boolean.class);
+        TYPE_LOOKUP.put("char", Character.class);
+        TYPE_LOOKUP.put(Character.class.getName(), Character.class);
+        TYPE_LOOKUP.put(String.class.getName(), String.class);
+    }
+    
     private final int defaultMaxSize;
     private final int defaultMinSize;
     private final int defaultBlockingTimeoutMilliseconds;
@@ -648,6 +669,19 @@
             return attributeName;
         }
     }
+    
+    private static String capitalize(String name) {
+        if (name == null || name.length() == 0) {
+            return name;
+        }
+        if (Character.isUpperCase(name.charAt(0))) {
+            return name;
+        } else {
+            char chars[] = name.toCharArray();
+            chars[0] = Character.toUpperCase(chars[0]);
+            return new String(chars);
+        }
+    }
 
     private Map getManagedConnectionFactoryInfoMap(ConnectionDefinitionType[] connectionDefinitionArray,
ClassLoader cl) throws DeploymentException {
         Map<String, GBeanData> managedConnectionFactoryInfos = new HashMap<String,
GBeanData>();
@@ -710,12 +744,16 @@
             String name = configProperty.getName();
             GAttributeInfo attributeInfo = gbeanData.getGBeanInfo().getAttribute(name);
             if (attributeInfo == null) {
-                unknownNames.add(name);
-//                    throw new DeploymentException("The plan is trying to set attribute:
" + name + " which does not exist.  Known attributes are: " + gbeanData.getGBeanInfo().getAttributes());
-            } else {
-                String type = attributeInfo.getType();
-                gbeanData.setAttribute(name, getValue(type, configProperty.getStringValue().trim(),
cl));
+                name = capitalize(name);
+                attributeInfo = gbeanData.getGBeanInfo().getAttribute(name);
+                if (attributeInfo == null) {
+                    unknownNames.add(name);
+                    continue;
+                }
             }
+
+            String type = attributeInfo.getType();
+            gbeanData.setAttribute(name, getValue(type, configProperty.getStringValue().trim(),
cl));
         }
         if (unknownNames.size() > 0) {
             StringBuffer buf = new StringBuffer("The plan is trying to set attributes: ").append(unknownNames).append("\n");
@@ -732,11 +770,13 @@
             return null;
         }
 
-        Class clazz;
-        try {
-            clazz = cl.loadClass(type);
-        } catch (ClassNotFoundException e) {
-            throw new DeploymentException("Could not load attribute class:  type: " + type,
e);
+        Class clazz = TYPE_LOOKUP.get(type);
+        if (clazz == null) {
+            try {
+                clazz = cl.loadClass(type);
+            } catch (ClassNotFoundException e) {
+                throw new DeploymentException("Could not load attribute class:  type: " +
type, e);
+            }
         }
 
         // Handle numeric fields with no value set



Mime
View raw message