1 7 package org.jboss.remoting.samples.stream; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.File ; 11 import java.io.FileWriter ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import javax.management.MBeanServer ; 15 import org.jboss.remoting.InvocationRequest; 16 import org.jboss.remoting.InvokerLocator; 17 import org.jboss.remoting.ServerInvoker; 18 import org.jboss.remoting.callback.InvokerCallbackHandler; 19 import org.jboss.remoting.stream.StreamInvocationHandler; 20 import org.jboss.remoting.transport.Connector; 21 22 25 public class StreamingServer 26 { 27 private static String transport = "socket"; 29 private static String host = "localhost"; 30 private static int port = 5400; 31 32 private String locatorURI = transport + "://" + host + ":" + port; 33 private Connector connector = null; 34 35 public void setupServer() throws Exception 36 { 37 InvokerLocator locator = new InvokerLocator(locatorURI); 38 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 39 connector = new Connector(); 40 connector.setInvokerLocator(locator.getLocatorURI()); 41 connector.create(); 42 43 TestStreamInvocationHandler invocationHandler = new TestStreamInvocationHandler(); 44 connector.addInvocationHandler("test_stream", invocationHandler); 46 47 connector.start(true); 48 } 49 50 51 57 public static void main(String [] args) 58 { 59 if(args != null && args.length == 3) 60 { 61 transport = args[0]; 62 host = args[1]; 63 port = Integer.parseInt(args[2]); 64 } 65 66 StreamingServer server = new StreamingServer(); 67 try 68 { 69 server.setupServer(); 70 71 while(true) 73 { 74 Thread.sleep(1000); 75 } 76 77 } 78 catch(Exception e) 79 { 80 e.printStackTrace(); 81 } 82 } 83 84 87 public static class TestStreamInvocationHandler implements StreamInvocationHandler 88 { 89 private int streamSize = 0; 90 91 99 public Object handleStream(InputStream stream, Object param) 100 { 101 try 102 { 103 System.out.println("Received input stream from client to write out to file " + param); 104 105 ByteArrayOutputStream out = new ByteArrayOutputStream (); 106 byte buf[] = new byte[4096]; 107 while(true) 108 { 109 int c = stream.read(buf); 110 if(c < 0) 111 { 112 break; 113 } 114 out.write(buf, 0, c); 115 } 116 byte[] bytes = out.toByteArray(); 117 streamSize = bytes.length; 118 System.out.println("Read stream of size " + streamSize + ". Now writing to " + param); 119 File newFile = new File ((String ) param); 120 if(!newFile.exists()) 121 { 122 newFile.createNewFile(); 123 } 124 125 FileWriter fileWriter = new FileWriter (newFile, false); 126 fileWriter.write(new String (bytes)); 127 fileWriter.flush(); 128 System.out.println("New file " + param + " has been written out to " + newFile.getAbsolutePath()); 129 fileWriter.close(); 130 } 131 catch(IOException e) 132 { 133 e.printStackTrace(); 134 } 135 finally 136 { 137 try 138 { 139 stream.close(); 140 } 141 catch(IOException e) 142 { 143 e.printStackTrace(); 144 } 145 } 146 return new Integer (streamSize); 147 } 148 149 156 public Object invoke(InvocationRequest invocation) throws Throwable 157 { 158 System.out.println("Invocation request is: " + invocation.getParameter()); 160 161 return new Integer (streamSize); 163 } 164 165 171 public void addListener(InvokerCallbackHandler callbackHandler) 172 { 173 } 175 176 182 public void removeListener(InvokerCallbackHandler callbackHandler) 183 { 184 } 186 187 192 public void setMBeanServer(MBeanServer server) 193 { 194 } 196 197 202 public void setInvoker(ServerInvoker invoker) 203 { 204 } 206 } 207 } | Popular Tags |