1 7 package org.jboss.test.remoting.callback.push; 8 9 import java.util.Random ; 10 import javax.management.MBeanServer ; 11 import org.jboss.remoting.Client; 12 import org.jboss.remoting.InvocationRequest; 13 import org.jboss.remoting.InvokerLocator; 14 import org.jboss.remoting.ServerInvocationHandler; 15 import org.jboss.remoting.ServerInvoker; 16 import org.jboss.remoting.callback.Callback; 17 import org.jboss.remoting.callback.HandleCallbackException; 18 import org.jboss.remoting.callback.InvokerCallbackHandler; 19 import org.jboss.remoting.transport.Connector; 20 21 import junit.framework.TestCase; 22 23 30 public class InVMPushCallbackTestCase extends TestCase 31 { 32 34 36 38 protected InvokerLocator targetServerLocator; 39 protected InvokerLocator callbackServerLocator; 40 41 protected Connector targetServerConnector; 42 protected Connector callbackServerConnector; 43 44 protected ServerInvocationHandlerImpl targetServerInvocationHandler; 45 46 protected Client client; 47 protected InvokerCallbackHandlerImpl callbackHandler; 48 49 51 public InVMPushCallbackTestCase(String name) 52 { 53 super(name); 54 } 55 56 58 public void setUp() throws Exception 59 { 60 super.setUp(); 61 62 targetServerLocator = new InvokerLocator("socket://localhost:2323"); 63 callbackServerLocator = new InvokerLocator("socket://localhost:3434"); 64 65 targetServerConnector = new Connector(); 66 targetServerConnector.setInvokerLocator(targetServerLocator.getLocatorURI()); 67 targetServerConnector.start(); 68 targetServerInvocationHandler = new ServerInvocationHandlerImpl(); 69 targetServerConnector.addInvocationHandler("TARGET", targetServerInvocationHandler); 70 71 callbackServerConnector = new Connector(); 72 callbackServerConnector.setInvokerLocator(callbackServerLocator.getLocatorURI()); 73 callbackServerConnector.start(); 74 callbackServerConnector.addInvocationHandler("IRRELEVANT", new ServerInvocationHandlerImpl()); 75 76 client = new Client(targetServerLocator); 77 callbackHandler = new InvokerCallbackHandlerImpl(); 78 try 79 { 80 client.addListener(callbackHandler, callbackServerLocator); 81 } 82 catch(Throwable t) 83 { 84 throw new Exception (t); 85 } 86 client.connect(); 87 } 88 89 public void tearDown() throws Exception 90 { 91 callbackServerConnector.stop(); 92 targetServerConnector.stop(); 93 client.disconnect(); 94 95 super.tearDown(); 96 } 97 98 100 101 public void testPushCallback() throws Exception 102 { 103 Long arg = new Long (new Random ().nextLong()); 105 targetServerInvocationHandler.sendCallback(arg); 106 107 assertEquals(arg, callbackHandler.getReceivedArgument()); 108 } 109 110 112 114 116 118 private class ServerInvocationHandlerImpl implements ServerInvocationHandler 119 { 120 121 private MBeanServer server; 122 private ServerInvoker invoker; 123 private InvokerCallbackHandler theOnlyHandler; 124 125 127 public void setMBeanServer(MBeanServer server) 128 { 129 this.server = server; 130 } 131 132 public void setInvoker(ServerInvoker invoker) 133 { 134 this.invoker = invoker; 135 } 136 137 public Object invoke(InvocationRequest invocation) throws Throwable 138 { 139 return null; 140 } 141 142 public void addListener(InvokerCallbackHandler callbackHandler) 143 { 144 theOnlyHandler = callbackHandler; 145 } 146 147 public void removeListener(InvokerCallbackHandler callbackHandler) 148 { 149 } 151 152 154 public void sendCallback(Object arg) throws Exception 155 { 156 Callback callback = new Callback(arg); 157 theOnlyHandler.handleCallback(callback); 158 } 159 160 } 161 162 private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler 163 { 164 165 Object receivedArg; 166 167 169 public synchronized void handleCallback(Callback callback) 170 throws HandleCallbackException 171 { 172 receivedArg = callback.getParameter(); 173 } 174 175 177 public synchronized Object getReceivedArgument() 178 { 179 return receivedArg; 180 } 181 } 182 183 } 184 | Popular Tags |