FileDocCategorySizeDatePackage
AdminObjectFactory.javaAPI DocJBoss 4.2.18871Fri Jul 13 21:01:16 BST 2007org.jboss.resource.deployment

AdminObjectFactory

public class AdminObjectFactory extends Object
An admin object factory
author
Adrian Brock
version
$Revision: 57189 $

Fields Summary
private static final Logger
log
The logger
Constructors Summary
Methods Summary
public static java.lang.ObjectcreateAdminObject(java.lang.String jndiName, javax.management.ObjectName rarName, org.jboss.resource.metadata.AdminObjectMetaData aomd, java.util.Properties properties)


               
   
      boolean trace = log.isTraceEnabled();
      
      // Get the current classloader
      ClassLoader cl = Thread.currentThread().getContextClassLoader();

      if (trace)
         log.trace("Creating AdminObject '" + jndiName + "' metadata=" + aomd + " rar=" + rarName + " properties=" + properties + " classloader=" + cl);
      
      // The interface class
      String interfaceName = aomd.getAdminObjectInterfaceClass();
      // Load the interface class class
      if (trace)
         log.trace("AdminObject '" + jndiName + "' loading interface=" + interfaceName); 
      Class interfaceClass = cl.loadClass(interfaceName);
      
      // Determine the implementation class
      String implName = aomd.getAdminObjectImplementationClass();
      if (implName == null)
         throw new DeploymentException("No implementation class for admin object '" + interfaceClass + "' ra=" + rarName);
      
      // Load the implementation class
      if (trace)
         log.trace("AdminObject '" + jndiName + "' loading implementation=" + implName); 
      Class implClass = cl.loadClass(implName);
      if (interfaceClass.isAssignableFrom(implClass) == false)
         throw new DeploymentException(implClass.getName() + " is not a '" + interfaceClass + "' ra=" + rarName);

      Object result = implClass.newInstance();
      if (trace)
         log.trace("AdminObject '" + jndiName + "' created instance=" + result); 
      
      // Apply values from the ra.xml 
      Collection raProperties = aomd.getProperties();
      if (raProperties != null && raProperties.size() != 0)
      {
         for (Iterator i = raProperties.iterator(); i.hasNext();)
         {
            ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
            String name = cpmd.getName();
            String value = cpmd.getValue();
            if (value != null && value.length() > 0)
            {
               if (properties.containsKey(name))
               {
                  if (trace)
                     log.trace("AdminObject '" + jndiName + "' property=" + name + " IGNORING value=" + value +" specified in MBean properties."); 
               }
               else
               {
                  // Load the property class as defined in the meta data
                  String typeName = cpmd.getType();
                  if (trace)
                     log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 
                  Class type = cl.loadClass(typeName); 
                  
                  // Find the property editor for this class
                  PropertyEditor editor = PropertyEditorManager.findEditor(type);
                  if (editor == null)
                     throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
                  editor.setAsText(value);
                  Object object = editor.getValue();
                  
                  try
                  {
                     String setter = "set" + Character.toUpperCase(name.charAt(0));
                     if (name.length() > 1)
                        setter = setter.concat(name.substring(1));
                     Method method = implClass.getMethod(setter, new Class[] { type });
                     if (trace)
                        log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 
                     method.invoke(result, new Object[] { object });
                  }
                  catch (InvocationTargetException e)
                  {
                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
                  }
                  catch (Throwable t)
                  {
                     DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
                  }
               }
            }
         }
      }
      
      // Apply the properties
      if (properties != null)
      {
         for (Iterator i = properties.entrySet().iterator(); i.hasNext();)
         {
            Map.Entry property = (Map.Entry) i.next();
            String name = (String) property.getKey();
            String value = (String) property.getValue();
            if (trace)
               log.trace("AdminObject '" + jndiName + "' property=" + name + " value=" + value); 
            
            // Pick up the property metadata
            ConfigPropertyMetaData cpmd = aomd.getProperty(name);
            if (cpmd == null)
               throw new DeploymentException("No property '" + name + "' for admin object '" + interfaceClass + "' ra=" + rarName);
            if (trace)
               log.trace("AdminObject '" + jndiName + "' property=" + name + " metadata=" + cpmd); 

            // Load the property class as defined in the meta data
            String typeName = cpmd.getType();
            if (trace)
               log.trace("AdminObject '" + jndiName + "' property=" + name + " loading class=" + typeName); 
            Class type = cl.loadClass(typeName); 
            
            // Find the property editor for this class
            PropertyEditor editor = PropertyEditorManager.findEditor(type);
            if (editor == null)
               throw new DeploymentException("No property editor found for property '" + name + " class='" + type + "' for admin object '" + interfaceClass + "' ra=" + rarName);
            editor.setAsText(value);
            Object object = editor.getValue();
            
            try
            {
               String setter = "set" + Character.toUpperCase(name.charAt(0));
               if (name.length() > 1)
                  setter = setter.concat(name.substring(1));
               Method method = implClass.getMethod(setter, new Class[] { type });
               if (trace)
                  log.trace("AdminObject '" + jndiName + "' property=" + name + " set=" + object); 
               method.invoke(result, new Object[] { object });
            }
            catch (InvocationTargetException e)
            {
               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, e.getTargetException());
            }
            catch (Throwable t)
            {
               DeploymentException.rethrowAsDeploymentException("Error for property '" + name + "' class=" + implClass + "' for admin object '" + interfaceClass + "' ra=" + rarName, t);
            }
         }
      }
      
      return result;