User: d_jencks
Date: 02/04/08 16:23:47
Modified: src/main/org/jboss/resource/adapter/jdbc/xa
XAManagedConnectionFactory.java
Log:
Added more property setting code, thanks to Larry Sanderson
Revision Changes Path
1.5 +120 -19
jbosscx/src/main/org/jboss/resource/adapter/jdbc/xa/XAManagedConnectionFactory.java
Index: XAManagedConnectionFactory.java
===================================================================
RCS file:
/cvsroot/jboss/jbosscx/src/main/org/jboss/resource/adapter/jdbc/xa/XAManagedConnectionFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XAManagedConnectionFactory.java 25 Dec 2001 15:35:18 -0000 1.4
+++ XAManagedConnectionFactory.java 8 Apr 2002 23:23:47 -0000 1.5
@@ -47,7 +47,8 @@
* </pre>
*
* @author Aaron Mulder <[EMAIL PROTECTED]>
- * @version $Revision: 1.4 $
+ * @author Larry Sanderson <[EMAIL PROTECTED]>
+ * @version $Revision: 1.5 $
*/
public class XAManagedConnectionFactory implements ManagedConnectionFactory
{
@@ -452,24 +453,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)
@@ -509,5 +493,122 @@
}
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)
+ {
+ 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;
+ }
+
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development
Sponsored by http://www.ThinkGeek.com/