1 8 9 package mx4j.util; 10 11 import java.lang.reflect.InvocationHandler ; 12 import java.lang.reflect.Method ; 13 import java.lang.reflect.Proxy ; 14 import java.util.List ; 15 16 import javax.management.Attribute ; 17 import javax.management.MBeanException ; 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanServerFactory ; 20 import javax.management.ObjectName ; 21 import javax.management.ReflectionException ; 22 import javax.management.RuntimeErrorException ; 23 import javax.management.RuntimeMBeanException ; 24 import javax.management.RuntimeOperationsException ; 25 import javax.management.InstanceNotFoundException ; 26 import javax.management.AttributeNotFoundException ; 27 import javax.management.InvalidAttributeValueException ; 28 29 53 public class StandardMBeanProxy 54 { 55 60 public static Object create(Class mbeanInterface, ObjectName name) 61 { 62 return create(mbeanInterface, null, name); 63 } 64 65 76 public static Object create(Class mbeanInterface, MBeanServer server, ObjectName name) 77 { 78 if (mbeanInterface == null) throw new IllegalArgumentException ("MBean interface cannot be null"); 79 if (!mbeanInterface.isInterface()) throw new IllegalArgumentException ("Class parameter must be an interface"); 80 if (name == null) throw new IllegalArgumentException ("MBean name cannot be null"); 81 82 if (server == null) 83 { 84 List list = MBeanServerFactory.findMBeanServer(null); 85 if (list.size() > 0) 86 server = (MBeanServer )list.get(0); 87 else 88 throw new IllegalArgumentException ("Cannot find MBeanServer"); 89 } 90 91 if (!server.isRegistered(name)) throw new IllegalArgumentException ("ObjectName " + name + " is not known to MBeanServer " + server); 94 95 ClassLoader loader = mbeanInterface.getClassLoader(); 98 return Proxy.newProxyInstance(loader, new Class []{mbeanInterface}, new LocalHandler(server, name)); 99 } 100 101 107 protected static abstract class Handler implements InvocationHandler 108 { 109 protected Handler() 110 { 111 } 112 113 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 114 { 115 if (args == null) args = new Object [0]; 117 118 121 Class [] declared = method.getExceptionTypes(); 122 123 if (Utils.isAttributeSetter(method)) 124 { 125 String name = method.getName().substring(3); 126 Attribute attribute = new Attribute (name, args[0]); 127 try 128 { 129 setAttribute(attribute); 130 return null; 131 } 132 catch (Throwable x) 133 { 134 unwrapThrowable(x, declared); 135 } 136 } 137 else if (Utils.isAttributeGetter(method)) 138 { 139 String n = method.getName(); 140 String name = null; 141 if (n.startsWith("is")) 142 name = n.substring(2); 143 else 144 name = n.substring(3); 145 146 try 147 { 148 return getAttribute(name); 149 } 150 catch (Throwable x) 151 { 152 unwrapThrowable(x, declared); 153 } 154 } 155 else 156 { 157 Class [] parameters = method.getParameterTypes(); 158 String [] params = new String [parameters.length]; 159 for (int i = 0; i < parameters.length; ++i) 160 { 161 params[i] = parameters[i].getName(); 162 } 163 164 try 165 { 166 return invokeOperation(method.getName(), args, params); 167 } 168 catch (Throwable x) 169 { 170 unwrapThrowable(x, declared); 171 } 172 } 173 174 return null; 175 } 176 177 180 protected abstract void setAttribute(Attribute attribute) throws Exception ; 181 182 185 protected abstract Object getAttribute(String attribute) throws Exception ; 186 187 190 protected abstract Object invokeOperation(String method, Object [] args, String [] params) throws Exception ; 191 192 196 protected void unwrapThrowable(Throwable x, Class [] declared) throws Throwable 197 { 198 if (declared != null) 199 { 200 for (int i = 0; i < declared.length; ++i) 203 { 204 Class exception = declared[i]; 205 if (exception.isInstance(x)) throw x; 206 } 207 } 208 209 if (x instanceof MBeanException ) 211 { 212 unwrapThrowable(((MBeanException )x).getTargetException(), declared); 213 } 214 else if (x instanceof ReflectionException ) 215 { 216 unwrapThrowable(((ReflectionException )x).getTargetException(), declared); 217 } 218 else if (x instanceof RuntimeOperationsException ) 219 { 220 unwrapThrowable(((RuntimeOperationsException )x).getTargetException(), declared); 221 } 222 else if (x instanceof RuntimeMBeanException ) 223 { 224 unwrapThrowable(((RuntimeMBeanException )x).getTargetException(), declared); 225 } 226 else if (x instanceof RuntimeErrorException ) 227 { 228 unwrapThrowable(((RuntimeErrorException )x).getTargetError(), declared); 229 } 230 else 231 { 232 throw x; 236 } 237 } 238 } 239 240 private static class LocalHandler extends Handler 241 { 242 private MBeanServer m_server; 243 private ObjectName m_name; 244 245 private LocalHandler(MBeanServer server, ObjectName name) 246 { 247 m_server = server; 248 m_name = name; 249 } 250 251 protected void setAttribute(Attribute attribute) throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 252 { 253 m_server.setAttribute(m_name, attribute); 254 } 255 256 protected Object getAttribute(String attribute) throws InstanceNotFoundException , AttributeNotFoundException , MBeanException , ReflectionException 257 { 258 return m_server.getAttribute(m_name, attribute); 259 } 260 261 protected Object invokeOperation(String method, Object [] args, String [] params) throws InstanceNotFoundException , MBeanException , ReflectionException 262 { 263 return m_server.invoke(m_name, method, args, params); 264 } 265 } 266 } 267 | Popular Tags |