KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > performance > synchronous > PerformanceInvocationHandler


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.test.remoting.performance.synchronous;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.logging.Logger;
14 import org.jboss.remoting.InvocationRequest;
15 import org.jboss.remoting.ServerInvocationHandler;
16 import org.jboss.remoting.ServerInvoker;
17 import org.jboss.remoting.callback.InvokerCallbackHandler;
18 import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
19 import org.jboss.remoting.invocation.RemoteInvocation;
20
21 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
22 import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock;
23 import EDU.oswego.cs.dl.util.concurrent.SyncList;
24
25 /**
26  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
27  */

28 public class PerformanceInvocationHandler implements ServerInvocationHandler
29 {
30    private ServerInvoker invoker;
31
32    private List JavaDoc listeners = new SyncList(new ArrayList JavaDoc(), new FIFOReadWriteLock());
33    private Map JavaDoc callTrackers = new ConcurrentHashMap();
34
35    private static final Logger log = Logger.getLogger(PerformanceInvocationHandler.class);
36
37
38    /**
39     * set the mbean server that the handler can reference
40     *
41     * @param server
42     */

43    public void setMBeanServer(MBeanServer JavaDoc server)
44    {
45    }
46
47    /**
48     * set the invoker that owns this handler
49     *
50     * @param invoker
51     */

52    public void setInvoker(ServerInvoker invoker)
53    {
54       this.invoker = invoker;
55    }
56
57    /**
58     * called to handle a specific invocation. Please take care to make sure
59     * implementations are thread safe and can, and often will, receive concurrent
60     * calls on this method.
61     *
62     * @param invocation
63     * @return
64     * @throws Throwable
65     */

66    public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
67    {
68       Object JavaDoc param = invocation.getParameter();
69       String JavaDoc sessionId = invocation.getSessionId();
70       String JavaDoc methodName = "";
71       Object JavaDoc[] params = null;
72       String JavaDoc[] sig = null;
73
74       if(param instanceof RemoteInvocation)
75       {
76          RemoteInvocation rminvo = (RemoteInvocation) param;
77          methodName = rminvo.getMethodName();
78          params = rminvo.getParameters();
79       }
80       else
81       {
82          throw new Exception JavaDoc("Unknown invocation payload (" + param + "). " +
83                              "Should be instance of RemoteInvocation.");
84       }
85
86       Object JavaDoc ret = null;
87       if(methodName.equals(PerformanceClientTest.NUM_OF_CALLS))
88       {
89          Integer JavaDoc totalCountInteger = (Integer JavaDoc) params[0];
90          int totalCount = totalCountInteger.intValue();
91          System.out.println("received totalCallCount call with total count of " + totalCount + " from " + sessionId);
92          CallTracker tracker = (CallTracker) callTrackers.get(sessionId);
93          if(tracker != null)
94          {
95             tracker.createTotalCount(totalCount);
96             ret = totalCountInteger;
97          }
98          else
99          {
100             log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
101             throw new Exception JavaDoc("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
102          }
103       }
104       else if(methodName.equals(PerformanceClientTest.TEST_INVOCATION))
105       {
106          if(params != null)
107          {
108             Payload payload = (Payload) params[0];
109             int clientInvokeCallCount = payload.getCallNumber();
110
111             CallTracker tracker = (CallTracker) callTrackers.get(sessionId);
112             if(tracker != null)
113             {
114                tracker.verifyClientInvokeCount(clientInvokeCallCount);
115             }
116             else
117             {
118                log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
119                throw new Exception JavaDoc("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
120             }
121             // just passing return, even though not needed
122
ret = new Integer JavaDoc(clientInvokeCallCount);
123          }
124          else
125          {
126             log.error("no parameter passed for method call " + methodName);
127          }
128
129       }
130       else
131       {
132          throw new Exception JavaDoc("Don't know what to do with call to " + methodName);
133       }
134
135       return ret;
136
137    }
138
139    private void createCallTracker(String JavaDoc sessionId, InvokerCallbackHandler callbackHandler)
140    {
141       CallTracker tracker = new CallTracker(sessionId, callbackHandler);
142       callTrackers.put(sessionId, tracker);
143    }
144
145    /**
146     * Adds a callback handler that will listen for callbacks from
147     * the server invoker handler.
148     *
149     * @param callbackHandler
150     */

151    public void addListener(InvokerCallbackHandler callbackHandler)
152    {
153       ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
154       String JavaDoc sessionId = handler.getClientSessionId();
155       System.out.println("Adding callback listener. Callback handler has session id: " + sessionId);
156       createCallTracker(sessionId, callbackHandler);
157       listeners.add(callbackHandler);
158    }
159
160    /**
161     * Removes the callback handler that was listening for callbacks
162     * from the server invoker handler.
163     *
164     * @param callbackHandler
165     */

166    public void removeListener(InvokerCallbackHandler callbackHandler)
167    {
168       //TODO: -TME Need to figure out how this should be handled.
169
// Could look up CallTracker based on session id (as in addListener() method)
170
// and then remove from tracker or kill tracker all together.
171
listeners.remove(callbackHandler);
172    }
173 }
Popular Tags