KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache;
8
9 import org.jboss.remoting.transport.socket.SocketClientInvoker;
10 import org.jboss.remoting.InvokerLocator;
11 import org.jboss.remoting.InvocationRequest;
12 import org.jboss.remoting.invocation.NameBasedInvocation;
13
14 import javax.management.ObjectName JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import java.lang.reflect.InvocationHandler JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18
19 /**
20  * Simple Remoting-based proxy that can be downloaded (via JNDI) into the client's address space. Uses
21  * java.lang.reflect.Proxy to handle all method calls, and uses remoteURI (identifies the server) and
22  * targetObject (identifies the target MBean on the server side) to dispatch requests using Remoting
23  * @author Bela Ban
24  * @version $Id: TreeCacheProxy.java,v 1.1.2.2 2005/04/06 21:07:11 starksm Exp $
25  */

26 public class TreeCacheProxy implements Serializable JavaDoc, InvocationHandler JavaDoc
27 {
28    static final long serialVersionUID = -4485997268761451933L;
29
30    String JavaDoc remoteURI=null;
31    SocketClientInvoker invoker;
32    ObjectName JavaDoc targetObject=null;
33
34    public TreeCacheProxy(String JavaDoc remoteURI, ObjectName JavaDoc targetObject) {
35       this.remoteURI=remoteURI;
36       this.targetObject=targetObject;
37    }
38
39
40    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
41       InvokerLocator invokerLocator=new InvokerLocator(remoteURI);
42       if(invoker == null) {
43          invoker=new SocketClientInvoker(invokerLocator);
44       }
45
46       // ObjectName name, String operationName, Object[] params, String[] signature)
47
NameBasedInvocation arg;
48       arg=new NameBasedInvocation("invoke",
49                                   new Object JavaDoc[]{targetObject, method.getName(), args, generateSignatureFromMethod(method)},
50                                   new String JavaDoc[]{ObjectName JavaDoc.class.getName(), String JavaDoc.class.getName(),
51                                   Object JavaDoc[].class.getName(), String JavaDoc[].class.getName()});
52       InvocationRequest req=new InvocationRequest("bla", "JBossCache", arg, null, null, invokerLocator);
53       return invoker.invoke(req);
54    }
55
56
57    private String JavaDoc[] generateSignatureFromMethod(final Method JavaDoc method) {
58       Class JavaDoc[] parameterTypes=method.getParameterTypes();
59       String JavaDoc[] signature=new String JavaDoc[parameterTypes.length];
60       for(int i=0; i < parameterTypes.length; i++) {
61          Class JavaDoc parameterType=parameterTypes[i];
62          signature[i]=parameterType.getName();
63       }
64       return signature;
65    }
66 }
67
Popular Tags