[jboss-cvs] JBossAS SVN: r97313 - in branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource: deployment and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Dec 2 13:46:11 EST 2009


Author: jesper.pedersen
Date: 2009-12-02 13:46:11 -0500 (Wed, 02 Dec 2009)
New Revision: 97313

Added:
   branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigProperty.java
   branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigPropertyHandler.java
Modified:
   branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/connectionmanager/RARDeployment.java
   branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/AdminObjectFactory.java
   branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ResourceAdapterFactory.java
Log:
[JBPAPP-2810] Support primitive types for config-properties

Modified: branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/connectionmanager/RARDeployment.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/connectionmanager/RARDeployment.java	2009-12-02 18:21:08 UTC (rev 97312)
+++ branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/connectionmanager/RARDeployment.java	2009-12-02 18:46:11 UTC (rev 97313)
@@ -40,6 +40,8 @@
 import org.jboss.deployment.DeploymentException;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.MetaData;
+import org.jboss.resource.deployment.ConfigProperty;
+import org.jboss.resource.deployment.ConfigPropertyHandler;
 import org.jboss.resource.metadata.ConfigPropertyMetaData;
 import org.jboss.resource.metadata.ConnectionDefinitionMetaData;
 import org.jboss.resource.metadata.ConnectorMetaData;
@@ -49,6 +51,7 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+
 /**
  * The RARDeployment mbean manages instantiation and configuration of a
  * ManagedConnectionFactory instance. It is intended to be configured
@@ -110,6 +113,8 @@
 
    private ManagedConnectionFactory mcf;
 
+   private ConfigPropertyHandler configPropertyHandler;
+
    /**
     * Default managed constructor for RARDeployment mbeans.
     */
@@ -346,7 +351,7 @@
       }
       //set overridden properties;
       setMcfProperties(managedConnectionFactoryProperties);
-      
+
       if (resourceAdapter != null && mcf instanceof ResourceAdapterAssociation)
       {
          ResourceAdapterAssociation raa = (ResourceAdapterAssociation) mcf;
@@ -364,38 +369,16 @@
    {
       setManagedConnectionFactoryAttribute(name, clazz, value, false);
    }
-   
+
    protected void setManagedConnectionFactoryAttribute(String name, Class clazz, Object value, boolean mustExist)
    {
-      if (name == null || name.length() == 0)
-         throw new IllegalArgumentException("Null or empty attribute name " + name);
-      String setterName = "set" + Character.toUpperCase(name.charAt(0));
-      if (name.length() > 1)
-         setterName = setterName.concat(name.substring(1));
-      Method setter;
       try
       {
-         setter = mcfClass.getMethod(setterName, new Class[] {clazz});
+         getConfigPropertyHandler().handle(new ConfigProperty(name, clazz, value), mustExist);
       }
-      catch (NoSuchMethodException nsme)
-      {
-         String error = "The class '" + mcfClass.toString() + "' has no setter for config property '" + name + "'"; 
-         if (mustExist)
-            throw new IllegalArgumentException(error);
-         else
-         {
-            log.trace(error, nsme);
-            return;
-         }
-      }
-      try
-      {
-         setter.invoke(mcf, new Object[] {value});
-         log.debug("set property " + name + " to value " + value);
-      }
       catch (Exception e)
       {
-         String error = "Unable to invoke setter method '" + setter + "' " + "on object '" + mcf + "'";
+         String error = "Unable to set property '" + name + "' " + "on object '" + mcf + "'";
          if (e instanceof InvocationTargetException)
             throw new NestedRuntimeException(error, ((InvocationTargetException) e).getCause());
          else
@@ -447,48 +430,21 @@
       {
          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
          String name = cpmd.getName();
-         String type = cpmd.getType();
          String value = cpmd.getValue();
-         if (name == null || name.length() == 0 || value == null || value.length() == 0)
-         {
-            log.debug("Not setting config property '" + name + "'");
-            continue;
-         }
-         // see if it is a primitive type first
-         Class clazz = Classes.getPrimitiveTypeForName(type);
-         if (clazz == null)
-         {
-            //not primitive, look for it.
-            try
-            {
-               clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
-            }
-            catch (ClassNotFoundException cnfe)
-            {
-               log.warn("Unable to find class '" + type + "' for " + "property '" + name + "' - skipping property.");
-               continue;
-            }
-         }
-         PropertyEditor pe = PropertyEditorManager.findEditor(clazz);
-         if (pe == null)
-         {
-            log.warn("Unable to find a PropertyEditor for class '" + clazz + "' of property '" + name + "' - "
-                  + "skipping property");
-            continue;
-         }
-         log.debug("setting property: " + name + " to value " + value);
          try
          {
-            pe.setAsText(value);
+            getConfigPropertyHandler().handle(cpmd, mustExist);
          }
-         catch (IllegalArgumentException iae)
+         catch (Exception e)
          {
-            log.warn("Value '" + value + "' is not valid for property '" + name + "' of class '" + clazz
-                  + "' - skipping " + "property");
-            continue;
+            String error = "Unable to set property '" + name + "' " + "on object '" + mcf + "'";
+            if (e instanceof InvocationTargetException)
+               throw new NestedRuntimeException(error, ((InvocationTargetException) e).getCause());
+            else
+               throw new NestedRuntimeException(error, e);
          }
-         Object v = pe.getValue();
-         setManagedConnectionFactoryAttribute(name, clazz, v, mustExist);
+         sendNotification(new Notification(MCF_ATTRIBUTE_CHANGED_NOTIFICATION, getServiceName(),
+               getNextNotificationSequenceNumber()));
       }
    }
 
@@ -607,7 +563,7 @@
       buffer.append(Integer.toHexString(System.identityHashCode(this)));
       return buffer.toString();
    }
-   
+
    public int hashCode()
    {
       return mcf.hashCode();
@@ -623,4 +579,13 @@
    {
       mcf.setLogWriter(out);
    }
+   
+   protected ConfigPropertyHandler getConfigPropertyHandler()
+   {
+      if (configPropertyHandler == null)
+      {
+         configPropertyHandler = new ConfigPropertyHandler(mcf, mcfClass);
+      }
+      return configPropertyHandler;
+   }
 }

Modified: branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/AdminObjectFactory.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/AdminObjectFactory.java	2009-12-02 18:21:08 UTC (rev 97312)
+++ branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/AdminObjectFactory.java	2009-12-02 18:46:11 UTC (rev 97313)
@@ -48,43 +48,50 @@
    /** The logger */
    private static final Logger log = Logger.getLogger(AdminObjectFactory.class);
 
-   public static Object createAdminObject(String jndiName, ObjectName rarName, AdminObjectMetaData aomd, Properties properties) throws Exception
+   public static Object createAdminObject(final String jndiName, ObjectName rarName, AdminObjectMetaData aomd,
+         Properties properties) throws Exception
    {
-      boolean trace = log.isTraceEnabled();
-      
+      final boolean trace = log.isTraceEnabled();
+
       // Get the current classloader
       ClassLoader cl = Thread.currentThread().getContextClassLoader();
 
       if (trace)
-         log.trace("Creating AdminObject '" + jndiName + "' metadata=" + aomd + " rar=" + rarName + " properties=" + properties + " classloader=" + cl);
-      
+         log.trace("Creating AdminObject '" + jndiName + "' metadata=" + aomd + " rar=" + rarName + " properties="
+               + properties + " classloader=" + cl);
+
       // The interface class
       String interfaceName = aomd.getAdminObjectInterfaceClass();
       // Load the interface class class
       if (trace)
-         log.trace("AdminObject '" + jndiName + "' loading interface=" + interfaceName); 
+         log.trace("AdminObject '" + jndiName + "' loading interface=" + interfaceName);
       Class interfaceClass = cl.loadClass(interfaceName);
-      
+
       // Determine the implementation class
       String implName = aomd.getAdminObjectImplementationClass();
       if (implName == null)
-         throw new DeploymentException("No implementation class for admin object '" + interfaceClass + "' ra=" + rarName);
-      
+         throw new DeploymentException("No implementation class for admin object '" + interfaceClass + "' ra="
+               + rarName);
+
       // Load the implementation class
       if (trace)
-         log.trace("AdminObject '" + jndiName + "' loading implementation=" + implName); 
+         log.trace("AdminObject '" + jndiName + "' loading implementation=" + implName);
       Class implClass = cl.loadClass(implName);
       if (interfaceClass.isAssignableFrom(implClass) == false)
          throw new DeploymentException(implClass.getName() + " is not a '" + interfaceClass + "' ra=" + rarName);
 
       Object result = implClass.newInstance();
       if (trace)
-         log.trace("AdminObject '" + jndiName + "' created instance=" + result); 
+         log.trace("AdminObject '" + jndiName + "' created instance=" + result);
+
+      // Create ConfigPropertyHandler for the AdminObject
+      ConfigPropertyHandler configPropertyHandler = new ConfigPropertyHandler(result, implClass, "AdminObject: ");
       
       // Apply values from the ra.xml 
       Collection raProperties = aomd.getProperties();
       if (raProperties != null && raProperties.size() != 0)
       {
+         
          for (Iterator i = raProperties.iterator(); i.hasNext();)
          {
             ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
@@ -95,46 +102,36 @@
                if (properties.containsKey(name))
                {
                   if (trace)
-                     log.trace("AdminObject '" + jndiName + "' property=" + name + " IGNORING value=" + value +" specified in MBean properties."); 
+                     log.trace("AdminObject '" + jndiName + "' property=" + name + " IGNORING value=" + value
+                           + " specified in MBean properties.");
                }
                else
                {
                   // Load the property class as defined in the meta data
                   String typeName = cpmd.getType();
                   if (trace)
-                     log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 
-                  Class type = cl.loadClass(typeName); 
-                  
-                  // Find the property editor for this class
-                  PropertyEditor editor = PropertyEditorManager.findEditor(type);
-                  if (editor == null)
-                     throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
-                  editor.setAsText(value);
-                  Object object = editor.getValue();
-                  
+                     log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName);
+
                   try
                   {
-                     String setter = "set" + Character.toUpperCase(name.charAt(0));
-                     if (name.length() > 1)
-                        setter = setter.concat(name.substring(1));
-                     Method method = implClass.getMethod(setter, new Class[] { type });
-                     if (trace)
-                        log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 
-                     method.invoke(result, new Object[] { object });
+                     configPropertyHandler.handle(cpmd);
                   }
                   catch (InvocationTargetException e)
                   {
-                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
+                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class="
+                           + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e
+                           .getTargetException());
                   }
                   catch (Throwable t)
                   {
-                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
+                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class="
+                           + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
                   }
                }
             }
          }
       }
-      
+
       // Apply the properties
       if (properties != null)
       {
@@ -143,50 +140,39 @@
             Map.Entry property = (Map.Entry) i.next();
             String name = (String) property.getKey();
             String value = (String) property.getValue();
-            if (trace)
-               log.trace("AdminObject '" + jndiName + "' property=" + name + " value=" + value); 
             
+            if (trace)
+               log.trace("AdminObject '" + jndiName + "' property=" + name + " value=" + value);
+
             // Pick up the property metadata
             ConfigPropertyMetaData cpmd = aomd.getProperty(name);
             if (cpmd == null)
-               throw new DeploymentException("No property '" + name + "' for admin object '" + interfaceClass + "' ra=" + rarName);
-            if (trace)
-               log.trace("AdminObject '" + jndiName + "' property=" + name + " metadata=" + cpmd); 
-
-            // Load the property class as defined in the meta data
-            String typeName = cpmd.getType();
-            if (trace)
-               log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 
-            Class type = cl.loadClass(typeName); 
+               throw new DeploymentException("No property '" + name + "' for admin object '" + interfaceClass + "' ra="
+                     + rarName);
             
-            // Find the property editor for this class
-            PropertyEditor editor = PropertyEditorManager.findEditor(type);
-            if (editor == null)
-               throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
-            editor.setAsText(value);
-            Object object = editor.getValue();
+            // Make copy of the ConfigPropertyMetaData with new value
+            ConfigPropertyMetaData cpmdCopy = new ConfigPropertyMetaData();
+            cpmdCopy.setName(name);
+            cpmdCopy.setType(cpmd.getType());
+            cpmdCopy.setValue(value);
             
             try
             {
-               String setter = "set" + Character.toUpperCase(name.charAt(0));
-               if (name.length() > 1)
-                  setter = setter.concat(name.substring(1));
-               Method method = implClass.getMethod(setter, new Class[] { type });
-               if (trace)
-                  log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 
-               method.invoke(result, new Object[] { object });
+               configPropertyHandler.handle(cpmdCopy);
             }
             catch (InvocationTargetException e)
             {
-               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
+               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass
+                     + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
             }
             catch (Throwable t)
             {
-               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
+               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass
+                     + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
             }
          }
       }
-      
+
       return result;
    }
 }

Added: branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigProperty.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigProperty.java	                        (rev 0)
+++ branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigProperty.java	2009-12-02 18:46:11 UTC (rev 97313)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.resource.deployment;
+
+/**
+ * Holder for resolved configuration properties.  
+
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public class ConfigProperty
+{
+   /**
+    * Name of config property
+    */
+   private final String name;
+
+   /**
+    * Class type of the config property
+    */
+   private final Class type;
+
+   /**
+    * Value of the config property
+    */
+   private final Object value;
+
+   /**
+    * Constructor
+    * 
+    * @param name
+    * @param type
+    * @param value
+    */
+   public ConfigProperty(String name, Class type, Object value)
+   {
+      super();
+      this.name = name;
+      this.type = type;
+      this.value = value;
+   }
+
+   /**
+    * 
+    * @return
+    */
+   public String getName()
+   {
+      return name;
+   }
+
+   /**
+    * 
+    * @return
+    */
+   public Class getType()
+   {
+      return type;
+   }
+
+   /**
+    * 
+    * @return
+    */
+   public Object getValue()
+   {
+      return value;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see java.lang.Object#toString()
+    */
+   public String toString()
+   {
+      StringBuffer buffer = new StringBuffer();
+      buffer.append("ConfigProperty").append('@');
+      buffer.append(Integer.toHexString(System.identityHashCode(this)));
+      buffer.append("[name=").append(name);
+      if (type != null)
+         buffer.append(" type=").append(type);
+      if (value != null)
+         buffer.append(" value=").append(value);
+      buffer.append(']');
+      return buffer.toString();
+   }
+}

Added: branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigPropertyHandler.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigPropertyHandler.java	                        (rev 0)
+++ branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ConfigPropertyHandler.java	2009-12-02 18:46:11 UTC (rev 97313)
@@ -0,0 +1,312 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.resource.deployment;
+
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.logging.Logger;
+import org.jboss.resource.metadata.ConfigPropertyMetaData;
+import org.jboss.util.Classes;
+
+/**
+ * Utility used to handle setting configuration properties from ra.xml file.
+ * 
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ * @author <a href="jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ * @version $Revision: $
+ */
+public class ConfigPropertyHandler
+{
+   private static final Logger log = Logger.getLogger(ConfigPropertyHandler.class);
+
+   /** A map of possible alternate property types to try. Used to support primitive types on ResourceAdapter impls **/
+   private static Map<String, Class<?>> PRIMATIVE_TYPE_ALTERNATES = new HashMap<String, Class<?>>();
+   static
+   {
+      PRIMATIVE_TYPE_ALTERNATES.put(Boolean.class.getName(), Boolean.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Byte.class.getName(), Byte.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Character.class.getName(), Character.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Double.class.getName(), Double.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Float.class.getName(), Float.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Integer.class.getName(), Integer.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Long.class.getName(), Long.TYPE);
+      PRIMATIVE_TYPE_ALTERNATES.put(Short.class.getName(), Short.TYPE);
+   }
+
+   /** Object to set the Config Property on **/
+   private final Object configTarget;
+
+   /** Object type to set the Config Property on **/
+   private final Class configTargetType;
+
+   /** Identifier used to trace property values being set **/
+   private final String traceIdentifier;
+
+   /**
+    * Constructor
+    * 
+    * @param configTarget
+    * @param configTargetType
+    */
+   public ConfigPropertyHandler(Object configTarget, Class configTargetType)
+   {
+      this(configTarget, configTargetType, "");
+   }
+
+   /**
+    * Constructor with trace string
+    * @param configTarget
+    * @param configTargetType
+    * @param traceIdentifier
+    */
+   public ConfigPropertyHandler(Object configTarget, Class configTargetType, String traceIdentifier)
+   {
+      super();
+      this.configTarget = configTarget;
+      this.configTargetType = configTargetType;
+      this.traceIdentifier = traceIdentifier;
+   }
+
+   /**
+    * Handles setting a configuration property on a target object.
+    * 
+    * @param configProperty
+    * @throws Exception
+    */
+   public void handle(ConfigProperty configProperty) throws Exception
+   {
+      handle(configProperty, true);
+   }
+
+   /**
+    * Handles setting a configuration property on a target object.
+    * 
+    * @param configProperty
+    * @param mustExist
+    * @throws Exception
+    */
+   public void handle(ConfigProperty configProperty, boolean mustExist) throws Exception
+   {
+      if (log.isTraceEnabled())
+      {
+         log.trace("Handling config property - " + configProperty);
+      }
+
+      String propertyName = configProperty.getName();
+      if (propertyName == null || propertyName.length() == 0)
+      {
+         throw new IllegalArgumentException("Null or empty attribute name " + propertyName);
+      }
+
+      Object propertyValue = configProperty.getValue();
+
+      if (propertyValue == null)
+      {
+         if (log.isDebugEnabled())
+         {
+            log.debug("Not setting config property with null value " + configProperty);
+         }
+         return;
+      }
+
+      // Generate the setter name
+      String setterName = getSetterName(propertyName);
+
+      // Get the setter method using the setter name
+      Method method = null;
+      try
+      {
+         method = getMethod(setterName, configProperty.getType());
+      }
+      catch (NoSuchMethodException nsme)
+      {
+         String error = "The class '" + configTargetType + "' has no setter for config property '" + propertyName + "'";
+         if (mustExist)
+         {
+            throw new IllegalArgumentException(error, nsme);
+         }
+         if (log.isTraceEnabled())
+         {
+            log.trace(error, nsme);
+         }
+         return;
+      }
+
+      if (log.isTraceEnabled())
+      {
+         log.trace(traceIdentifier + " setting property=" + propertyName + " value=" + configProperty.getValue());
+      }
+
+      // Invoke the setter
+      method.invoke(configTarget, new Object[] {configProperty.getValue()});
+   }
+
+   /**
+    * Handles setting a configuration property on a target object based on ConfigPropertyMetaData.
+    * 
+    * @param configPropertyMetaData
+    * @throws Exception
+    */
+   public void handle(ConfigPropertyMetaData configPropertyMetaData) throws Exception
+   {
+      this.handle(configPropertyMetaData, true);
+   }
+
+   /**
+    * Handles setting a configuration property on a target object based on ConfigPropertyMetaData.
+    * 
+    * @param configPropertyMetaData
+    * @param mustExist
+    * @throws Exception
+    */
+   public void handle(ConfigPropertyMetaData configPropertyMetaData, boolean mustExist) throws Exception
+   {
+      String propertyName = configPropertyMetaData.getName();
+      String propertyType = configPropertyMetaData.getType();
+      String propertyValue = configPropertyMetaData.getValue();
+
+      if (propertyValue == null || propertyValue.length() == 0)
+      {
+         log.debug("Not setting config property with null value " + configPropertyMetaData);
+         return;
+      }
+
+      // See if it is a primitive type alias first
+      Class expectedPropertyType = Classes.getPrimitiveTypeForName(propertyType);
+      if (expectedPropertyType == null)
+      {
+         // Not primitive alias, look for it.
+         try
+         {
+            expectedPropertyType = Thread.currentThread().getContextClassLoader().loadClass(propertyType);
+         }
+         catch (ClassNotFoundException cnfe)
+         {
+            if (mustExist)
+            {
+               throw cnfe;
+            }
+            log.warn("Unable to find class '" + propertyType + "' for " + "property '" + propertyName
+                  + "' - skipping property.");
+         }
+      }
+
+      // Use the actual property type to get the value
+      Object value = getValue(propertyName, expectedPropertyType, propertyValue, mustExist);
+
+      // Invoke handle with constructed ConfigProperty
+      handle(new ConfigProperty(propertyName, expectedPropertyType, value), mustExist);
+   }
+
+   /**
+    * Generates the setter name for a property
+    * 
+    * @param propertyName
+    * @return
+    */
+   private String getSetterName(String propertyName)
+   {
+      String setter = "set" + Character.toUpperCase(propertyName.charAt(0));
+      if (propertyName.length() > 1)
+         setter = setter.concat(propertyName.substring(1));
+      return setter;
+   }
+
+   /**
+    * Retrieves the setter method for the property.  Will check the expected type first, 
+    * and if not found it will check for primitive alternatives.
+    *   
+    * @param setterName
+    * @param expectedPropertyType
+    * @return
+    * @throws NoSuchMethodException
+    */
+   private Method getMethod(String setterName, Class expectedPropertyType) throws NoSuchMethodException
+   {
+      Method method = null;
+      try
+      {
+         // Check to see if the method exists with the expected type
+         method = configTargetType.getMethod(setterName, new Class[] {expectedPropertyType});
+      }
+      catch (NoSuchMethodException nsme)
+      {
+         // Check to see if a primitive alternative is available  
+         expectedPropertyType = PRIMATIVE_TYPE_ALTERNATES.get(expectedPropertyType.getName());
+         if (expectedPropertyType == null)
+         {
+            // No alternative to try
+            throw nsme;
+         }
+         // Check to see if a setter with the primitive alternative exists
+         method = configTargetType.getMethod(setterName, new Class[] {expectedPropertyType});
+      }
+      return method;
+   }
+
+   /**
+    * Get the correctly typed value from the string value.  
+    * 
+    * @param propertyName
+    * @param actualType
+    * @param value
+    * @param mustExist
+    * @return
+    */
+   private Object getValue(String propertyName, Class actualType, String value, boolean mustExist)
+   {
+      PropertyEditor editor = PropertyEditorManager.findEditor(actualType);
+      if (editor == null)
+      {
+         String error = "No property editor found for property " + propertyName;
+         if (mustExist)
+         {
+            throw new IllegalArgumentException(error);
+         }
+         else
+         {
+            log.warn(error);
+            return null;
+         }
+      }
+      try
+      {
+         editor.setAsText(value);
+      }
+      catch (IllegalArgumentException iae)
+      {
+         if (mustExist)
+         {
+            throw iae;
+         }
+         log.warn("Value '" + value + "' is not valid for property '" + propertyName + "' of class '" + actualType
+               + "' - skipping " + "property");
+         return null;
+      }
+
+      return editor.getValue();
+   }
+}

Modified: branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ResourceAdapterFactory.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ResourceAdapterFactory.java	2009-12-02 18:21:08 UTC (rev 97312)
+++ branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/deployment/ResourceAdapterFactory.java	2009-12-02 18:46:11 UTC (rev 97313)
@@ -25,7 +25,9 @@
 import java.beans.PropertyEditorManager;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 import javax.resource.spi.ResourceAdapter;
 
@@ -61,46 +63,35 @@
          else
             throw new IllegalArgumentException("No resource adapter class name specified");
       }
-      
+
       // Load the class
       Class raClass = Thread.currentThread().getContextClassLoader().loadClass(className);
       if (ResourceAdapter.class.isAssignableFrom(raClass) == false)
          throw new DeploymentException(raClass.getName() + " is not a resource adapter class");
       ResourceAdapter result = (ResourceAdapter) raClass.newInstance();
-      
+
       // Apply the properties
+      ConfigPropertyHandler configPropertyHandler = new ConfigPropertyHandler(result, raClass, "ResourceAdapter: ");
       for (Iterator i = cmd.getProperties().iterator(); i.hasNext();)
       {
          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
-         String name = cpmd.getName();
-         String type = cpmd.getType();
-         String value = cpmd.getValue();
-         
-         Class clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
-         PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
-         if (editor == null)
-            throw new IllegalArgumentException("No property editor found for property " + cpmd);
-         editor.setAsText(value);
-         Object object = editor.getValue();
-         
-         try
-         {
-            String setter = "set" + Character.toUpperCase(name.charAt(0));
-            if (name.length() > 1)
-               setter = setter.concat(name.substring(1));
-            Method method = raClass.getMethod(setter, new Class[] { clazz });
-            method.invoke(result, new Object[] { object });
-         }
+         try 
+         {  
+            configPropertyHandler.handle(cpmd);
+         } 
          catch (InvocationTargetException e)
          {
-            DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + raClass.getName() + " setting property " + cpmd, e.getTargetException());
+            DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + raClass.getName()
+                  + " setting property " + cmd, e.getTargetException());
          }
          catch (Throwable t)
          {
-            DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + raClass.getName() + " accessing property setter " + cpmd, t);
+            DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + raClass.getName()
+                  + " accessing property setter " + cmd, t);
          }
       }
-      
+
       return result;
    }
+
 }




More information about the jboss-cvs-commits mailing list