private XADataSource createXaDataSource(Properties xaProps)
throws JBossResourceException
{
if(getXADataSourceClass() == null)
{
throw new JBossResourceException("No XADataSourceClass supplied!");
}
XADataSource xads;
try
{
Class clazz = Thread.currentThread().getContextClassLoader().loadClass(getXADataSourceClass());
xads = (XADataSource)clazz.newInstance();
Class[] NOCLASSES = new Class[]{};
for(Iterator i = xaProps.keySet().iterator(); i.hasNext();)
{
String name = (String)i.next();
String value = xaProps.getProperty(name);
char firstCharName = Character.toUpperCase(name.charAt(0));
if (name.length() > 1)
name = firstCharName+name.substring(1);
else
name = ""+firstCharName;
//This is a bad solution. On the other hand the only known example
// of a setter with no getter is for Oracle with password.
//Anyway, each xadatasource implementation should get its
//own subclass of this that explicitly sets the
//properties individually.
Class type = null;
try
{
Method getter = clazz.getMethod("get" + name, NOCLASSES);
type = getter.getReturnType();
}
catch(NoSuchMethodException e)
{
type = String.class;
} // end of try-catch
Method setter = clazz.getMethod("set" + name, new Class[]{type});
PropertyEditor editor = PropertyEditorManager.findEditor(type);
if(editor == null)
{
throw new JBossResourceException("No property editor found for type: " + type);
} // end of if ()
editor.setAsText(value);
setter.invoke(xads, new Object[]{editor.getValue()});
} // end of for ()
}
catch(ClassNotFoundException cnfe)
{
throw new JBossResourceException("Class not found for XADataSource " + getXADataSourceClass(), cnfe);
} // end of try-catch
catch(InstantiationException ie)
{
throw new JBossResourceException("Could not create an XADataSource: ", ie);
} // end of catch
catch(IllegalAccessException iae)
{
throw new JBossResourceException("Could not set a property: ", iae);
} // end of catch
catch(IllegalArgumentException iae)
{
throw new JBossResourceException("Could not set a property: ", iae);
} // end of catch
catch(InvocationTargetException ite)
{
throw new JBossResourceException("Could not invoke setter on XADataSource: ", ite);
} // end of catch
catch(NoSuchMethodException nsme)
{
throw new JBossResourceException("Could not find accessor on XADataSource: ", nsme);
} // end of catch
return xads;
}