1 7 package org.jboss.test.remoting.callback.listeners; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import javax.management.MBeanServer ; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.InvocationRequest; 15 import org.jboss.remoting.InvokerLocator; 16 import org.jboss.remoting.ServerInvocationHandler; 17 import org.jboss.remoting.ServerInvoker; 18 import org.jboss.remoting.callback.Callback; 19 import org.jboss.remoting.callback.HandleCallbackException; 20 import org.jboss.remoting.callback.InvokerCallbackHandler; 21 import org.jboss.remoting.transport.Connector; 22 23 import junit.framework.TestCase; 24 25 40 public class ListenerRemovedTestCase extends TestCase 41 { 42 private static List callbacks = new ArrayList (); 43 44 public void testCallbacks() throws Throwable 45 { 46 47 InvokerLocator callbackServerLocator = new InvokerLocator("socket://localhost:2222"); 49 Connector callbackConnector = new Connector(); 50 callbackConnector.setInvokerLocator(callbackServerLocator.getLocatorURI()); 51 callbackConnector.start(); 52 53 InvokerLocator targetServerLocator = new InvokerLocator("socket://localhost:3456"); 55 Connector connector = new Connector(); 56 connector.setInvokerLocator(targetServerLocator.getLocatorURI()); 57 connector.create(); 58 connector.addInvocationHandler("MySubsystem", new ServerInvocationHandlerImpl()); 59 connector.start(); 60 61 62 Client client = new Client(targetServerLocator); 63 client.connect(); 64 65 InvokerCallbackHandler callbackHandler1 = new InvokerCallbackHandlerImpl("ONE"); 66 67 client.addListener(callbackHandler1, callbackServerLocator); 68 69 client.removeListener(callbackHandler1); 70 71 try 72 { 73 client.removeListener(callbackHandler1); 74 assertTrue("Should throw an exception if remove a callback handler that has already been removed.", false); 75 } 76 catch(Throwable t) 77 { 78 assertTrue("Got exception from server as expected.", true); 79 } 80 finally 81 { 82 connector.stop(); 83 callbackConnector.stop(); 84 85 connector.destroy(); 86 callbackConnector.destroy(); 87 } 88 } 89 90 91 private static class InvokerCallbackHandlerImpl implements InvokerCallbackHandler 92 { 93 private String id; 94 95 public InvokerCallbackHandlerImpl(String id) 96 { 97 this.id = id; 98 } 99 100 public void handleCallback(Callback callback) throws HandleCallbackException 101 { 102 System.out.println("Handler " + id + " got callback: " + callback.getParameter()); 103 callbacks.add(callback.getParameter()); 104 } 106 } 107 108 private static class ServerInvocationHandlerImpl implements ServerInvocationHandler 109 { 110 111 protected ServerInvoker serverInvoker; 112 protected ArrayList callbackHandlers = new ArrayList (); 113 114 public void setMBeanServer(MBeanServer server) 115 { 116 } 117 118 public void setInvoker(ServerInvoker invoker) 119 { 120 serverInvoker = invoker; 121 } 122 123 public Object invoke(InvocationRequest invocation) throws Throwable 124 { 125 String param = (String ) invocation.getParameter(); 126 System.out.println(param); 127 128 if(param.startsWith("call back")) 129 { 130 String arg = param.substring("call back".length()).trim(); 131 for(Iterator i = callbackHandlers.iterator(); i.hasNext();) 132 { 133 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) i.next(); 134 callbackHandler.handleCallback(new Callback(arg)); 135 } 136 } 137 138 return null; 139 } 140 141 public void addListener(InvokerCallbackHandler callbackHandler) 142 { 143 System.out.println("adding listener " + callbackHandler); 144 callbackHandlers.add(callbackHandler); 145 } 146 147 public void removeListener(InvokerCallbackHandler callbackHandler) 148 { 149 callbackHandlers.remove(callbackHandler); 150 } 151 } 152 153 private static class CustomListenerException extends RuntimeException 154 { 155 public CustomListenerException(String e) 156 { 157 super(e); 158 } 159 } 160 161 } 162 | Popular Tags |