package org.jboss.mx.capability;
import org.jboss.mx.metadata.AttributeOperationResolver;
import org.jboss.mx.metadata.MethodMapper;
import org.jboss.mx.server.ServerConstants;
import org.jboss.mx.util.PropertyAccess;
import javax.management.DynamicMBean;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.Descriptor;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import java.lang.reflect.Method;
public class DispatcherFactory
implements ServerConstants
{
public static DynamicMBean create(MBeanInfo info, Object resource) throws IntrospectionException
{
return create(info, resource, new AttributeOperationResolver(info));
}
public static DynamicMBean create(MBeanInfo info, Object resource, AttributeOperationResolver resolver) throws IntrospectionException
{
if (null == info)
{
throw new IllegalArgumentException("info cannot be null");
}
if (null == resolver)
{
throw new IllegalArgumentException("resolver cannot be null");
}
if (null == resource)
{
throw new IllegalArgumentException("resource cannot be null");
}
MethodMapper mmap = new MethodMapper(resource.getClass());
ReflectedMBeanDispatcher dispatcher = new ReflectedMBeanDispatcher(info, resolver, resource);
String flag = PropertyAccess.getProperty(OPTIMIZE_REFLECTED_DISPATCHER, "false");
if (flag.equalsIgnoreCase("true"))
{
dispatcher = OptimizedMBeanDispatcher.create(info, resource );
}
MBeanAttributeInfo[] attributes = info.getAttributes();
for (int i = 0; i < attributes.length; i++)
{
MBeanAttributeInfo attribute = attributes[i];
Method getter = null;
Method setter = null;
if (attribute.isReadable())
{
if (attribute instanceof ModelMBeanAttributeInfo)
{
ModelMBeanAttributeInfo mmbAttribute = (ModelMBeanAttributeInfo) attribute;
Descriptor desc = mmbAttribute.getDescriptor();
if (desc != null && desc.getFieldValue("getMethod") != null)
{
getter = mmap.lookupGetter(mmbAttribute);
if (getter == null)
{
throw new IntrospectionException("no getter method found for attribute: " + attribute.getName());
}
}
}
else
{
getter = mmap.lookupGetter(attribute);
if (getter == null)
{
throw new IntrospectionException("no getter method found for attribute: " + attribute.getName());
}
}
}
if (attribute.isWritable())
{
if (attribute instanceof ModelMBeanAttributeInfo)
{
ModelMBeanAttributeInfo mmbAttribute = (ModelMBeanAttributeInfo) attribute;
Descriptor desc = mmbAttribute.getDescriptor();
if (desc != null && desc.getFieldValue("setMethod") != null)
{
setter = mmap.lookupSetter(mmbAttribute);
if (setter == null)
{
throw new IntrospectionException("no setter method found for attribute: " + attribute.getName());
}
}
}
else
{
setter = mmap.lookupSetter(attribute);
if (setter == null)
{
throw new IntrospectionException("no setter method found for attribute: " + attribute.getName());
}
}
}
dispatcher.bindAttributeAt(i, getter, setter);
}
MBeanOperationInfo[] operations = info.getOperations();
for (int i = 0; i < operations.length; i++)
{
MBeanOperationInfo operation = operations[i];
Method method = mmap.lookupOperation(operation);
if (method == null)
{
throw new IntrospectionException("no method found for operation: " + operation.getName()); }
dispatcher.bindOperationAt(i, method);
}
return dispatcher;
}
}