1 9 10 package org.jboss.cache; 11 12 import org.jboss.logging.Logger; 13 import org.jboss.remoting.InvocationRequest; 14 import org.jboss.remoting.InvokerCallbackHandler; 15 import org.jboss.remoting.ServerInvocationHandler; 16 import org.jboss.remoting.ServerInvoker; 17 import org.jboss.remoting.ident.Identity; 18 import org.jboss.remoting.invocation.NameBasedInvocation; 19 20 import javax.management.*; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.UndeclaredThrowableException ; 24 25 33 public class TreeCacheInvocationHandler implements ServerInvocationHandler { 34 private static final Logger log=Logger.getLogger(TreeCacheInvocationHandler.class); 35 private MBeanServer server; 36 private Identity identity; 37 38 private static Method getObjectInstance; 39 private static Method isRegistered; 40 private static Method getAttribute; 41 private static Method getAttributes; 42 private static Method setAttribute; 43 private static Method setAttributes; 44 private static Method invoke; 45 private static Method getMBeanInfo; 46 47 static { 48 try { 49 Class LObject=(new Object [0]).getClass(); 50 Class LString=(new String [0]).getClass(); 51 52 Class [] Sig_ObjectName=new Class []{ObjectName.class}; 53 Class [] Sig_ObjectName_String=new Class []{ObjectName.class, String .class}; 54 Class [] Sig_ObjectName_LString=new Class []{ObjectName.class, LString}; 55 Class [] Sig_ObjectName_Attribute=new Class []{ObjectName.class, Attribute.class}; 56 Class [] Sig_ObjectName_AttributeList=new Class []{ObjectName.class, AttributeList.class}; 57 Class [] Sig_ObjectName_String_LObject_LString=new Class []{ObjectName.class, String .class, LObject, LString}; 58 59 getObjectInstance=MBeanServer.class.getMethod("getObjectInstance", Sig_ObjectName); 60 isRegistered=MBeanServer.class.getMethod("isRegistered", Sig_ObjectName); 61 getAttribute=MBeanServer.class.getMethod("getAttribute", Sig_ObjectName_String); 62 getAttributes=MBeanServer.class.getMethod("getAttributes", Sig_ObjectName_LString); 63 setAttribute=MBeanServer.class.getMethod("setAttribute", Sig_ObjectName_Attribute); 64 setAttributes=MBeanServer.class.getMethod("setAttributes", Sig_ObjectName_AttributeList); 65 invoke=MBeanServer.class.getMethod("invoke", Sig_ObjectName_String_LObject_LString); 66 getMBeanInfo=MBeanServer.class.getMethod("getMBeanInfo", Sig_ObjectName); 67 } 68 catch(Exception e) { 69 throw new RuntimeException ("Error resolving methods", e); 70 } 71 } 72 73 public TreeCacheInvocationHandler() { 74 super(); 75 } 76 77 82 public void setMBeanServer(MBeanServer server) { 83 if(server == null) 84 return; 85 this.server=server; 86 identity=Identity.get(server); 87 if(log.isDebugEnabled()) { 89 log.debug("setMBeanServer called with: " + server + " with identity: " + identity); 90 } 91 } 92 93 public void setInvoker(ServerInvoker invoker) { 94 } 95 96 97 98 public Object invoke(InvocationRequest invocation) throws Throwable { 99 if(this.server == null) { 100 throw new IllegalStateException ("invoke called prior to mbean server being set"); 101 } 102 try { 103 NameBasedInvocation nbi=(NameBasedInvocation)invocation.getParameter(); 104 String methodName=nbi.getMethodName(); 105 Object args []=nbi.getParameters(); 106 String signature []=nbi.getSignature(); 107 108 Object _args[]=(args == null && signature != null) ? new Object [signature.length] : args; 109 Method method=getMethod(methodName, signature); 111 return method.invoke(server, _args); 113 } 114 catch(Throwable ex) { 115 if(ex instanceof UndeclaredThrowableException ) { 116 UndeclaredThrowableException ut=(UndeclaredThrowableException )ex; 117 Throwable ute=ut.getUndeclaredThrowable(); 118 if(ute instanceof Exception ) { 119 throw new MBeanException((Exception )ute, ut.getUndeclaredThrowable().getMessage()); 120 } 121 else { 122 throw new MBeanException(new Exception (ute.getMessage()), ute.getMessage()); 123 } 124 } 125 if(ex instanceof InvocationTargetException ) 126 throw ((InvocationTargetException )ex).getTargetException(); 127 throw ex; 128 } 129 } 130 131 public void addListener(InvokerCallbackHandler callbackHandler) { 132 } 133 134 public void removeListener(InvokerCallbackHandler callbackHandler) { 135 } 136 137 145 private Method getMethod(String methodName, String sig[]) 146 throws Throwable { 147 if(methodName.equals("invoke")) 148 return invoke; 149 else if(methodName.equals("getAttribute")) 150 return getAttribute; 151 else if(methodName.equals("setAttribute")) 152 return setAttribute; 153 else if(methodName.equals("getAttributes")) 154 return getAttributes; 155 else if(methodName.equals("setAttributes")) 156 return setAttributes; 157 else if(methodName.equals("setAttributes")) 158 return setAttributes; 159 else if(methodName.equals("getMBeanInfo")) 160 return getMBeanInfo; 161 else if(methodName.equals("getObjectInstance")) 162 return getObjectInstance; 163 else if(methodName.equals("isRegistered")) 164 return isRegistered; 165 166 Class [] params=null; 167 if(sig != null) { 168 params=new Class [sig.length]; 169 for(int i=0; i < sig.length; ++i) 170 params[i]=Class.forName(sig[i]); 171 } 172 return MBeanServer.class.getMethod(methodName, params); 173 } 174 175 } 176 | Popular Tags |