KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > mock > MockServerInvocationHandler


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.transport.mock;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import org.jboss.logging.Logger;
16 import org.jboss.remoting.Client;
17 import org.jboss.remoting.InvocationRequest;
18 import org.jboss.remoting.InvokerLocator;
19 import org.jboss.remoting.ServerInvocationHandler;
20 import org.jboss.remoting.ServerInvoker;
21 import org.jboss.remoting.callback.Callback;
22 import org.jboss.remoting.callback.InvokerCallbackHandler;
23 import org.jboss.remoting.invocation.InternalInvocation;
24 import org.jboss.remoting.invocation.NameBasedInvocation;
25 import org.jboss.test.remoting.ComplexReturn;
26 import org.jboss.test.remoting.byvalue.ByValuePayload;
27
28 /**
29  * MockServerInvocationHandler
30  *
31  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  * @version $Revision: 1.3 $
34  */

35 public class MockServerInvocationHandler implements ServerInvocationHandler
36 {
37    private ServerInvoker invoker;
38    private List JavaDoc listeners = new ArrayList JavaDoc();
39    private Map JavaDoc clientListeners = new HashMap JavaDoc();
40
41    private static final Logger log = Logger.getLogger(MockServerInvocationHandler.class);
42
43
44    /**
45     * set the invoker that owns this handler
46     *
47     * @param invoker
48     */

49    public void setInvoker(ServerInvoker invoker)
50    {
51       this.invoker = invoker;
52    }
53
54    /**
55     * set the mbean server that the handler can reference
56     *
57     * @param server
58     */

59    public void setMBeanServer(MBeanServer JavaDoc server)
60    {
61    }
62
63    public Object JavaDoc invoke(InvocationRequest invocation)
64          throws Throwable JavaDoc
65    {
66       Object JavaDoc param = invocation.getParameter();
67       String JavaDoc methodName = "";
68       Object JavaDoc[] params = null;
69       String JavaDoc[] sig = null;
70
71       if(param instanceof NameBasedInvocation)
72       {
73          NameBasedInvocation nbi = (NameBasedInvocation) param;
74          methodName = nbi.getMethodName();
75          params = nbi.getParameters();
76          sig = nbi.getSignature();
77       }
78       else if(param instanceof InternalInvocation)
79       {
80          InternalInvocation ii = (InternalInvocation) param;
81          methodName = ii.getMethodName();
82          params = ii.getParameters();
83       }
84       else
85       {
86          log.info("Don't recognize the parameter type, so just returning it.");
87          return param;
88       }
89
90       String JavaDoc sessionId = invocation.getSessionId();
91       String JavaDoc subsystem = invocation.getSubsystem();
92
93       log.debug("invoke() called with method: " + methodName +
94                 "\tsessionId: " + sessionId + "\tsubsystem:" + subsystem);
95       //deprecated since specific to JMX (old way of handling callbacks)
96
if(methodName.equals("testComplexReturn"))
97       {
98          //Need to send back complex object containing array of complext objects.
99
ComplexReturn ret = new ComplexReturn();
100          return ret;
101       }
102       else if(methodName.equals("test"))
103       {
104          // will cause a callback on all the listeners
105
log.debug("test called on server invocation handler, so should do callback.");
106          CallbackDispatcher callbackDispatcher = new CallbackDispatcher(invocation.getSessionId(),
107                                                                         invocation.getSubsystem(),
108                                                                         new NameBasedInvocation("handleCallback",
109                                                                                                 params,
110                                                                                                 sig));
111          Thread JavaDoc callbackThread = new Thread JavaDoc(callbackDispatcher);
112          callbackThread.start();
113       }
114       else if(methodName.equals("addClientListener"))
115       {
116          Object JavaDoc obj = params[0];
117          InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
118
119          clientListeners.put(invocation.getSessionId(), clientHandler);
120       }
121       else if(methodName.equals("removeClientListener"))
122       {
123          Object JavaDoc obj = params[0];
124          InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
125
126          clientListeners.remove(invocation.getSessionId());
127       }
128       else if(methodName.equals("handleCallback"))
129       {
130          // got a callback from remote server
131
InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) clientListeners.get(sessionId);
132          clientHandler.handleCallback(new Callback(invocation.getParameter()));
133       }
134       else if(methodName.equals("testByValue"))
135       {
136          // check to see if by value payload was serialized at some point
137
Object JavaDoc arg = params[0];
138          if(arg instanceof ByValuePayload)
139          {
140             ByValuePayload byValuePayload = (ByValuePayload) arg;
141             return new Boolean JavaDoc(byValuePayload.wasMarshalled());
142          }
143          else
144          {
145             // Error in tests
146
return Boolean.FALSE;
147          }
148       }
149       Object JavaDoc ret = null;
150       if(params != null)
151       {
152          ret = params[0];
153          log.info("Found a parameter to return " + ret);
154       } // end of if ()
155
else
156       {
157          log.info("returning null");
158       } // end of else
159

160       return ret;
161    }
162
163    /**
164     * @param sessionId
165     * @deprecated
166     */

167    private void handleRemoveNotificationListener(String JavaDoc sessionId)
168    {
169       listeners.remove(sessionId);
170    }
171
172    /**
173     * @param clientLocator
174     * @param subsystem
175     * @param sessionId
176     * @throws Exception
177     * @deprecated
178     */

179    private void handleAddNotificationListener(InvokerLocator clientLocator,
180                                               String JavaDoc subsystem,
181                                               String JavaDoc sessionId) throws Exception JavaDoc
182    {
183       Client callBackClient = new Client(clientLocator, subsystem);
184       callBackClient.connect();
185       listeners.add(callBackClient);
186    }
187
188    public void addListener(InvokerCallbackHandler callbackHandler)
189    {
190       listeners.add(callbackHandler);
191       log.debug("added listener " + callbackHandler);
192    }
193
194    public void removeListener(InvokerCallbackHandler callbackHandler)
195    {
196       listeners.remove(callbackHandler);
197       log.debug("removed listener " + callbackHandler);
198    }
199
200    private class CallbackDispatcher implements Runnable JavaDoc
201    {
202       private String JavaDoc sessionId;
203       private String JavaDoc subsystem;
204       private Object JavaDoc param;
205
206       public CallbackDispatcher(String JavaDoc sessionId, String JavaDoc subsystem, Object JavaDoc param)
207       {
208          this.sessionId = sessionId;
209          this.subsystem = subsystem;
210          this.param = param;
211       }
212
213       public void run()
214       {
215          Iterator JavaDoc itr = listeners.iterator();
216          while(itr.hasNext())
217          {
218             try
219             {
220                InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
221                Callback invocation = new Callback(param);
222                handler.handleCallback(invocation);
223             }
224             catch(Throwable JavaDoc e)
225             {
226                e.printStackTrace();
227             }
228
229          }
230       }
231    }
232 }
233
Popular Tags