1 7 package org.jboss.test.remoting.callback; 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.jrunit.extensions.ServerTestCase; 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.InvokerCallbackHandler; 20 import org.jboss.remoting.transport.Connector; 21 22 28 public class CallbackTestServer extends ServerTestCase 29 { 30 private static String transport = "socket"; 32 private static String host = "localhost"; 33 private static int port = 5500; 34 35 private String locatorURI = transport + "://" + host + ":" + port; 36 private Connector connector; 37 38 public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 40 public static final String CALLBACK_VALUE = "This is the payload of callback invocation."; 41 42 43 public void setupServer() throws Exception 44 { 45 InvokerLocator locator = new InvokerLocator(locatorURI); 46 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 47 connector = new Connector(); 48 connector.setInvokerLocator(locator.getLocatorURI()); 49 connector.start(); 50 51 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 52 connector.addInvocationHandler("sample", invocationHandler); 54 } 55 56 protected void setUp() throws Exception 57 { 58 setupServer(); 59 } 60 61 protected void tearDown() throws Exception 62 { 63 if(connector != null) 64 { 65 connector.stop(); 66 connector.destroy(); 67 } 68 } 69 70 73 public static class SampleInvocationHandler implements ServerInvocationHandler 74 { 75 76 private List listeners = new ArrayList (); 77 78 79 86 public Object invoke(InvocationRequest invocation) throws Throwable 87 { 88 90 Callback callback = new Callback(CALLBACK_VALUE); 93 Iterator itr = listeners.iterator(); 94 while(itr.hasNext()) 95 { 96 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 97 callbackHandler.handleCallback(callback); 98 } 99 100 return RESPONSE_VALUE; 101 102 } 103 104 110 public void addListener(InvokerCallbackHandler callbackHandler) 111 { 112 listeners.add(callbackHandler); 113 } 114 115 121 public void removeListener(InvokerCallbackHandler callbackHandler) 122 { 123 listeners.remove(callbackHandler); 124 } 125 126 131 public void setMBeanServer(MBeanServer server) 132 { 133 } 135 136 141 public void setInvoker(ServerInvoker invoker) 142 { 143 } 145 146 } 147 } | Popular Tags |