FileDocCategorySizeDatePackage
ServiceMBeanDelegate.javaAPI DocJBoss 4.2.115946Fri Jul 13 20:53:50 BST 2007org.jboss.ejb3.service

ServiceMBeanDelegate

public class ServiceMBeanDelegate extends Object implements DynamicMBean, org.jboss.mx.modelmbean.XMBeanConstants
author
Kabir Khan
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
MBeanServer
server
ServiceContainer
container
ObjectName
serviceOn
MBeanInfo
mbeanInfo
HashMap
getterMethods
HashSet
getterBlackList
HashMap
setterMethods
HashSet
setterBlackList
HashMap
operations
HashSet
operationBlackList
Constructors Summary
public ServiceMBeanDelegate(MBeanServer server, ServiceContainer container, Class intf, ObjectName on)


           
   
      this.container = container;
      this.server = server;
      serviceOn = on;
      StandardMBean mbean = null;
      try
      {
         mbean = new StandardMBean(container.getSingleton(), intf);
      }
      catch (NotCompliantMBeanException e)
      {
         throw new RuntimeException(e);
      }
      mbeanInfo = mbean.getMBeanInfo();
   
public ServiceMBeanDelegate(MBeanServer server, ServiceContainer container, String xmbean, ObjectName on)

      this.container = container;
      this.server = server;
      serviceOn = on;
      XMBean mbean = null;
      try
      {
         Descriptor descriptor = new DescriptorSupport();
         descriptor.setField(RESOURCE_REFERENCE, container.getSingleton());
         descriptor.setField(RESOURCE_TYPE, xmbean);
         descriptor.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");

         mbean = new XMBean(descriptor, DESCRIPTOR);
      }
      catch (NotCompliantMBeanException e)
      {
         throw new RuntimeException(e);
      }
      catch (javax.management.MBeanException e)
      {
         throw new RuntimeException(e);
      }
      mbeanInfo = mbean.getMetaData();
   
Methods Summary
public java.lang.ObjectgetAttribute(java.lang.String attribute)

      Method getter = getGetter(attribute);

      try
      {
         return container.localInvoke(getter, new Object[0]);
      }
      catch (Throwable t)
      {
         if (t instanceof Exception) throw new MBeanException((Exception) t);
         else throw new RuntimeException(t);
      }
   
public javax.management.AttributeListgetAttributes(java.lang.String[] attributes)

      AttributeList list = new AttributeList();

      for (int i = 0; i < attributes.length; i++)
      {
         try
         {
            Object obj = getAttribute(attributes[i]);
            list.add(new Attribute(attributes[i], obj));
         }
         catch (Exception e)
         {
            throw new RuntimeException("Error reading attribute: " + attributes[i], e);
         }
      }
      return list;
   
private java.lang.reflect.MethodgetGetter(java.lang.String attribute)

      Method getter = getterMethods.get(attribute);

      if (getter == null && !getterBlackList.contains(attribute))
      {
         synchronized (getterMethods)
         {
            getter = getterMethods.get(attribute);
            if (getter == null)
            {
               try
               {
                  MBeanAttributeInfo[] attrInfos = mbeanInfo.getAttributes();
                  for (int i = 0; i < attrInfos.length; i++)
                  {
                     MBeanAttributeInfo attrInfo = attrInfos[i];
                     if (attrInfo.getName().equals(attribute))
                     {
                        if (!attrInfo.isReadable())
                        {
                           throw new AttributeNotFoundException("Attribute '" + attribute + "' is not writable in " + container.getBeanClass().getName());
                        }

                        String getterName = ((attrInfo.isIs()) ? "is" : "get") + attribute;
                        getter = container.getBeanClass().getMethod(getterName);
                        getterMethods.put(attribute, getter);
                     }
                  }

                  if (getter == null)
                  {
                     throw new AttributeNotFoundException("No attribute called '" + attribute + "' in " + container.getBeanClass());
                  }
               }
               catch (NoSuchMethodException e)
               {
                  throw new AttributeNotFoundException("Could not find getter for attribute '" + attribute + "' on " + container.getBeanClass().getName());
               }
               finally
               {
                  if (getter == null)
                  {
                     getterBlackList.add(attribute);
                  }
               }
            }
         }
      }

      return getter;
   
public javax.management.MBeanInfogetMBeanInfo()

      return mbeanInfo;
   
public javax.management.ObjectNamegetObjectName()

      return serviceOn;
   
private java.lang.reflect.MethodgetOperation(java.lang.String actionName, java.lang.String[] signature)

      String opSig = getOperationSignature(actionName, signature);
      Method operation = operations.get(actionName);

      if (operation == null && !setterBlackList.contains(opSig))
      {
         synchronized (setterMethods)
         {
            operation = operations.get(opSig);
            if (operation == null)
            {
               try
               {
                  MBeanOperationInfo[] opInfos = mbeanInfo.getOperations();
                  for (int i = 0; i < opInfos.length; i++)
                  {
                     MBeanOperationInfo op = opInfos[i];
                     if (op.getName().equals(actionName))
                     {
                        boolean match = true;
                        MBeanParameterInfo[] sigTypes = op.getSignature();
                        if (sigTypes.length == signature.length)
                        {
                           for (int j = 0; j < sigTypes.length; j++)
                           {
                              if (!sigTypes[j].getType().equals(signature[j]))
                              {
                                 match = false;
                                 break;
                              }
                           }
                        }

                        if (match)
                        {
                           Class[] types = null;
                           if (signature.length > 0)
                           {
                              types = new Class[signature.length];
                              for (int j = 0; j < signature.length; j++)
                              {
                                 types[j] = Classes.loadClass(signature[j]);
                              }
                           }
                           else
                           {
                              types = new Class[0];
                           }
                           operation = container.getBeanClass().getMethod(actionName, types);
                           operations.put(opSig, operation);
                        }
                     }
                  }

                  if (operation == null)
                  {
                     throw new RuntimeException("No operation called '" + actionName + "' in " + container.getBeanClass());
                  }

               }
               catch (ClassNotFoundException e)
               {
                  throw new RuntimeException("Could not find  type for operation '" + actionName + "' on " + container.getBeanClass().getName());
               }
               catch (NoSuchMethodException e)
               {
                  throw new RuntimeException("Could not find method for operation '" + actionName + "' on " + container.getBeanClass().getName());
               }
               finally
               {
                  if (operation == null)
                  {
                     operationBlackList.add(opSig);
                  }
               }
            }
         }
      }

      return operation;
   
private java.lang.StringgetOperationSignature(java.lang.String actionName, java.lang.String[] types)

      //Not really the signature, just something unique
      StringBuffer sig = new StringBuffer();
      sig.append(actionName);
      
      if (types != null)
      {
         for (int i = 0; i < types.length; i++)
         {
            sig.append(" ");
            sig.append(types[i]);
         }
      }
      return sig.toString();
   
private java.lang.reflect.MethodgetSetter(javax.management.Attribute attribute)

      String attrName = attribute.getName();
      Method setter = setterMethods.get(attrName);

      if (setter == null && !setterBlackList.contains(attrName))
      {
         synchronized (setterMethods)
         {
            setter = setterMethods.get(attrName);
            if (setter == null)
            {
               try
               {
                  MBeanAttributeInfo[] attrInfos = mbeanInfo.getAttributes();
                  for (int i = 0; i < attrInfos.length; i++)
                  {
                     MBeanAttributeInfo attrInfo = attrInfos[i];
                     if (attrInfo.getName().equals(attrName))
                     {
                        if (!attrInfo.isWritable())
                        {
                           throw new AttributeNotFoundException("Attribute '" + attrName + "' is not readable in " + container.getBeanClass().getName());
                        }

                        String setterName = "set" + attrName;
                        Class type = Classes.loadClass(attrInfo.getType());
                        setter = container.getBeanClass().getMethod(setterName, type);
                        setterMethods.put(attrName, setter);
                     }
                  }

                  if (setter == null)
                  {
                     throw new AttributeNotFoundException("No attribute called '" + attribute + "' in " + container.getBeanClass());
                  }
               }
               catch (ClassNotFoundException e)
               {
                  throw new AttributeNotFoundException("Could not load setter type for attribute '" + attrName + "' on " + container.getBeanClass().getName());
               }
               catch (NoSuchMethodException e)
               {
                  throw new AttributeNotFoundException("Could not find setter for attribute '" + attrName + "' on " + container.getBeanClass().getName());
               }
               finally
               {
                  if (setter == null)
                  {
                     setterBlackList.add(attrName);
                  }
               }
            }
         }
      }

      return setter;
   
public java.lang.Objectinvoke(java.lang.String actionName, java.lang.Object[] params, java.lang.String[] signature)

      if(log.isTraceEnabled())
         log.trace("invoke: " + actionName);
      
      try
      {
         // EJBTHREE-655: intercept lifecycle methods
//         if(isMagicLifecycleMethod(actionName))
//         {
//            invokeMagicLifecycleMethod(actionName);
//            return null;
//         }
         
         Method operation = getOperation(actionName, signature);
         return container.localInvoke(operation, params);
      }
      catch (Throwable t)
      {
         if (t instanceof Exception) throw new MBeanException((Exception) t);
         else throw new RuntimeException(t);
      }
   
public voidregister(javax.management.ObjectName on, java.lang.Class intf)

      server.registerMBean(this, serviceOn);
   
public voidsetAttribute(javax.management.Attribute attribute)

      Method setter = getSetter(attribute);
      try
      {
         container.localInvoke(setter, new Object[]{attribute.getValue()});
      }
      catch (Throwable t)
      {
         if (t instanceof Exception) throw new MBeanException((Exception) t);
         else throw new RuntimeException(t);
      }
   
public javax.management.AttributeListsetAttributes(javax.management.AttributeList attributes)

      for (Iterator it = attributes.iterator(); it.hasNext();)
      {
         Attribute attribute = (Attribute) it.next();
         try
         {
            setAttribute(attribute);
         }
         catch (Exception e)
         {
            throw new RuntimeException("Error setting attribute: " + attribute, e);
         }
      }
      return attributes;
   
public voidunregister()

      server.unregisterMBean(serviceOn);