| Method from org.jboss.mx.util.MBeanProxyExt Detail: |
public static Object create(Class intf,
String name) throws MalformedObjectNameException {
return create(intf, new ObjectName(name));
}
|
public static Object create(Class intf,
ObjectName name) {
return create(intf, name, MBeanServerLocator.locateJBoss());
}
|
public static Object create(Class intf,
String name,
MBeanServer server) throws MalformedObjectNameException {
return create(intf, new ObjectName(name), server);
}
|
public static Object create(Class intf,
ObjectName name,
MBeanServer server) {
return create(intf, name, server, false);
}
|
public static Object create(Class intf,
ObjectName name,
MBeanServer server,
boolean lazyInit) {
// CL which delegates to MBeanProxyInstance's cl for it's class resolution
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)
{
// only allow loading of MBeanProxyInstance from this loader
if (className.equals(MBeanProxyInstance.class.getName()))
{
return MBeanProxyInstance.class.getClassLoader().loadClass(className);
}
// was some other classname, throw the CNFE
throw e;
}
}
};
return cl;
}
};
ClassLoader cl = (ClassLoader) AccessController.doPrivileged(action);
Class[] ifaces = {MBeanProxyInstance.class, intf};
InvocationHandler handler = new MBeanProxyExt(name, server, lazyInit);
return Proxy.newProxyInstance(cl, ifaces, handler);
}
|
public boolean equals(Object that) {
if (that == null) return false;
if (that == this) return true;
// check if 'that' is an MBeanProxyExt or a Proxy instance
// that implements the MBeanProxyInstance interface
if (that instanceof MBeanProxyInstance)
{
MBeanProxyInstance proxy = (MBeanProxyInstance) that;
// assume equality if both the MBeanServer and ObjectName match
if (name.equals(proxy.getMBeanProxyObjectName()) &&
server.equals(proxy.getMBeanProxyMBeanServer()))
{
return true;
}
}
return false;
}
We need to override this because by default equals returns false when
called on the proxy object and then relayed here. |
public final MBeanServer getMBeanProxyMBeanServer() {
if (server instanceof MBeanServer == false)
throw new IllegalStateException("This operation is not available for an MBeanServerConnection");
return (MBeanServer) server;
}
|
public final MBeanServerConnection getMBeanProxyMBeanServerConnection() {
return server;
}
|
public final ObjectName getMBeanProxyObjectName() {
return name;
}
|
public int hashCode() {
return name.hashCode() * 31 + server.hashCode();
}
As with equals, use the MBeanServer + ObjectName to calculate the
hashCode |
public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
// if the method belongs to ProxyInstance, then invoke locally
Class type = method.getDeclaringClass();
if (type == MBeanProxyInstance.class || type == Object.class)
{
return method.invoke(this, args);
}
String methodName = method.getName();
// Get attribute
if (methodName.startsWith("get") && args == null)
{
if (inited == false)
init();
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);
}
}
}
}
// Is attribute
else if (methodName.startsWith("is") && args == null)
{
if (inited == false)
init();
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);
}
}
}
}
// Set attribute
else if (methodName.startsWith("set") && args != null && args.length == 1)
{
if (inited == false)
init();
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);
}
}
}
// Operation
// convert the parameter types to strings for JMX
Class[] types = method.getParameterTypes();
String[] sig = new String[types.length];
for (int i = 0; i < types.length; i++)
{
sig[i] = types[i].getName();
}
// invoke the server and decode JMX exceptions
try
{
return server.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig);
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
Invoke the configured MBean via the target MBeanServer and decode any
resulting JMX exceptions that are thrown. |
public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException {
name = (ObjectName) in.readObject();
server = (MBeanServerConnection) in.readObject();
}
|
public String toString() {
StringBuffer sbuf = new StringBuffer(128);
sbuf.append("MBeanProxyExt[").append(name.toString()).append(']");
return sbuf.toString();
}
avoid the default printout, e.g. org.jboss.mx.util.MBeanProxyExt@120540c |
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
if (remote != null)
out.writeObject(remote);
else
out.writeObject(server); // This will fail for a normal MBeanServer
}
|