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.Collection ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 33 import javax.management.ObjectName ; 34 35 import org.jboss.deployment.DeploymentException; 36 import org.jboss.logging.Logger; 37 import org.jboss.resource.metadata.AdminObjectMetaData; 38 import org.jboss.resource.metadata.ConfigPropertyMetaData; 39 40 46 public class AdminObjectFactory 47 { 48 49 private static final Logger log = Logger.getLogger(AdminObjectFactory.class); 50 51 public static Object createAdminObject(String jndiName, ObjectName rarName, AdminObjectMetaData aomd, Properties properties) throws Exception 52 { 53 boolean trace = log.isTraceEnabled(); 54 55 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 57 58 if (trace) 59 log.trace("Creating AdminObject '" + jndiName + "' metadata=" + aomd + " rar=" + rarName + " properties=" + properties + " classloader=" + cl); 60 61 String interfaceName = aomd.getAdminObjectInterfaceClass(); 63 if (trace) 65 log.trace("AdminObject '" + jndiName + "' loading interface=" + interfaceName); 66 Class interfaceClass = cl.loadClass(interfaceName); 67 68 String implName = aomd.getAdminObjectImplementationClass(); 70 if (implName == null) 71 throw new DeploymentException("No implementation class for admin object '" + interfaceClass + "' ra=" + rarName); 72 73 if (trace) 75 log.trace("AdminObject '" + jndiName + "' loading implementation=" + implName); 76 Class implClass = cl.loadClass(implName); 77 if (interfaceClass.isAssignableFrom(implClass) == false) 78 throw new DeploymentException(implClass.getName() + " is not a '" + interfaceClass + "' ra=" + rarName); 79 80 Object result = implClass.newInstance(); 81 if (trace) 82 log.trace("AdminObject '" + jndiName + "' created instance=" + result); 83 84 Collection raProperties = aomd.getProperties(); 86 if (raProperties != null && raProperties.size() != 0) 87 { 88 for (Iterator i = raProperties.iterator(); i.hasNext();) 89 { 90 ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next(); 91 String name = cpmd.getName(); 92 String value = cpmd.getValue(); 93 if (value != null && value.length() > 0) 94 { 95 if (properties.containsKey(name)) 96 { 97 if (trace) 98 log.trace("AdminObject '" + jndiName + "' property=" + name + " IGNORING value=" + value +" specified in MBean properties."); 99 } 100 else 101 { 102 String typeName = cpmd.getType(); 104 if (trace) 105 log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 106 Class type = cl.loadClass(typeName); 107 108 PropertyEditor editor = PropertyEditorManager.findEditor(type); 110 if (editor == null) 111 throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName); 112 editor.setAsText(value); 113 Object object = editor.getValue(); 114 115 try 116 { 117 String setter = "set" + Character.toUpperCase(name.charAt(0)); 118 if (name.length() > 1) 119 setter = setter.concat(name.substring(1)); 120 Method method = implClass.getMethod(setter, new Class [] { type }); 121 if (trace) 122 log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 123 method.invoke(result, new Object [] { object }); 124 } 125 catch (InvocationTargetException e) 126 { 127 DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException()); 128 } 129 catch (Throwable t) 130 { 131 DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t); 132 } 133 } 134 } 135 } 136 } 137 138 if (properties != null) 140 { 141 for (Iterator i = properties.entrySet().iterator(); i.hasNext();) 142 { 143 Map.Entry property = (Map.Entry ) i.next(); 144 String name = (String ) property.getKey(); 145 String value = (String ) property.getValue(); 146 if (trace) 147 log.trace("AdminObject '" + jndiName + "' property=" + name + " value=" + value); 148 149 ConfigPropertyMetaData cpmd = aomd.getProperty(name); 151 if (cpmd == null) 152 throw new DeploymentException("No property '" + name + "' for admin object '" + interfaceClass + "' ra=" + rarName); 153 if (trace) 154 log.trace("AdminObject '" + jndiName + "' property=" + name + " metadata=" + cpmd); 155 156 String typeName = cpmd.getType(); 158 if (trace) 159 log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 160 Class type = cl.loadClass(typeName); 161 162 PropertyEditor editor = PropertyEditorManager.findEditor(type); 164 if (editor == null) 165 throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName); 166 editor.setAsText(value); 167 Object object = editor.getValue(); 168 169 try 170 { 171 String setter = "set" + Character.toUpperCase(name.charAt(0)); 172 if (name.length() > 1) 173 setter = setter.concat(name.substring(1)); 174 Method method = implClass.getMethod(setter, new Class [] { type }); 175 if (trace) 176 log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 177 method.invoke(result, new Object [] { object }); 178 } 179 catch (InvocationTargetException e) 180 { 181 DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException()); 182 } 183 catch (Throwable t) 184 { 185 DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t); 186 } 187 } 188 } 189 190 return result; 191 } 192 } 193 | Popular Tags |