1 22 package org.jboss.jmx.adaptor.rmi; 23 24 import java.io.Serializable ; 25 import java.lang.reflect.InvocationHandler ; 26 import javax.management.ObjectName ; 27 import javax.naming.InitialContext ; 28 29 30 37 public class RMIRemoteMBeanProxy 38 implements Serializable , InvocationHandler 39 { 40 41 private final RMIAdaptor remoteServer; 42 43 44 private final ObjectName name; 45 46 49 50 RMIRemoteMBeanProxy (final ObjectName name, final javax.management.MBeanServer server) throws Exception 51 { 52 this.name = name; 53 this.remoteServer = getRmiAdaptor (); 54 } 55 56 57 private static final Object EMPTY_ARGS[] = {}; 58 59 63 public Object invoke (final Object proxy, final java.lang.reflect.Method method, final Object [] args) throws Throwable 64 { 65 String methodName = method.getName(); 66 67 if (methodName.startsWith("get") && args == null) 69 { 70 String attrName = methodName.substring(3); 71 return remoteServer.getAttribute(name, attrName); 72 } 73 74 else if (methodName.startsWith("is") && args == null) 76 { 77 String attrName = methodName.substring(2); 78 return remoteServer.getAttribute(name, attrName); 79 } 80 81 else if (methodName.startsWith("set") && args != null && args.length == 1) 83 { 84 String attrName = methodName.substring(3); 85 remoteServer.setAttribute(name, new javax.management.Attribute (attrName, args[0])); 86 return null; 87 } 88 89 91 Class [] types = method.getParameterTypes(); 93 String [] sig = new String [types.length]; 94 for (int i = 0; i < types.length; i++) { 95 sig[i] = types[i].getName(); 96 } 97 98 return remoteServer.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig); 100 } 101 102 protected RMIAdaptor getRmiAdaptor () throws Exception 103 { 104 InitialContext ctx = new InitialContext (); 105 return (RMIAdaptor) ctx.lookup("jmx/invoker/RMIAdaptor"); 106 } 107 108 109 113 public final ObjectName getMBeanProxyObjectName() 114 { 115 return name; 116 } 117 118 public final RMIAdaptor getMBeanProxyRMIAdaptor() 119 { 120 return remoteServer; 121 } 122 123 124 128 139 public static Object create (final Class intf, final String name, final javax.management.MBeanServer server) throws Exception 140 { 141 return create(intf, new ObjectName (name), server); 142 } 143 144 152 public static Object create (final Class intf, final ObjectName name, final javax.management.MBeanServer server) throws Exception 153 { 154 return java.lang.reflect.Proxy.newProxyInstance(Thread.currentThread ().getContextClassLoader (), 155 new Class [] { intf }, 156 new RMIRemoteMBeanProxy(name, server)); 157 } 158 } 159 | Popular Tags |