KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > TreeCacheInvocationHandler


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

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 JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
24
25 /**
26  * TreeCacheInvocationHandler is a ServerInvocationHandler that will forward requests to the
27  * MBeanServer and return the results from the MBeanServer.
28  *
29  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
30  * @author Bela Ban
31  * @version $Id: TreeCacheInvocationHandler.java,v 1.1 2004/12/02 15:40:41 belaban Exp $
32  */

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 JavaDoc getObjectInstance;
39    private static Method JavaDoc isRegistered;
40    private static Method JavaDoc getAttribute;
41    private static Method JavaDoc getAttributes;
42    private static Method JavaDoc setAttribute;
43    private static Method JavaDoc setAttributes;
44    private static Method JavaDoc invoke;
45    private static Method JavaDoc getMBeanInfo;
46
47    static {
48       try {
49          Class JavaDoc LObject=(new Object JavaDoc[0]).getClass();
50          Class JavaDoc LString=(new String JavaDoc[0]).getClass();
51
52          Class JavaDoc[] Sig_ObjectName=new Class JavaDoc[]{ObjectName.class};
53          Class JavaDoc[] Sig_ObjectName_String=new Class JavaDoc[]{ObjectName.class, String JavaDoc.class};
54          Class JavaDoc[] Sig_ObjectName_LString=new Class JavaDoc[]{ObjectName.class, LString};
55          Class JavaDoc[] Sig_ObjectName_Attribute=new Class JavaDoc[]{ObjectName.class, Attribute.class};
56          Class JavaDoc[] Sig_ObjectName_AttributeList=new Class JavaDoc[]{ObjectName.class, AttributeList.class};
57          Class JavaDoc[] Sig_ObjectName_String_LObject_LString=new Class JavaDoc[]{ObjectName.class, String JavaDoc.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 JavaDoc e) {
69          throw new RuntimeException JavaDoc("Error resolving methods", e);
70       }
71    }
72
73    public TreeCacheInvocationHandler() {
74       super();
75    }
76
77    /**
78     * set the mbean server that the handler can reference
79     *
80     * @param server
81     */

82    public void setMBeanServer(MBeanServer server) {
83       if(server == null)
84          return;
85       this.server=server;
86       identity=Identity.get(server);
87       // make sure our local server is set
88
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 JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc {
99       if(this.server == null) {
100          throw new IllegalStateException JavaDoc("invoke called prior to mbean server being set");
101       }
102       try {
103          NameBasedInvocation nbi=(NameBasedInvocation)invocation.getParameter();
104          String JavaDoc methodName=nbi.getMethodName();
105          Object JavaDoc args []=nbi.getParameters();
106          String JavaDoc signature []=nbi.getSignature();
107
108          Object JavaDoc _args[]=(args == null && signature != null) ? new Object JavaDoc[signature.length] : args;
109          // get the mbean server method that's being invoked
110
Method JavaDoc method=getMethod(methodName, signature);
111          // transport against the mbean server
112
return method.invoke(server, _args);
113       }
114       catch(Throwable JavaDoc ex) {
115          if(ex instanceof UndeclaredThrowableException JavaDoc) {
116             UndeclaredThrowableException JavaDoc ut=(UndeclaredThrowableException JavaDoc)ex;
117             Throwable JavaDoc ute=ut.getUndeclaredThrowable();
118             if(ute instanceof Exception JavaDoc) {
119                throw new MBeanException((Exception JavaDoc)ute, ut.getUndeclaredThrowable().getMessage());
120             }
121             else {
122                throw new MBeanException(new Exception JavaDoc(ute.getMessage()), ute.getMessage());
123             }
124          }
125          if(ex instanceof InvocationTargetException JavaDoc)
126             throw ((InvocationTargetException JavaDoc)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    /**
138     * convenience method to lookup the Method object for a given method and signature
139     *
140     * @param methodName
141     * @param sig
142     * @return
143     * @throws java.lang.Throwable
144     */

145    private Method JavaDoc getMethod(String JavaDoc methodName, String JavaDoc sig[])
146          throws Throwable JavaDoc {
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 JavaDoc[] params=null;
167       if(sig != null) {
168          params=new Class JavaDoc[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