KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > stream > StreamingServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.samples.stream;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileWriter JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
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 /**
23  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
24  */

25 public class StreamingServer
26 {
27    // Default locator values
28
private static String JavaDoc transport = "socket";
29    private static String JavaDoc host = "localhost";
30    private static int port = 5400;
31
32    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
33    private Connector connector = null;
34
35    public void setupServer() throws Exception JavaDoc
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       // first parameter is sub-system name. can be any String value.
45
connector.addInvocationHandler("test_stream", invocationHandler);
46
47       connector.start(true);
48    }
49
50
51    /**
52     * Can pass transport and port to be used as parameters.
53     * Valid transports are 'rmi' and 'socket'.
54     *
55     * @param args
56     */

57    public static void main(String JavaDoc[] 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          // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
72
while(true)
73          {
74             Thread.sleep(1000);
75          }
76
77       }
78       catch(Exception JavaDoc e)
79       {
80          e.printStackTrace();
81       }
82    }
83
84    /**
85     * Simple invocation handler implementation.
86     */

87    public static class TestStreamInvocationHandler implements StreamInvocationHandler
88    {
89       private int streamSize = 0;
90
91       /**
92        * takes the incoming stream and writes out to a file specified by the other param specified.
93        * will return the size of the file.
94        *
95        * @param stream
96        * @param param
97        * @return
98        */

99       public Object JavaDoc handleStream(InputStream JavaDoc stream, Object JavaDoc param)
100       {
101          try
102          {
103             System.out.println("Received input stream from client to write out to file " + param);
104
105             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc newFile = new File JavaDoc((String JavaDoc) param);
120             if(!newFile.exists())
121             {
122                newFile.createNewFile();
123             }
124
125             FileWriter JavaDoc fileWriter = new FileWriter JavaDoc(newFile, false);
126             fileWriter.write(new String JavaDoc(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 JavaDoc e)
132          {
133             e.printStackTrace();
134          }
135          finally
136          {
137             try
138             {
139                stream.close();
140             }
141             catch(IOException JavaDoc e)
142             {
143                e.printStackTrace();
144             }
145          }
146          return new Integer JavaDoc(streamSize);
147       }
148
149       /**
150        * called to handle a specific invocation
151        *
152        * @param invocation
153        * @return
154        * @throws Throwable
155        */

156       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
157       {
158          // Print out the invocation request
159
System.out.println("Invocation request is: " + invocation.getParameter());
160
161          // Just going to return static string as this is just simple example code.
162
return new Integer JavaDoc(streamSize);
163       }
164
165       /**
166        * Adds a callback handler that will listen for callbacks from
167        * the server invoker handler.
168        *
169        * @param callbackHandler
170        */

171       public void addListener(InvokerCallbackHandler callbackHandler)
172       {
173          // NO OP as do not handling callback listeners in this example
174
}
175
176       /**
177        * Removes the callback handler that was listening for callbacks
178        * from the server invoker handler.
179        *
180        * @param callbackHandler
181        */

182       public void removeListener(InvokerCallbackHandler callbackHandler)
183       {
184          // NO OP as do not handling callback listeners in this example
185
}
186
187       /**
188        * set the mbean server that the handler can reference
189        *
190        * @param server
191        */

192       public void setMBeanServer(MBeanServer JavaDoc server)
193       {
194          // NO OP as do not need reference to MBeanServer for this handler
195
}
196
197       /**
198        * set the invoker that owns this handler
199        *
200        * @param invoker
201        */

202       public void setInvoker(ServerInvoker invoker)
203       {
204          // NO OP as do not need reference back to the server invoker
205
}
206    }
207 }
Popular Tags