1 7 package org.jboss.remoting.callback; 8 9 import org.jboss.dtf.MultipleTestRunner; 10 import org.jboss.logging.Logger; 11 import org.jboss.remoting.AbstractInvokerTest; 12 import org.jboss.remoting.Callback; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.HandleCallbackException; 15 import org.jboss.remoting.InvocationRequest; 16 import org.jboss.remoting.InvokerCallbackHandler; 17 import org.jboss.remoting.InvokerLocator; 18 import org.jboss.remoting.ServerInvocationHandler; 19 import org.jboss.remoting.ServerInvoker; 20 import org.jboss.remoting.transport.Connector; 21 import org.jboss.samples.callback.CallbackServer; 22 import org.apache.log4j.Level; 23 24 import javax.management.MBeanServer ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 29 32 public class CallbackTestClient extends AbstractInvokerTest 33 { 34 private static String transport = "socket"; 36 private static String host = "localhost"; 37 private static int port = 5500; 38 39 private String locatorURI = null; 40 41 private Client remotingClient; 42 43 private static final Logger log = Logger.getLogger(CallbackTestClient.class); 44 45 public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 46 47 48 public CallbackTestClient(String name) 49 { 50 super(name); 51 } 52 53 public CallbackTestClient(String name, int numberOfInstances) 54 { 55 super(name, numberOfInstances); 56 } 57 58 public CallbackTestClient(String name, String transport, int port) 59 { 60 super(name, transport, port); 61 } 62 63 public CallbackTestClient(String name, String transport, int port, int numberOfInstances) 64 { 65 super(name, transport, port, numberOfInstances); 66 } 67 68 public CallbackTestClient(String name, String locatorURI) 69 { 70 super(name); 71 this.locatorURI = locatorURI; 72 } 73 74 private void init() throws Exception 75 { 76 createRemotingClient(); 79 } 81 82 public void createRemotingClient() throws Exception 83 { 84 InvokerLocator locator = new InvokerLocator(locatorURI); 85 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 86 87 remotingClient = new Client(locator, null); 90 91 } 92 93 public void makeInvocation() throws Throwable 94 { 95 Object response = remotingClient.invoke("Do something", null); 96 System.out.println("Invocation response: " + response); 97 } 98 99 public void testPullCallback() throws Throwable 100 { 101 init(); 102 103 CallbackHandler callbackHandler = new CallbackHandler(); 104 remotingClient.addListener(callbackHandler); 106 makeInvocation(); 108 109 List callbacks = remotingClient.getCallbacks(); 110 Iterator itr = callbacks.iterator(); 111 while(itr.hasNext()) 112 { 113 Object obj = itr.next(); 114 log.info("testPullCallback - Callback object should have been " + 115 CallbackTestServer.CALLBACK_VALUE + " and was " + 116 (obj == null ? null : ((Callback)obj).getCallbackObject())); 117 if(obj instanceof Callback) 118 { 119 assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject()); 120 } 121 else 122 { 123 assertTrue("Callback object is NOT of type Callback.", false); 124 } 125 } 126 127 remotingClient.removeListener(callbackHandler); 129 remotingClient.disconnect(); 130 } 131 132 public void testPushCallback() throws Throwable 133 { 134 init(); 135 136 String callbackLocatorURI = transport + "://" + host + ":" + (port + 5); 138 InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI); 139 140 log.info("testPushCallback - Setting up server."); 141 setupServer(callbackLocator); 144 145 CallbackHandler callbackHandler = new CallbackHandler(); 146 String callbackHandleObject = "myCallbackHandleObject"; 148 log.info("testPushCallback - adding listener."); 149 remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject); 151 log.info("testPushCallback - make invocation"); 152 makeInvocation(); 154 155 Thread.sleep(5000); 157 158 Callback callback = callbackHandler.getCallback(); 159 log.info("testPushCallback - Callback returned was " + callback); 160 assertNotNull("Callback returned was null.", callback); 161 162 Object callbackObj = callback.getCallbackObject(); 163 log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj); 164 assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj, 165 CallbackTestServer.CALLBACK_VALUE, callbackObj); 166 Object callbackHandleObj = callback.getCallbackHandleObject(); 167 log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj); 168 assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj, 169 callbackHandleObject, callbackHandleObj ); 170 InvokerLocator serverLoc = callback.getServerLocator(); 171 log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() + 172 ", and was " + serverLoc); 173 assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() + 174 ", but was " + serverLoc, 175 remotingClient.getInvoker().getLocator(), serverLoc); 176 177 remotingClient.removeListener(callbackHandler); 179 180 } 181 182 public void setupServer(InvokerLocator locator) throws Exception 183 { 184 log.info("Starting remoting server with locator uri of: " + locator); 185 try 186 { 187 Connector connector = new Connector(); 188 connector.setInvokerLocator(locator.getLocatorURI()); 189 connector.start(); 190 191 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 192 connector.addInvocationHandler("sample", invocationHandler); 194 } 195 catch(Exception e) 196 { 197 log.error("Error starting callback server", e); 198 throw e; 199 } 200 } 201 202 203 209 public static void main(String [] args) 210 { 211 if(args != null && args.length == 2) 212 { 213 transport = args[0]; 214 port = Integer.parseInt(args[1]); 215 } 216 String locatorURI = transport + "://" + host + ":" + port; 217 CallbackTestClient client = new CallbackTestClient(CallbackTestClient.class.getName(), locatorURI); 218 219 try 221 { 222 MultipleTestRunner runner = new MultipleTestRunner(); 223 runner.doRun(client, true); 224 } 225 catch(Throwable e) 226 { 227 e.printStackTrace(); 228 System.exit(1); 229 } 230 System.exit(0); 231 } 232 233 236 public static class SampleInvocationHandler implements ServerInvocationHandler 237 { 238 239 private List listeners = new ArrayList (); 240 241 242 249 public Object invoke(InvocationRequest invocation) throws Throwable 250 { 251 253 Callback callback = new Callback("This is the payload of callback invocation."); 256 Iterator itr = listeners.iterator(); 257 while(itr.hasNext()) 258 { 259 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 260 try 261 { 262 callbackHandler.handleCallback(callback); 263 } 264 catch(HandleCallbackException e) 265 { 266 e.printStackTrace(); 267 } 268 } 269 270 return RESPONSE_VALUE; 271 272 } 273 274 280 public void addListener(InvokerCallbackHandler callbackHandler) 281 { 282 listeners.add(callbackHandler); 283 } 284 285 291 public void removeListener(InvokerCallbackHandler callbackHandler) 292 { 293 listeners.remove(callbackHandler); 294 } 295 296 301 public void setMBeanServer(MBeanServer server) 302 { 303 } 305 306 311 public void setInvoker(ServerInvoker invoker) 312 { 313 } 315 316 } 317 318 319 public class CallbackHandler implements InvokerCallbackHandler 320 { 321 private Callback callback; 322 323 331 public void handleCallback(InvocationRequest invocation) throws HandleCallbackException 332 { 333 if(invocation instanceof Callback) 334 { 335 callback = (Callback) invocation; 336 System.out.println("Received callback value of: " + callback.getCallbackObject()); 337 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 338 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 339 } 340 else 341 { 342 System.out.println("Received callback value of: " + invocation.getParameter()); 343 } 344 } 345 346 public Callback getCallback() 347 { 348 return callback; 349 } 350 351 } 352 353 } | Popular Tags |