KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > stream > StreamingTestServer


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

24 public class StreamingTestServer extends ServerTestCase
25 {
26    // Default locator values
27
private static String JavaDoc transport = "socket";
28    private static String JavaDoc host = "localhost";
29    private static int port = 5400;
30
31    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
32    private Connector connector = null;
33
34    public void setupServer() throws Exception JavaDoc
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       // first parameter is sub-system name. can be any String value.
44
connector.addInvocationHandler("test_stream", invocationHandler);
45
46       connector.start();
47    }
48
49    protected void setUp() throws Exception JavaDoc
50    {
51       setupServer();
52    }
53
54    protected void tearDown() throws Exception JavaDoc
55    {
56       if(connector != null)
57       {
58          connector.stop();
59          connector.destroy();
60       }
61    }
62
63    /**
64     * Can pass transport and port to be used as parameters.
65     * Valid transports are 'rmi' and 'socket'.
66     *
67     * @param args
68     */

69    public static void main(String JavaDoc[] 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          // sleep the thread for 10 seconds while waiting for client to call
84
Thread.sleep(10000);
85
86          server.tearDown();
87       }
88       catch(Exception JavaDoc e)
89       {
90          e.printStackTrace();
91       }
92    }
93
94    /**
95     * Simple invocation handler implementation.
96     */

97    public static class TestStreamInvocationHandler implements StreamInvocationHandler
98    {
99       private InputStream JavaDoc stream = null;
100
101       private int streamSize = 0;
102
103       /**
104        * called to handle a specific invocation
105        *
106        * @param invocation
107        * @return
108        * @throws Throwable
109        */

110       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
111       {
112          // Print out the invocation request
113
System.out.println("Invocation request is: " + invocation.getParameter());
114
115          // Just going to return static string as this is just simple example code.
116
return new Integer JavaDoc(streamSize);
117       }
118
119       /**
120        * Adds a callback handler that will listen for callbacks from
121        * the server invoker handler.
122        *
123        * @param callbackHandler
124        */

125       public void addListener(InvokerCallbackHandler callbackHandler)
126       {
127          // NO OP as do not handling callback listeners in this example
128
}
129
130       /**
131        * Removes the callback handler that was listening for callbacks
132        * from the server invoker handler.
133        *
134        * @param callbackHandler
135        */

136       public void removeListener(InvokerCallbackHandler callbackHandler)
137       {
138          // NO OP as do not handling callback listeners in this example
139
}
140
141       /**
142        * set the mbean server that the handler can reference
143        *
144        * @param server
145        */

146       public void setMBeanServer(MBeanServer JavaDoc server)
147       {
148          // NO OP as do not need reference to MBeanServer for this handler
149
}
150
151       /**
152        * set the invoker that owns this handler
153        *
154        * @param invoker
155        */

156       public void setInvoker(ServerInvoker invoker)
157       {
158          // NO OP as do not need reference back to the server invoker
159
}
160
161       public Object JavaDoc handleStream(InputStream JavaDoc stream, Object JavaDoc param)
162       {
163          this.stream = stream;
164
165          try
166          {
167             ByteArrayOutputStream JavaDoc out=new ByteArrayOutputStream JavaDoc();
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 JavaDoc(bytes));
181          }
182          catch(IOException JavaDoc e)
183          {
184             e.printStackTrace();
185          }
186          finally
187          {
188             try
189             {
190                stream.close();
191             }
192             catch(IOException JavaDoc e)
193             {
194                e.printStackTrace();
195             }
196          }
197          return param;
198       }
199    }
200
201 }
Popular Tags