1 7 package org.jboss.test.remoting.stream; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 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.ServerInvoker; 17 import org.jboss.remoting.callback.InvokerCallbackHandler; 18 import org.jboss.remoting.stream.StreamInvocationHandler; 19 import org.jboss.remoting.transport.Connector; 20 21 24 public class StreamingTestServer extends ServerTestCase 25 { 26 private static String transport = "socket"; 28 private static String host = "localhost"; 29 private static int port = 5400; 30 31 private String locatorURI = transport + "://" + host + ":" + port; 32 private Connector connector = null; 33 34 public void setupServer() throws Exception 35 { 36 InvokerLocator locator = new InvokerLocator(locatorURI); 37 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 38 connector = new Connector(); 39 connector.setInvokerLocator(locator.getLocatorURI()); 40 connector.create(); 41 42 TestStreamInvocationHandler invocationHandler = new TestStreamInvocationHandler(); 43 connector.addInvocationHandler("test_stream", invocationHandler); 45 46 connector.start(); 47 } 48 49 protected void setUp() throws Exception 50 { 51 setupServer(); 52 } 53 54 protected void tearDown() throws Exception 55 { 56 if(connector != null) 57 { 58 connector.stop(); 59 connector.destroy(); 60 } 61 } 62 63 69 public static void main(String [] args) 70 { 71 if(args != null && args.length == 3) 72 { 73 transport = args[0]; 74 host = args[1]; 75 port = Integer.parseInt(args[2]); 76 } 77 78 StreamingTestServer server = new StreamingTestServer(); 79 try 80 { 81 server.setUp(); 82 83 Thread.sleep(10000); 85 86 server.tearDown(); 87 } 88 catch(Exception e) 89 { 90 e.printStackTrace(); 91 } 92 } 93 94 97 public static class TestStreamInvocationHandler implements StreamInvocationHandler 98 { 99 private InputStream stream = null; 100 101 private int streamSize = 0; 102 103 110 public Object invoke(InvocationRequest invocation) throws Throwable 111 { 112 System.out.println("Invocation request is: " + invocation.getParameter()); 114 115 return new Integer (streamSize); 117 } 118 119 125 public void addListener(InvokerCallbackHandler callbackHandler) 126 { 127 } 129 130 136 public void removeListener(InvokerCallbackHandler callbackHandler) 137 { 138 } 140 141 146 public void setMBeanServer(MBeanServer server) 147 { 148 } 150 151 156 public void setInvoker(ServerInvoker invoker) 157 { 158 } 160 161 public Object handleStream(InputStream stream, Object param) 162 { 163 this.stream = stream; 164 165 try 166 { 167 ByteArrayOutputStream out=new ByteArrayOutputStream (); 168 byte buf[]=new byte[4096]; 169 while(true) 170 { 171 int c = this.stream.read(buf); 172 if (c<0) 173 { 174 break; 175 } 176 out.write(buf,0,c); 177 } 178 byte[] bytes = out.toByteArray(); 179 streamSize = bytes.length; 180 System.out.println("Read stream. Contents is: " + new String (bytes)); 181 } 182 catch(IOException e) 183 { 184 e.printStackTrace(); 185 } 186 finally 187 { 188 try 189 { 190 stream.close(); 191 } 192 catch(IOException e) 193 { 194 e.printStackTrace(); 195 } 196 } 197 return param; 198 } 199 } 200 201 } | Popular Tags |