}
protected MBeanInfo generateMBeanInfo (Class[] intfs)
throws IntrospectionException
{
MBeanInfo result = super.getMBeanInfo();
if (intfs != null && intfs.length > 0)
{
ArrayList attrs = new ArrayList (Arrays.asList(result.getAttributes()));
ArrayList ops = new ArrayList (Arrays.asList(result.getOperations()));
HashMap readAttr = new HashMap ();
HashMap writeAttr = new HashMap ();
// we now populate the MBeanInfo with information from our script
//
for (int i=0; i<intfs.length; i++)
{
Class clazz = intfs[i];
Method[] methods = clazz.getMethods();
for (int m=0; m<methods.length; m++)
{
Method meth = methods[m];
String name = meth.getName();
Class[] params = meth.getParameterTypes();
if (name.startsWith("get") && params.length == 0)
{
readAttr.put (name, meth);
}
else if (name.startsWith("set") && params.length == 1)
{
writeAttr.put (name, meth);
}
else
{
ops.add(new MBeanOperationInfo
(
"Method " + name + " from class/interface " + clazz.getName(), meth
)
);
}
}
}
// we now combine the getters and setters in single RW attributes
//
Iterator readKeys = readAttr.keySet().iterator();
while (readKeys.hasNext())
{
String getter = (String)readKeys.next();
Method getterMethod = (Method)readAttr.get( getter );
String attribute = getter.substring(3);
String setter = "set" + attribute;
Method setterMethod = (Method)writeAttr.remove(setter);
attrs.add (new MBeanAttributeInfo (attribute, "", getterMethod, setterMethod));
}
// we add the remaining WO attributes
//
Iterator writeKeys = writeAttr.keySet().iterator();
while (writeKeys.hasNext())
{
String setter = (String)writeKeys.next();
Method setterMethod = (Method)writeAttr.get( setter );
String attribute = setter.substring(3);
attrs.add (new MBeanAttributeInfo (attribute, "", null, setterMethod));
}
result = new MBeanInfo(this.name,
"Dynamic MBean Service around BSH script " + this.name,
(MBeanAttributeInfo[])attrs.toArray(new MBeanAttributeInfo[attrs.size()]),
result.getConstructors(),
(MBeanOperationInfo[])ops.toArray(new MBeanOperationInfo[ops.size()]),
result.getNotifications());
}
return result;
}