1 22 package org.jboss.mx.util; 23 24 import javax.management.*; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.UndeclaredThrowableException ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 38 final class MBeanTyperInvoker implements java.lang.reflect.InvocationHandler 39 { 40 private final MBeanServer server; 41 private final ObjectName mbean; 42 private final Map signatureCache = Collections.synchronizedMap(new HashMap ()); 43 44 MBeanTyperInvoker(MBeanServer server, ObjectName mbean) 45 { 46 this.server = server; 47 this.mbean = mbean; 48 } 49 50 private boolean isJMXAttribute(Method m) 51 { 52 String name = m.getName(); 53 return (name.startsWith("get")); 54 55 } 56 57 public Object invoke(Object proxy, Method method, Object [] args) 58 throws Throwable 59 { 60 if (MBeanTyper.DEBUG) 61 { 62 System.err.println(" ++ method=" + method.getName() + ",args=" + args); 63 } 64 try 65 { 66 if (method.getDeclaringClass() == Object .class) 67 { 68 String name = method.getName(); 69 if (name.equals("hashCode")) 70 { 71 return new Integer (this.hashCode()); 72 } 73 else if (name.equals("toString")) 74 { 75 return this.toString(); 76 } 77 else if (name.equals("equals")) 78 { 79 return new Boolean (equals(args[0])); 82 } 83 } 84 else if (isJMXAttribute(method) && (args == null || args.length <= 0)) 85 { 86 String name = method.getName().substring(3); 87 return server.getAttribute(mbean, name); 88 } 89 90 String sig[] = (String []) signatureCache.get(method); 91 if (sig == null) 92 { 93 Class _args[] = method.getParameterTypes(); 97 if (_args != null && _args.length > 0) 98 { 99 sig = new String [_args.length]; 100 for (int c = 0; c < sig.length; c++) 101 { 102 if (_args[c] != null) 103 { 104 sig[c] = _args[c].getName(); 105 } 106 } 107 } 108 else 109 { 110 sig = new String [0]; 111 } 112 signatureCache.put(method, sig); 113 } 114 return server.invoke(mbean, method.getName(), args, sig); 115 } 116 catch (Throwable t) 117 { 118 if (MBeanTyper.DEBUG) 119 { 120 t.printStackTrace(); 121 } 122 if (t instanceof UndeclaredThrowableException ) 123 { 124 UndeclaredThrowableException ut = (UndeclaredThrowableException ) t; 125 throw ut.getUndeclaredThrowable(); 126 } 127 else if (t instanceof InvocationTargetException ) 128 { 129 InvocationTargetException it = (InvocationTargetException ) t; 130 throw it.getTargetException(); 131 } 132 else if (t instanceof MBeanException) 133 { 134 MBeanException me=(MBeanException)t; 135 throw me.getTargetException(); 136 } 137 else 138 { 139 throw t; 140 } 141 } 142 } 143 } 144 | Popular Tags |