package org.jboss.mx.util;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.security.PrivilegedAction;
import java.security.AccessController;
import javax.management.Attribute;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.jboss.util.NestedRuntimeException;
import org.jboss.mx.util.JMXExceptionDecoder;
public class MBeanProxyExt
implements InvocationHandler, MBeanProxyInstance
{
private final MBeanServer server;
private final ObjectName name;
private final HashMap attributeMap = new HashMap();
MBeanProxyExt(final ObjectName name, final MBeanServer server)
{
this.name = name;
this.server = server;
try
{
MBeanInfo info = server.getMBeanInfo(name);
MBeanAttributeInfo[] attributes = info.getAttributes();
for (int i = 0; i < attributes.length; ++i)
attributeMap.put(attributes[i].getName(), attributes[i]);
}
catch (Exception e)
{
throw new NestedRuntimeException("Error creating MBeanProxy: " + name, e);
}
}
private static final Object EMPTY_ARGS[] = {};
public Object invoke(final Object proxy,
final Method method,
final Object[] args)
throws Throwable
{
Class type = method.getDeclaringClass();
if (type == MBeanProxyInstance.class || type == Object.class)
{
return method.invoke(this, args);
}
String methodName = method.getName();
if (methodName.startsWith("get") && args == null)
{
String attrName = methodName.substring(3);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null)
{
String retType = method.getReturnType().getName();
if (retType.equals(info.getType()))
{
try
{
return server.getAttribute(name, attrName);
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
}
else if (methodName.startsWith("is") && args == null)
{
String attrName = methodName.substring(2);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null && info.isIs())
{
Class retType = method.getReturnType();
if (retType.equals(Boolean.class) || retType.equals(Boolean.TYPE))
{
try
{
return server.getAttribute(name, attrName);
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
}
else if (methodName.startsWith("set") && args != null && args.length == 1)
{
String attrName = methodName.substring(3);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null && method.getReturnType() == Void.TYPE)
{
try
{
server.setAttribute(name, new Attribute(attrName, args[0]));
return null;
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
Class[] types = method.getParameterTypes();
String[] sig = new String[types.length];
for (int i = 0; i < types.length; i++) {
sig[i] = types[i].getName();
}
try {
return server.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig);
}
catch (Exception e) {
throw JMXExceptionDecoder.decode(e);
}
}
public final ObjectName getMBeanProxyObjectName()
{
return name;
}
public final MBeanServer getMBeanProxyMBeanServer()
{
return server;
}
public boolean equals(Object that)
{
if (that == null) return false;
if (that == this) return true;
if (that instanceof MBeanProxyInstance)
{
MBeanProxyInstance proxy = (MBeanProxyInstance)that;
if (name.equals(proxy.getMBeanProxyObjectName()) &&
server.equals(proxy.getMBeanProxyMBeanServer()))
{
return true;
}
}
return false;
}
public int hashCode()
{
return name.hashCode() * 31 + server.hashCode();
}
public String toString()
{
StringBuffer sbuf = new StringBuffer(128);
sbuf.append("MBeanProxyExt[").append(name.toString()).append(']');
return sbuf.toString();
}
public static Object create(final Class intf, final String name)
throws MalformedObjectNameException
{
return create(intf, new ObjectName(name));
}
public static Object create(final Class intf,
final String name,
final MBeanServer server)
throws MalformedObjectNameException
{
return create(intf, new ObjectName(name), server);
}
public static Object create(final Class intf, final ObjectName name)
{
return create(intf, name, MBeanServerLocator.locateJBoss());
}
public static Object create(final Class intf,
final ObjectName name,
final MBeanServer server)
{
PrivilegedAction action = new PrivilegedAction()
{
public Object run()
{
ClassLoader cl = new ClassLoader(intf.getClassLoader())
{
public Class loadClass(final String className) throws ClassNotFoundException
{
try
{
return super.loadClass(className);
}
catch (ClassNotFoundException e)
{
if (className.equals(MBeanProxyInstance.class.getName()))
{
return MBeanProxyInstance.class.getClassLoader().loadClass(className);
}
throw e;
}
}
};
return cl;
}
};
ClassLoader cl = (ClassLoader) AccessController.doPrivileged(action);
Class[] ifaces = { MBeanProxyInstance.class, intf };
return Proxy.newProxyInstance(cl, ifaces, new MBeanProxyExt(name, server));
}
}