FileDocCategorySizeDatePackage
JmxIntrospectingMixin.javaAPI DocJBoss 4.2.111827Fri Jul 13 21:02:30 BST 2007org.jboss.aspects.jmx

JmxIntrospectingMixin

public class JmxIntrospectingMixin extends Object implements DynamicMBean
Introspects a class to provide the JMX management interface. The class does not have to follow any pattern.

public get/set methods will create mbean attributes, all other public methods will be operations.

author
Bill Burke
version
$Revision: 57186 $

Fields Summary
private final Object
target
private final HashMap
ops
private final HashMap
gets
private final HashMap
sets
private MBeanInfo
mbeanInfo
Constructors Summary
public JmxIntrospectingMixin(Object target)



     
   
      this.target = target;
   
Methods Summary
public java.lang.ObjectgetAttribute(java.lang.String attribute)

      Method get = (Method) gets.get(attribute);
      if (get == null) throw new AttributeNotFoundException(attribute);
      try
      {
         return get.invoke(target, null);
      }
      catch (IllegalAccessException e)
      {
         throw new ReflectionException(e);
      }
      catch (InvocationTargetException e)
      {
         if (e.getTargetException() instanceof Exception) throw new MBeanException((Exception) e.getTargetException());
         throw new MBeanException(new Exception(e.getTargetException().getMessage()));
      }
   
public javax.management.AttributeListgetAttributes(java.lang.String[] attributes)

      AttributeList list = new AttributeList();
      for (int i = 0; i < attributes.length; i++)
      {
         try
         {
            list.add(0, new Attribute(attributes[i], getAttribute(attributes[i])));
         }
         catch (AttributeNotFoundException e)
         {
            throw new RuntimeException(e);
         }
         catch (MBeanException e)
         {
            throw new RuntimeException(e);
         }
         catch (ReflectionException e)
         {
            throw new RuntimeException(e);
         }
      }
      return list;
   
public javax.management.MBeanInfogetMBeanInfo()

      //System.out.println("******************* MBEAN INFO **********************");
      if (mbeanInfo != null)
      {
         //System.out.println("****mbeanInfo already exists***");
         return mbeanInfo;
      }


      try
      {
         Method[] methods = target.getClass().getMethods();

         //System.out.println("**** introspect attributes ****");
         for (int i = 0; i < methods.length; i++)
         {
            if (methods[i].getName().equals("_getInstanceAdvisor")) continue;
            if (methods[i].getName().equals("_setInstanceAdvisor")) continue;
            if (methods[i].getName().equals("_getAdvisor")) continue;
            if (methods[i].getName().equals("setAttribute")) continue;
            if (methods[i].getName().equals("getMBeanInfo")) continue;
            if (methods[i].getName().equals("getAttribute")) continue;
            if (methods[i].getName().equals("setAttributes")) continue;
            if (methods[i].getName().equals("getAttributes")) continue;
            if (methods[i].getName().equals("invoke")) continue;
            if (methods[i].getName().indexOf("$aop") != -1) continue;
            if (methods[i].getDeclaringClass().equals(java.lang.Object.class)) continue;
            if (methods[i].getName().startsWith("get") && methods[i].getParameterTypes().length == 0)
            {
               //System.out.println("adding get attribute: " + methods[i].getName().substring(3));
               gets.put(methods[i].getName().substring(3), methods[i]);
            }
            else if (methods[i].getName().startsWith("is") && methods[i].getParameterTypes().length == 0 &&
                    (methods[i].getReturnType().equals(Boolean.class) || methods[i].getReturnType().equals(boolean.class)))
            {
               //System.out.println("adding is attribute: " + methods[i].getName().substring(2));
               gets.put(methods[i].getName().substring(2), methods[i]);
            }
            else if (methods[i].getName().startsWith("set") && methods[i].getParameterTypes().length == 1)
            {
               //System.out.println("adding set attribute: " + methods[i].getName().substring(3));
               sets.put(methods[i].getName().substring(3), methods[i]);
            }
            else
            {
               //System.out.println("adding operation: " + methods[i].getName());
               String[] signature = new String[methods[i].getParameterTypes().length];
               for (int j = 0; j < methods[i].getParameterTypes().length; j++)
               {
                  signature[j] = methods[i].getParameterTypes()[j].getName();
               }
               ops.put(new OpsKey(methods[i].getName(), signature), methods[i]);
            }
         }

         HashMap attributes = new HashMap();
         Iterator it = gets.keySet().iterator();
         while (it.hasNext())
         {
            String attribute = (String) it.next();
            Method m = (Method) gets.get(attribute);
            //System.out.println("*** creating get: " + attribute);
            boolean isWritable = sets.containsKey(attribute);
            boolean isIs = m.getName().startsWith("is");
            MBeanAttributeInfo info = new MBeanAttributeInfo(attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, true, isWritable, isIs);
            attributes.put(attribute, info);
         }
         it = sets.keySet().iterator();
         while (it.hasNext())
         {
            String attribute = (String) it.next();
            if (gets.containsKey(attribute)) continue;
            //System.out.println("*** creating set: " + attribute);
            Method m = (Method) sets.get(attribute);
            MBeanAttributeInfo info = new MBeanAttributeInfo(attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, false, true, false);
            attributes.put(attribute, info);
         }

         MBeanOperationInfo[] operations = new MBeanOperationInfo[ops.size()];
         it = ops.values().iterator();
         int i = 0;
         while (it.hasNext())
         {
            Method m = (Method) it.next();
            operations[i++] = new MBeanOperationInfo(m.toString(), m);
         }
         MBeanAttributeInfo[] attrs = (MBeanAttributeInfo[]) attributes.values().toArray(new MBeanAttributeInfo[attributes.size()]);

         mbeanInfo = new MBeanInfo(target.getClass().getName(), target.getClass().getName(),
                 attrs, null, operations, null);
         //System.out.println("***returning MBeanInfo****");
         return mbeanInfo;
      }

      catch (Exception e)
      {
         e.printStackTrace();
         throw new RuntimeException(e);
      }
   
public java.lang.Objectinvoke(java.lang.String actionName, java.lang.Object[] params, java.lang.String[] signature)

      OpsKey key = new OpsKey(actionName, signature);
      Method m = (Method) ops.get(key);
      if (m == null) throw new NoSuchMethodError(actionName);
      try
      {
         return m.invoke(target, params);
      }
      catch (IllegalAccessException e)
      {
         throw new ReflectionException(e);
      }
      catch (InvocationTargetException e)
      {
         Throwable cause = e.getTargetException();
         if (cause instanceof Exception) throw new MBeanException((Exception) cause);
         throw new MBeanException(new Exception(e.getMessage()));
      }
   
public voidsetAttribute(javax.management.Attribute attribute)

      Method set = (Method) sets.get(attribute.getName());
      if (set == null) throw new AttributeNotFoundException(attribute.getName());
      try
      {
         Object[] args = {attribute.getValue()};
         set.invoke(target, args);
      }
      catch (IllegalAccessException e)
      {
         throw new ReflectionException(e);
      }
      catch (InvocationTargetException e)
      {
         if (e.getTargetException() instanceof Exception) throw new MBeanException((Exception) e.getTargetException());
         throw new MBeanException(new Exception(e.getTargetException().getMessage()));
      }
   
public javax.management.AttributeListsetAttributes(javax.management.AttributeList attributes)

      for (int i = 0; i < attributes.size(); i++)
      {
         try
         {
            Attribute attr = (Attribute) attributes.get(i);
            setAttribute(attr);
         }
         catch (InvalidAttributeValueException inv)
         {
            throw new RuntimeException(inv);
         }
         catch (AttributeNotFoundException e)
         {
            throw new RuntimeException(e);
         }
         catch (MBeanException e)
         {
            throw new RuntimeException(e);
         }
         catch (ReflectionException e)
         {
            throw new RuntimeException(e);
         }
      }
      return attributes;