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.callback.listeners;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
import javax.management.MBeanServer;
29
import org.jboss.remoting.Client;
30
import org.jboss.remoting.InvocationRequest;
31
import org.jboss.remoting.InvokerLocator;
32
import org.jboss.remoting.ServerInvocationHandler;
33
import org.jboss.remoting.ServerInvoker;
34
import org.jboss.remoting.callback.Callback;
35
import org.jboss.remoting.callback.HandleCallbackException;
36
import org.jboss.remoting.callback.InvokerCallbackHandler;
37
import org.jboss.remoting.transport.Connector;
39
import junit.framework.TestCase;
42
* This is a test to make sure multiple callback listeners can be added
43
* from the same client and the each gets their callbacks, as well
44
* as making sure they are removed correctly.
46
* This test really only works the way it is coded because know that
47
* everything will be run locally, meaning that I can check the
48
* callback values after making in invoke call because the same
49
* thread will make the callbacks before the thread returns from
52
* @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
53
* @author <a href="mailto:tom@jboss.org">Ton Elrod</a>
54
* @version <tt>$Revision: 566 $</tt>
56
public class ListenerRemovedTestCase extends TestCase
58
private static List callbacks = new ArrayList();
60
public void testCallbacks() throws Throwable
63
// Start the callback server
64
InvokerLocator callbackServerLocator = new InvokerLocator("socket://localhost:2222");
65
Connector callbackConnector = new Connector();
66
callbackConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
67
callbackConnector.start();
69
// Start the target server
70
InvokerLocator targetServerLocator = new InvokerLocator("socket://localhost:3456");
71
Connector connector = new Connector();
72
connector.setInvokerLocator(targetServerLocator.getLocatorURI());
74
connector.addInvocationHandler("MySubsystem", new ServerInvocationHandlerImpl());
78
Client client = new Client(targetServerLocator);
81
InvokerCallbackHandler callbackHandler1 = new InvokerCallbackHandlerImpl("ONE");
83
client.addListener(callbackHandler1, callbackServerLocator);
85
client.removeListener(callbackHandler1);
89
client.removeListener(callbackHandler1);
90
assertTrue("Should throw an exception if remove a callback handler that has already been removed.", false);
94
assertTrue("Got exception from server as expected.", true);
99
callbackConnector.stop();
102
callbackConnector.destroy();
107
private static class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
111
public InvokerCallbackHandlerImpl(String id)
116
public void handleCallback(Callback callback) throws HandleCallbackException
118
System.out.println("Handler " + id + " got callback: " + callback.getParameter());
119
callbacks.add(callback.getParameter());
120
//throw new CustomListenerException("This is the custom listener exception that I want to know about.");
124
private static class ServerInvocationHandlerImpl implements ServerInvocationHandler
127
protected ServerInvoker serverInvoker;
128
protected ArrayList callbackHandlers = new ArrayList();
130
public void setMBeanServer(MBeanServer server)
134
public void setInvoker(ServerInvoker invoker)
136
serverInvoker = invoker;
139
public Object invoke(InvocationRequest invocation) throws Throwable
141
String param = (String) invocation.getParameter();
142
System.out.println(param);
144
if(param.startsWith("call back"))
146
String arg = param.substring("call back".length()).trim();
147
for(Iterator i = callbackHandlers.iterator(); i.hasNext();)
149
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) i.next();
150
callbackHandler.handleCallback(new Callback(arg));
157
public void addListener(InvokerCallbackHandler callbackHandler)
159
System.out.println("adding listener " + callbackHandler);
160
callbackHandlers.add(callbackHandler);
163
public void removeListener(InvokerCallbackHandler callbackHandler)
165
callbackHandlers.remove(callbackHandler);
169
private static class CustomListenerException extends RuntimeException
171
public CustomListenerException(String e)