1 22 package org.jboss.resource.deployment; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.Iterator ; 29 30 import javax.resource.spi.ResourceAdapter ; 31 32 import org.jboss.deployment.DeploymentException; 33 import org.jboss.resource.metadata.ConfigPropertyMetaData; 34 import org.jboss.resource.metadata.ConnectorMetaData; 35 import org.jboss.resource.metadata.RARDeploymentMetaData; 36 37 44 public class ResourceAdapterFactory 45 { 46 47 public static final String DUMMY_RA_CLASS = DummyResourceAdapter.class.getName(); 48 49 56 public static ResourceAdapter createResourceAdapter(RARDeploymentMetaData ramd) throws Exception 57 { 58 ResourceAdapter adapter = createResourceAdapter(ramd.getConnectorMetaData()); 59 60 for(Iterator iter = ramd.getRaXmlMetaData().getProperties().iterator(); iter.hasNext();) 61 { 62 ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData)iter.next(); 63 applyProperty(cpmd, adapter.getClass(), adapter); 64 65 } 66 67 return adapter; 68 } 69 70 76 public static ResourceAdapter createResourceAdapter(ConnectorMetaData cmd) throws Exception 77 { 78 String className = cmd.getRAClass(); 80 if (className == null) 81 { 82 if (cmd.getVersion().equals("1.0")) 83 className = DUMMY_RA_CLASS; 84 else 85 throw new IllegalArgumentException ("No resource adapter class name specified"); 86 } 87 88 Class raClass = Thread.currentThread().getContextClassLoader().loadClass(className); 90 if (ResourceAdapter .class.isAssignableFrom(raClass) == false) 91 throw new DeploymentException(raClass.getName() + " is not a resource adapter class"); 92 ResourceAdapter result = (ResourceAdapter ) raClass.newInstance(); 93 94 for (Iterator i = cmd.getProperties().iterator(); i.hasNext();) 96 { 97 ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next(); 98 applyProperty(cpmd, raClass, result); 99 100 101 } 102 103 return result; 104 } 105 106 private static void applyProperty(ConfigPropertyMetaData cpmd, Class clz, ResourceAdapter ra) throws Exception 107 { 108 109 String name = cpmd.getName(); 110 String type = cpmd.getType(); 111 String value = cpmd.getValue(); 112 113 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(type); 114 PropertyEditor editor = PropertyEditorManager.findEditor(clazz); 115 if (editor == null) 116 throw new IllegalArgumentException ("No property editor found for property " + cpmd); 117 editor.setAsText(value); 118 Object object = editor.getValue(); 119 120 try 121 { 122 String setter = "set" + Character.toUpperCase(name.charAt(0)); 123 if (name.length() > 1) 124 setter = setter.concat(name.substring(1)); 125 Method method = clz.getMethod(setter, new Class [] { clazz }); 126 method.invoke(ra, new Object [] { object }); 127 } 128 catch (InvocationTargetException e) 129 { 130 DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + clz.getName() + " setting property " + cpmd, e.getTargetException()); 131 } 132 catch (Throwable t) 133 { 134 DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + clz.getName() + " accessing property setter " + cpmd, t); 135 } 136 137 } 138 139 } 140 | Popular Tags |