FileDocCategorySizeDatePackage
MBeanInfoBuilder.javaAPI DocGlassfish v2 API8018Fri May 04 22:34:08 BST 2007com.sun.enterprise.admin.server.core.mbean.meta

MBeanInfoBuilder

public class MBeanInfoBuilder extends Object
A Class to build the MBeanInfo by providing the class name. Does not validate whether the class passed is an MBean. Things to note is that it provides the data in the form of JMX *Info Data Structures.
author
Kedar Mhaswade
version
1.0

Fields Summary
public static final String
kSetterPrefix
public static final String
kGetterPrefix
private Class
mClass
private MBeanOperationInfo[]
mOperations
private MBeanAttributeInfo[]
mAttributes
private MBeanConstructorInfo[]
mConstructors
private MBeanNotificationInfo[]
mNotifications
private MBeanInfo
mMBeanInfo
Constructors Summary
public MBeanInfoBuilder(Class aClass)
Creates new MBeanInfoBuilder


	 		  	
	
        
	
		if(aClass == null)
		{
			throw new IllegalArgumentException();
		}
		mClass = aClass;
		createOperations();
		createConstructors();
		createAttributes();
		createMBeanInfo();
		createNotifications();
    
public MBeanInfoBuilder(String aClassName)

	
public MBeanInfoBuilder(String aClassName, ClassLoader cl)

	
Methods Summary
private voidcreateAttributes()
This will only give the attributes in getX or setX style. Inheritence is not supported in here.

		Vector attrVector		= new Vector();
		Method[] declMethods	= new Introspector(mClass).getDeclaredConcretePublicMethods();
		Method[] getters		= this.getGetters(declMethods);
		Method[] setters		= this.getSetters(declMethods);
		
		for (int i = 0 ; i < getters.length ; i ++)
		{
			MBeanAttributeInfo attr		= null;
			String methodName			= getters[i].getName();
			String attrName				= getAttributeNameFromGetter(methodName);
			Method getter				= findAttrIn(attrName, getters);
			Method setter				= findAttrIn(attrName, setters);
			boolean isReadable			= ( getter != null );
			boolean isWritable			= ( setter != null );
			boolean isIs				= false;
			
			attr = new MBeanAttributeInfo(attrName, null, attrName,	
				isReadable, isWritable, isIs);
			attrVector.add(attr);
		}
		mAttributes = new MBeanAttributeInfo[attrVector.size()];
		attrVector.toArray(mAttributes);
	
private voidcreateConstructors()

		Constructor[] ctors = mClass.getConstructors();
		mConstructors = new MBeanConstructorInfo[ctors.length];
		for (int i = 0 ; i < ctors.length ; i++)
		{
			String					ctorDesc = ctors[i].getName();
			MBeanConstructorInfo	ctorInfo = 
				new MBeanConstructorInfo(ctorDesc, ctors[i]);
			mConstructors[i] = ctorInfo;
		}
	
private voidcreateMBeanInfo()

		mMBeanInfo = new MBeanInfo(mClass.getName(), 
			mClass.getName(),
			mAttributes, mConstructors, mOperations, mNotifications);
	
private voidcreateNotifications()

	
private voidcreateOperations()

		Collection excludeList	= new Vector();
		excludeList.add("java.lang.Object");
		excludeList.add("com.sun.enterprise.admin.server.core.mbean.config.AdminBase");
		Introspector reflector = new Introspector(mClass);
		Method[] methods = reflector.getCallableInstanceMethods(excludeList);
		Vector oprVector = new Vector();
		for (int i = 0 ; i < methods.length ; i++)
		{
			String name		= methods[i].getName();
			boolean isGetter = name.startsWith(kGetterPrefix);
			boolean isSetter = name.startsWith(kSetterPrefix);
			if (!isGetter && !isSetter)
			{
				oprVector.add(new MBeanOperationInfo(name, methods[i]));
			}
		}
		mOperations = new MBeanOperationInfo[oprVector.size()];
		oprVector.toArray (mOperations);
	
private java.lang.reflect.MethodfindAttrIn(java.lang.String attrName, java.lang.reflect.Method[] methods)

		Method matcher = null;
		boolean found = false;
		
		for (int i = 0 ; i < methods.length ; i++)
		{
			String methodName = methods[i].getName();
			if (methodName.endsWith(attrName))
			{
				matcher = methods[i];
				break;
			}
		}
		return ( matcher );
	
private java.lang.StringgetAttributeNameFromGetter(java.lang.String getterName)

		int getStartsAt = getterName.indexOf(kGetterPrefix);
		int attributeStartsAt = getStartsAt + kGetterPrefix.length();

		return ( getterName.substring (attributeStartsAt) );
	
private java.lang.reflect.Method[]getGetters(java.lang.reflect.Method[] methods)

		Vector getters = new Vector();
		for (int i = 0 ; i < methods.length ; i++)
		{
			String methodName = methods[i].getName();
			if (methodName.startsWith (kGetterPrefix))
			{
				getters.add (methods[i]);
			}
		}
		Method[] getterMethods = new Method[getters.size()];
		
		return ( (Method[]) getters.toArray (getterMethods) );
	
public javax.management.MBeanAttributeInfo[]getMBeanAttributes()

		return ( mAttributes );
	
public javax.management.MBeanConstructorInfo[]getMBeanConstructors()

		return ( mConstructors );
	
public javax.management.MBeanInfogetMBeanInfo()

		return ( mMBeanInfo );
	
public javax.management.MBeanOperationInfo[]getMBeanOperations()
Returns only the public methods in the given MBean. These are the operations that can be called on this MBean with invoke method.

return
array of MBeanOperationInfo instances. If there is no such method that can be called an array with 0 elements is returned.

		return ( mOperations );
	
private java.lang.reflect.Method[]getSetters(java.lang.reflect.Method[] methods)

		Vector setters = new Vector();
		for (int i = 0 ; i < methods.length ; i++)
		{
			String methodName = methods[i].getName();
			if (methodName.startsWith (kSetterPrefix))
			{
				setters.add (methods[i]);
			}
		}
		Method[] setterMethods = new Method[setters.size()];
		
		return ( (Method[]) setters.toArray (setterMethods) );