private synchronized DataSource getDataSource() throws ResourceException {
if (dataSource == null)
{
if (driverClass == null)
throw new JBossResourceException("No DataSourceClass supplied!");
try
{
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(driverClass);
dataSource = (DataSource) clazz.newInstance();
Class<?>[] NOCLASSES = new Class[] {};
for (Iterator i = this.connectionProps.keySet().iterator(); i.hasNext();)
{
String name = (String) i.next();
String value = this.connectionProps.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)
{
try
{
Method isMethod = clazz.getMethod("is" + name, NOCLASSES);
type = isMethod.getReturnType();
}
catch(NoSuchMethodException nsme)
{
log.warn("Property with name " + name + " could not be found for datasource class" + getDriverClass() + ". This property will be ignored.");
continue;
}
}
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);
editor.setAsText(value);
setter.invoke(dataSource, new Object[] { editor.getValue() });
}
}
catch (ClassNotFoundException cnfe)
{
throw new JBossResourceException("Class not found for DataSource " + getDriverClass(), cnfe);
}
catch (InstantiationException ie)
{
throw new JBossResourceException("Could not create an DataSource: ", ie);
}
catch (IllegalAccessException iae)
{
throw new JBossResourceException("Could not set a property: ", iae);
}
catch (IllegalArgumentException iae)
{
throw new JBossResourceException("Could not set a property: ", iae);
}
catch (InvocationTargetException ite)
{
throw new JBossResourceException("Could not invoke setter on DataSource: ", ite);
}
catch (NoSuchMethodException nsme)
{
throw new JBossResourceException("Could not find accessor on DataSource: ", nsme);
}
}
return dataSource;
}