Patches item #541255, was opened at 2002-04-08 14:44
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=376687&aid=541255&group_id=22866

Category: JBossCX
Group: v3.0 Rabbit Hole (unstable)
Status: Open
Resolution: None
Priority: 5
Submitted By: Larry Sanderson (lsanders)
Assigned to: Nobody/Anonymous (nobody)
Summary: properties in underlying XA-JDBC drivers

Initial Comment:
XA-JDBC drivers are typically made available to the 
App server via the 
org.jboss.resource.adapter.jdbc.xa.XAManagedConnectionF
actory MBean.  This class provides a generic way to 
set JavaBean-style properties on the underlying 
XADataSource via the setXADataSourceProperties 
method.  Unfortunately, only String-based JavaBean 
arguments are supported.

The XA driver that I am using requires an integer 
property to be set (portNumber).  The attached patch 
opens up the properties to support all primitive 
setXXX methods in addition to Strings.

-Larry

Index: 
connector//src/main/org/jboss/resource/adapter/jdbc/xa/
XAManagedConnectionFactory.java
=======================================================
============
RCS 
file: /cvsroot/jboss/jbosscx/src/main/org/jboss/resourc
e/adapter/jdbc/xa/XAManagedConnectionFactory.java,v
retrieving revision 1.4
diff -u -w -r1.4 XAManagedConnectionFactory.java
--- 
connector//src/main/org/jboss/resource/adapter/jdbc/xa/
XAManagedConnectionFactory.java 25 Dec 2001 15:35:18 -
0000    1.4
+++ 
connector//src/main/org/jboss/resource/adapter/jdbc/xa/
XAManagedConnectionFactory.java 8 Apr 2002 21:20:38 -
0000
@@ -452,24 +452,7 @@
             log.trace("got DataSource instance");
 
             Properties props = parseProperties();
-            Iterator it = props.keySet().iterator();
-            while (it.hasNext())
-            {
-               String name = null;
-               String value = null;
-               try
-               {
-                  name = (String)it.next();
-                  value = props.getProperty(name);
-                  Method meth = cls.getMethod("set" + 
Character.toUpperCase(name.charAt(0)) + name.substring
(1),
-                        new Class[]{String.class});
-                  meth.invoke(xads, new Object[]
{value});
-               }
-               catch (Exception e)
-               {
-                  log.warn("Unable to set 
XADataSource property " + name + "=" + value + ":");
-               }
-            }
+            populate(xads, props);
             return xads;
          }
          catch (Exception e)
@@ -508,6 +491,122 @@
          props.put(key, value);
       }
       return props;
+   }
+   
+   /**
+    * Populate the obj with the properties in the 
Map.  No exceptions
+    * are thrown if a property is specified for which 
there is no setter, 
+    * however a warning is issued.
+    * 
+    * @param props the properties to use.
+    */
+   private void populate(Object obj, Map props) 
+   {
+      // get all valid set methods - 
+      Method[] methods = obj.getClass().getMethods();
+      HashMap setters = new HashMap();
+
+      for (int i = 0; i < methods.length; i++)
+      {
+         Method meth = methods[i];
+         String name = meth.getName();
+         if (name.startsWith("set")) 
+         {
+            String attrName = name.substring(3);
+            if (meth.getParameterTypes().length == 1) 
+            {
+               Class type = meth.getParameterTypes()
[0];
+               if (type == String.class || 
type.isPrimitive()) 
+               {
+                  setters.put(attrName, meth);
+               }
+            }
+         }
+       }
+       
+       for (Iterator i = props.entrySet().iterator(); 
i.hasNext();) 
+       {
+          Map.Entry entry = (Map.Entry) i.next();
+          String attributeName = (String) entry.getKey
();
+          String attributeValue = (String) 
entry.getValue();
+          
+          Method meth = (Method)setters.get
(attributeName);
+          if (meth != null)
+          {
+             try
+             {
+                Object val = convert(attributeValue, 
meth.getParameterTypes()[0]);
+                meth.invoke(obj, new Object[] { 
val });
+             }
+             catch (Exception e)
+             {
+                e.printStackTrace();
+                log.warn("Unable to set XADataSource 
property " + 
+                    attributeName + "=" + 
attributeValue + ":");
+             }
+          } 
+          else 
+          {
+             log.warn("No setter method for 
attribute " + attributeName +
+                      " (value=" + attributeValue 
+ ").");
+          }
+       }
+    }
+   
+   /**
+    * Convert the specified value to the specified 
class.  This should be 
+    * replaced with the 
org.apache.commons.beanUtils.ConvertUtils class.
+    * 
+    * @return the value converted to type
+    * @param value the String value to convert
+    * @type the class to convert to.  Must be a 
primitive type or String.
+    */
+   private Object convert(String value, Class type) 
+   {
+      if (type == String.class) 
+      {
+         return value;
+      }
+      if (type == Integer.TYPE) 
+      {
+         return Integer.valueOf(value);
+      }
+      if (type == Double.TYPE) 
+      {
+         return Double.valueOf(value);
+      }
+      if (type == Boolean.TYPE) {
+         return new Boolean(
+            value.equalsIgnoreCase("true") ||
+            value.equalsIgnoreCase("on") ||
+            value.equalsIgnoreCase("yes")
+         );
+      }
+      if (type == Long.TYPE) 
+      {
+         return Long.valueOf(value);
+      }
+      if (type == Float.TYPE) 
+      {
+         return Float.valueOf(value);
+      }
+      if (type == Byte.TYPE) 
+      {
+         return Byte.valueOf(value);
+      }
+      if (type == Short.TYPE) 
+      {
+         return Short.valueOf(value);
+      }
+      if (type == Character.TYPE) 
+      {
+         return new Character(value.charAt(0));
+      }
+      if (type == Double.TYPE) 
+      {
+         return Double.valueOf(value);
+      }
+      return null;
    }
 }


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=376687&aid=541255&group_id=22866

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Sponsored by http://www.ThinkGeek.com/

Reply via email to