2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.transport.mock;
25
import java.rmi.MarshalledObject;
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
32
import javax.management.MBeanServer;
33
import org.jboss.logging.Logger;
34
import org.jboss.remoting.Client;
35
import org.jboss.remoting.InvocationRequest;
36
import org.jboss.remoting.InvokerLocator;
37
import org.jboss.remoting.ServerInvocationHandler;
38
import org.jboss.remoting.ServerInvoker;
39
import org.jboss.remoting.callback.Callback;
40
import org.jboss.remoting.callback.InvokerCallbackHandler;
41
import org.jboss.remoting.invocation.InternalInvocation;
42
import org.jboss.remoting.invocation.NameBasedInvocation;
43
import org.jboss.test.remoting.ComplexReturn;
44
import org.jboss.test.remoting.byvalue.ByValuePayload;
47
* MockServerInvocationHandler
49
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
50
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
51
* @version $Revision: 1066 $
53
public class MockServerInvocationHandler implements ServerInvocationHandler
55
private ServerInvoker invoker;
56
private List listeners = Collections.synchronizedList(new ArrayList());
57
private Map clientListeners = new HashMap();
59
private static final Logger log = Logger.getLogger(MockServerInvocationHandler.class);
63
* set the invoker that owns this handler
67
public void setInvoker(ServerInvoker invoker)
69
this.invoker = invoker;
73
* set the mbean server that the handler can reference
77
public void setMBeanServer(MBeanServer server)
81
public Object invoke(InvocationRequest invocation)
84
Object param = invocation.getParameter();
85
String methodName = "";
86
Object[] params = null;
89
if(param instanceof NameBasedInvocation)
91
NameBasedInvocation nbi = (NameBasedInvocation) param;
92
methodName = nbi.getMethodName();
93
params = nbi.getParameters();
94
sig = nbi.getSignature();
96
else if(param instanceof InternalInvocation)
98
InternalInvocation ii = (InternalInvocation) param;
99
methodName = ii.getMethodName();
100
params = ii.getParameters();
104
log.info("Don't recognize the parameter type, so just returning it.");
108
String sessionId = invocation.getSessionId();
109
String subsystem = invocation.getSubsystem();
111
log.debug("invoke() called with method: " + methodName +
112
"\tsessionId: " + sessionId + "\tsubsystem:" + subsystem);
113
//deprecated since specific to JMX (old way of handling callbacks)
114
if(methodName.equals("testComplexReturn"))
116
//Need to send back complex object containing array of complext objects.
117
ComplexReturn ret = new ComplexReturn();
120
if(methodName.equals("testMarshalledObject"))
122
ComplexReturn ret = new ComplexReturn();
123
MarshalledObject mObj = new MarshalledObject(ret);
126
else if(methodName.equals("test"))
128
// will cause a callback on all the listeners
129
log.debug("test called on server invocation handler, so should do callback.");
130
CallbackDispatcher callbackDispatcher = new CallbackDispatcher(invocation.getSessionId(),
131
invocation.getSubsystem(),
132
new NameBasedInvocation("handleCallback",
135
Thread callbackThread = new Thread(callbackDispatcher);
136
callbackThread.start();
138
else if(methodName.equals("addClientListener"))
140
Object obj = params[0];
141
InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
143
clientListeners.put(invocation.getSessionId(), clientHandler);
145
else if(methodName.equals("removeClientListener"))
147
Object obj = params[0];
148
InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
150
clientListeners.remove(invocation.getSessionId());
152
else if(methodName.equals("handleCallback"))
154
// got a callback from remote server
155
InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) clientListeners.get(sessionId);
156
clientHandler.handleCallback(new Callback(invocation.getParameter()));
158
else if(methodName.equals("testException") || methodName.equals("testThrowException"))
160
throw new Exception("Got call from client to throw exception. This is expected.");
162
else if(methodName.equals("testByValue"))
164
// check to see if by value payload was serialized at some point
165
Object arg = params[0];
166
if(arg instanceof ByValuePayload)
168
ByValuePayload byValuePayload = (ByValuePayload) arg;
169
return new Boolean(byValuePayload.wasMarshalled());
174
return Boolean.FALSE;
181
log.info("Found a parameter to return " + ret);
185
log.info("returning null");
195
private void handleRemoveNotificationListener(String sessionId)
197
listeners.remove(sessionId);
201
* @param clientLocator
207
private void handleAddNotificationListener(InvokerLocator clientLocator,
209
String sessionId) throws Exception
211
Client callBackClient = new Client(clientLocator, subsystem);
212
callBackClient.connect();
213
listeners.add(callBackClient);
216
public void addListener(InvokerCallbackHandler callbackHandler)
218
listeners.add(callbackHandler);
219
log.debug("added listener " + callbackHandler);
222
public void removeListener(InvokerCallbackHandler callbackHandler)
224
listeners.remove(callbackHandler);
225
log.debug("removed listener " + callbackHandler);
228
private class CallbackDispatcher implements Runnable
230
private String sessionId;
231
private String subsystem;
232
private Object param;
234
public CallbackDispatcher(String sessionId, String subsystem, Object param)
236
this.sessionId = sessionId;
237
this.subsystem = subsystem;
243
List tempList = null;
244
synchronized(listeners)
246
tempList = new ArrayList(listeners);
249
Iterator itr = tempList.iterator();
254
InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
255
Callback invocation = new Callback(param);
256
handler.handleCallback(invocation);