{
try
{
// First check the mbean instance implements the interface
if (mbeanInterface == null)
throw new NotCompliantMBeanException("The mbean does not implement a management interface");
if (mbeanInstance != null && mbeanInterface.isInstance(mbeanInstance) == false)
throw new NotCompliantMBeanException("The mbean does not implement its management interface " +
mbeanInterface.getName());
// First build the constructors
Constructor[] constructors = mbeanClass.getConstructors();
MBeanConstructorInfo[] constructorInfo = new MBeanConstructorInfo[constructors.length];
for (int i = 0; i < constructors.length; ++i)
{
constructorInfo[i] = new MBeanConstructorInfo("MBean Constructor.", constructors[i]);
}
// Next we have to figure out how the methods in the mbean class map
// to attributes and operations
Method[] methods = mbeanInterface.getMethods();
HashMap getters = new HashMap();
HashMap setters = new HashMap();
HashMap operInfo = new HashMap();
List attrInfo = new ArrayList();
for (int i = 0; i < methods.length; ++i)
{
String methodName = methods[i].getName();
Class[] signature = methods[i].getParameterTypes();
Class returnType = methods[i].getReturnType();
if (methodName.startsWith("set") && signature.length == 1 && returnType == Void.TYPE)
{
String key = methodName.substring(3, methodName.length());
Method setter = (Method) setters.get(key);
if (setter != null && setter.getParameterTypes()[0].equals(signature[0]) == false)
{
throw new IntrospectionException("overloaded type for attribute set: " + key);
}
setters.put(key, methods[i]);
}
else if (methodName.startsWith("get") && signature.length == 0 && returnType != Void.TYPE)
{
String key = methodName.substring(3, methodName.length());
Method getter = (Method) getters.get(key);
if (getter != null && getter.getName().startsWith("get") == false)
{
throw new IntrospectionException("mixed use of get/is for attribute " + key);
}
getters.put(key, methods[i]);
}
else if (methodName.startsWith("is") && signature.length == 0 && (returnType == Boolean.class || returnType == Boolean.TYPE))
{
String key = methodName.substring(2, methodName.length());
Method getter = (Method) getters.get(key);
if (getter != null && getter.getName().startsWith("is") == false)
{
throw new IntrospectionException("mixed use of get/is for attribute " + key);
}
getters.put(key, methods[i]);
}
else
{
MBeanOperationInfo info = new MBeanOperationInfo("MBean Operation.", methods[i]);
operInfo.put(getSignatureString(methods[i]), info);
}
}
Object[] keys = getters.keySet().toArray();
for (int i = 0; i < keys.length; ++i)
{
String attrName = (String) keys[i];
Method getter = (Method) getters.remove(attrName);
Method setter = (Method) setters.remove(attrName);
MBeanAttributeInfo info = new MBeanAttributeInfo(attrName, "MBean Attribute.", getter, setter);
attrInfo.add(info);
}
Iterator it = setters.keySet().iterator();
while (it.hasNext())
{
String attrName = (String) it.next();
Method setter = (Method) setters.get(attrName);
MBeanAttributeInfo info = new MBeanAttributeInfo(attrName, "MBean Attribute.", null, setter);
attrInfo.add(info);
}
// save away the attribute and operation info objects
MBeanAttributeInfo[] attributeInfo = (MBeanAttributeInfo[]) attrInfo.toArray(new MBeanAttributeInfo[0]);
MBeanOperationInfo[] operationInfo = (MBeanOperationInfo[]) operInfo.values().toArray(new MBeanOperationInfo[0]);
// if the builder was initialized with the resource instance, check if
// it is a notification broadcaster, and add the appropriate notifications
// to the interface.
MBeanNotificationInfo[] notifications = null;
if (mbeanInstance instanceof NotificationBroadcaster)
{
notifications = ((NotificationBroadcaster) mbeanInstance).getNotificationInfo();
}
else
{
notifications = new MBeanNotificationInfo[0];
}
return new MBeanInfo(mbeanClass.getName(), "Management Bean.",
attributeInfo, constructorInfo, operationInfo, notifications);
}
catch (IntrospectionException e)
{
throw new NotCompliantMBeanException(e.getMessage());
}
}