Sample class jdbc.wrapper.XAResourceInfoImpl
Sample code for the JDBC integration topic
/*
* ============================================================================
* (C) Copyright IBM Corporation 2006, 2007. All Rights Reserved.
*
* You may only copy, modify, and distribute these samples internally.
* These samples have not been tested under all conditions and are provided
* to you by IBM without obligation of support of any kind.
*
* IBM PROVIDES THESE SAMPLES "AS IS" SUBJECT TO ANY STATUTORY WARRANTIES THAT
* CANNOT BE EXCLUDED. IBM MAKES NO WARRANTIES OR CONDITIONS, EITHER EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR
* CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* NON-INFRINGEMENT REGARDING THESE SAMPLES OR TECHNICAL SUPPORT, IF ANY.
* ============================================================================
*/
package jdbc.wrapper;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Set;
import javax.sql.XADataSource;
public class XAResourceInfoImpl implements Serializable {
private String _className;
private Properties _properties;
public XAResourceInfoImpl(String className, Properties properties) {
_className = className;
_properties = properties;
}
public String getRMName() {
return _className;
}
public boolean commitInLastPhase() {
return false;
}
@Override
public boolean equals(Object o) {
boolean equal = false;
if (this == o) {
equal = true;
} else if (o instanceof XAResourceInfoImpl) {
final XAResourceInfoImpl other = (XAResourceInfoImpl) o;
if (other._className.equals(_className)) {
if (other._properties.equals(_properties)) {
equal = true;
}
}
}
return equal;
}
protected Object createObjectInstance() {
try {
final Class clazz = Class.forName(_className);
final Object object = clazz.newInstance();
final Set keys = _properties.keySet();
final Method[] methods = clazz.getMethods();
for (Object key : keys) {
final String methodName = "set" + key.toString();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(methodName)) {
final Class[] parameterTypes = method
.getParameterTypes();
if (parameterTypes.length == 1) {
final Class parameterType = parameterTypes[0];
if (parameterType.equals(String.class)) {
method.invoke(object,
new Object[] { _properties
.getProperty((String) key) });
} else if (parameterType.equals(int.class)) {
final int parameter = Integer
.parseInt(_properties
.getProperty((String) key));
method.invoke(object,
new Object[] { parameter });
}
}
}
}
}
return object;
} catch (ClassNotFoundException cnfe) {
throw new IllegalArgumentException(cnfe);
} catch (IllegalAccessException iae) {
throw new IllegalArgumentException(iae);
} catch (InstantiationException ie) {
throw new IllegalArgumentException(ie);
} catch (InvocationTargetException ite) {
throw new IllegalArgumentException(ite);
}
}
public XADataSource createDataSource() {
Object o = createObjectInstance();
return (javax.sql.XADataSource) o;
}
}