KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > stream > StreamServer


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.stream;
8
9 import java.io.InputStream JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.UnknownHostException JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.logging.Logger;
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.InvokerCallbackHandler;
19 import org.jboss.remoting.transport.Connector;
20
21 /**
22  * This is a helper class that runs internal to remoting on the
23  * client side. It contains a reference to a local input stream
24  * and creates a remoting server to receive calls from a target
25  * remoting server (via calls from a StreamHandler initiated by a
26  * server invoker handler).
27  * <p/>
28  * NOTE: That once this class receives the close() method called
29  * from the server, it will also stop and destroy the internal
30  * remoting server, since is assumed there will be no more callbacks
31  * (since the stream itself is closed).
32  *
33  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
34  */

35 public class StreamServer
36 {
37    private InputStream JavaDoc streamSource = null;
38
39    private String JavaDoc transport = "socket";
40    private String JavaDoc host = "localhost";
41    private int port = 5405;
42
43    private Connector connector = null;
44
45    private static final Logger log = Logger.getLogger(StreamServer.class);
46
47    public static final String JavaDoc STREAM_TRANSPORT_KEY = "remoting.stream.transport";
48    public static final String JavaDoc STREAM_HOST_KEY = "remoting.stream.host";
49    public static final String JavaDoc STREAM_PORT_KEY = "remoting.stream.port";
50
51
52    /**
53     * Creates the server wrapped around the specified input stream.
54     * This will create the remoting server as well.
55     *
56     * @param stream
57     * @throws Exception
58     */

59    public StreamServer(InputStream JavaDoc stream) throws Exception JavaDoc
60    {
61       this.streamSource = stream;
62       String JavaDoc locatorURI = getLocatorURI();
63       setupServer(locatorURI);
64    }
65
66    private String JavaDoc getLocatorURI()
67    {
68       // check for system properties for locator values
69
transport = System.getProperty(STREAM_TRANSPORT_KEY, transport);
70       try
71       {
72          host = InetAddress.getLocalHost().getHostName();
73       }
74       catch(UnknownHostException JavaDoc e)
75       {
76          try
77          {
78             host = InetAddress.getLocalHost().getHostAddress();
79          }
80          catch(UnknownHostException JavaDoc e1)
81          {
82             log.error("Stream server could not determine local host or address.");
83          }
84       }
85       host = System.getProperty(STREAM_HOST_KEY, host);
86
87       String JavaDoc sPort = System.getProperty(STREAM_PORT_KEY, "" + port);
88       try
89       {
90          port = Integer.parseInt(sPort);
91       }
92       catch(NumberFormatException JavaDoc e)
93       {
94          log.error("Stream server could not convert specified port " + sPort + " to a number.");
95       }
96
97       return transport + "://" + host + ":" + port;
98    }
99
100    /**
101     * Gets the locator to call back on this server to get the inputstream data.
102     *
103     * @return
104     * @throws Exception
105     */

106    public String JavaDoc getInvokerLocator() throws Exception JavaDoc
107    {
108       String JavaDoc locator = null;
109
110       if(connector != null)
111       {
112          locator = connector.getInvokerLocator();
113       }
114       return locator;
115    }
116
117    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
118    {
119       InvokerLocator locator = new InvokerLocator(locatorURI);
120
121       connector = new Connector();
122       connector.setInvokerLocator(locator.getLocatorURI());
123       connector.create();
124
125       ServerInvocationHandler invocationHandler = new Handler();
126       connector.addInvocationHandler("stream", invocationHandler);
127
128       connector.start();
129
130    }
131
132    /**
133     * Handler for accepting method calls on the input stream and perform the coresponding
134     * method call on the original input stream and returning the data.
135     */

136    public class Handler implements ServerInvocationHandler
137    {
138       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
139       {
140          Object JavaDoc obj = invocation.getParameter();
141
142          // will expect the parameter to ALWAYS be of type StreamCallPaylod
143
if(obj instanceof StreamCallPayload)
144          {
145             StreamCallPayload payload = (StreamCallPayload) obj;
146             String JavaDoc method = payload.getMethod();
147
148             if(StreamHandler.READ.equals(method))
149             {
150                int i = streamSource.read();
151                return new Integer JavaDoc(i);
152             }
153             else if(StreamHandler.AVAILABLE.equals(method))
154             {
155                int i = streamSource.available();
156                return new Integer JavaDoc(i);
157             }
158             else if(StreamHandler.CLOSE.equals(method))
159             {
160                streamSource.close();
161             }
162             else if(StreamHandler.RESET.equals(method))
163             {
164                streamSource.reset();
165             }
166             else if(StreamHandler.MARKSUPPORTED.equals(method))
167             {
168                boolean b = streamSource.markSupported();
169                return new Boolean JavaDoc(b);
170             }
171             else if(StreamHandler.MARKREADLIMIT.equals(method))
172             {
173                Object JavaDoc[] param = payload.getParams();
174                Integer JavaDoc intr = (Integer JavaDoc) param[0];
175                int readLimit = intr.intValue();
176                streamSource.mark(readLimit);
177             }
178             else if(StreamHandler.SKIP.equals(method))
179             {
180                Object JavaDoc[] param = payload.getParams();
181                Long JavaDoc lg = (Long JavaDoc) param[0];
182                long n = lg.longValue();
183                long ret = streamSource.skip(n);
184                return new Long JavaDoc(ret);
185             }
186             else if(StreamHandler.READBYTEARRAY.equals(method))
187             {
188                Object JavaDoc[] param = payload.getParams();
189                byte[] byteParam = (byte[]) param[0];
190                int i = streamSource.read(byteParam);
191                StreamCallPayload ret = new StreamCallPayload(StreamHandler.READBYTEARRAY);
192                ret.setParams(new Object JavaDoc[]{byteParam, new Integer JavaDoc(i)});
193                return ret;
194             }
195             else
196             {
197                throw new Exception JavaDoc("Unsupported method call - " + method);
198             }
199          }
200          else
201          {
202             log.error("Can not process invocation request because is not of type StreamCallPayload.");
203             throw new Exception JavaDoc("Invalid payload type. Must be of type StreamCallPayload.");
204          }
205          return null;
206       }
207
208       /**
209        * Adds a callback handler that will listen for callbacks from
210        * the server invoker handler.
211        *
212        * @param callbackHandler
213        */

214       public void addListener(InvokerCallbackHandler callbackHandler)
215       {
216          // NO OP as do not handling callback listeners in this example
217
}
218
219       /**
220        * Removes the callback handler that was listening for callbacks
221        * from the server invoker handler.
222        *
223        * @param callbackHandler
224        */

225       public void removeListener(InvokerCallbackHandler callbackHandler)
226       {
227          // NO OP as do not handling callback listeners in this example
228
}
229
230       /**
231        * set the mbean server that the handler can reference
232        *
233        * @param server
234        */

235       public void setMBeanServer(MBeanServer JavaDoc server)
236       {
237          // NO OP as do not need reference to MBeanServer for this handler
238
}
239
240       /**
241        * set the invoker that owns this handler
242        *
243        * @param invoker
244        */

245       public void setInvoker(ServerInvoker invoker)
246       {
247          // NO OP as do not need reference back to the server invoker
248
}
249    }
250
251 }
Popular Tags