package org.jboss.mx.metadata;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import java.lang.reflect.Method;
import java.util.HashMap;
public class MethodMapper
{
private HashMap map = null;
public MethodMapper(Class resourceClass)
{
if (null == resourceClass)
{
throw new IllegalArgumentException("resourceClass cannot be null");
}
map = createMap(resourceClass);
}
public Method lookupOperation(MBeanOperationInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanOperationInfo cannot be null");
}
return (Method) map.get(operationSignature(info));
}
public Method lookupGetter(MBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanAttributeInfo cannot be null");
}
return (Method) map.get(getterSignature(info));
}
public Method lookupGetter(ModelMBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanAttributeInfo cannot be null");
}
return (Method) map.get(getterSignature(info));
}
public Method lookupSetter(MBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanAttributeInfo cannot be null");
}
return (Method) map.get(setterSignature(info));
}
public Method lookupSetter(ModelMBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null");
}
return (Method) map.get(setterSignature(info));
}
public Method lookupMethod(String returnType, String name, String[] signature)
{
if (null == returnType)
{
throw new IllegalArgumentException("returnType cannot be null");
}
if (null == name)
{
throw new IllegalArgumentException("method name cannot be null");
}
return (Method) map.get(methodSignature(returnType, name, signature));
}
public static String getterSignature(MBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanAttributeInfo cannot be null");
}
String prefix = (info.isIs()) ? "is" : "get";
return methodSignature(info.getType(), prefix + info.getName(), null);
}
public static String getterSignature(ModelMBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null");
}
String methodName = (String) info.getDescriptor().getFieldValue("getMethod");
if (null == methodName)
{
throw new IllegalArgumentException("getMethod not defined in ModelMBeanAttributeInfo descriptor");
}
return methodSignature(info.getType(), methodName, null);
}
public static String setterSignature(MBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanAttributeInfo cannot be null");
}
return methodSignature(Void.TYPE.getName(), "set" + info.getName(), new String[]{info.getType()});
}
public static String setterSignature(ModelMBeanAttributeInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null");
}
String methodName = (String) info.getDescriptor().getFieldValue("setMethod");
if (null == methodName)
{
throw new IllegalArgumentException("setMethod not defined in ModelMBeanAttributeInfo descriptor");
}
return methodSignature(Void.TYPE.getName(), methodName, new String[]{info.getType()});
}
public static String operationSignature(MBeanOperationInfo info)
{
if (null == info)
{
throw new IllegalArgumentException("MBeanOperationInfo cannot be null");
}
MBeanParameterInfo[] params = info.getSignature();
String[] signature = new String[params.length];
for (int i = 0; i < signature.length; i++)
{
signature[i] = params[i].getType();
}
return methodSignature(info.getReturnType(), info.getName(), signature);
}
public static String methodSignature(Method method)
{
if (null == method)
{
throw new IllegalArgumentException("Method cannot be null");
}
Class[] paramtypes = method.getParameterTypes();
String[] signature = new String[paramtypes.length];
for (int i = 0; i < signature.length; i++)
{
signature[i] = paramtypes[i].getName();
}
return methodSignature(method.getReturnType().getName(), method.getName(), signature);
}
public static String methodSignature(String returnType, String name, String[] signature)
{
if (null == returnType)
{
throw new IllegalArgumentException("returnType cannot be null");
}
if (null == name)
{
throw new IllegalArgumentException("method name cannot be null");
}
StringBuffer buf = new StringBuffer(returnType).append(';').append(name);
if (null == signature)
{
return buf.toString();
}
for (int i = 0; i < signature.length; i++)
{
buf.append(';').append(signature[i]); }
return buf.toString();
}
protected HashMap createMap(Class resourceClass)
{
HashMap cmap = new HashMap();
Method[] methods = resourceClass.getMethods();
for (int i = 0; i < methods.length; i++)
{
cmap.put(methodSignature(methods[i]), methods[i]);
}
return cmap;
}
}