KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > simple > SimpleServer


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.simple;
8
9 import javax.management.MBeanServer JavaDoc;
10 import org.jboss.remoting.InvocationRequest;
11 import org.jboss.remoting.InvokerLocator;
12 import org.jboss.remoting.ServerInvocationHandler;
13 import org.jboss.remoting.ServerInvoker;
14 import org.jboss.remoting.callback.InvokerCallbackHandler;
15 import org.jboss.remoting.transport.Connector;
16
17 /**
18  * Simple remoting server. Uses inner class SampleInvocationHandler
19  * as the invocation target handler class.
20  *
21  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
22  */

23 public class SimpleServer
24 {
25    // Default locator values
26
private static String JavaDoc transport = "socket";
27    private static String JavaDoc host = "localhost";
28    private static int port = 5400;
29
30    // String to be returned from invocation handler upon client invocation calls.
31
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
32
33
34    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
35    {
36       // create the InvokerLocator based on url string format
37
// to indicate the transport, host, and port to use for the
38
// server invoker.
39
InvokerLocator locator = new InvokerLocator(locatorURI);
40       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
41       Connector connector = new Connector();
42       connector.setInvokerLocator(locator.getLocatorURI());
43       // creates all the connector's needed resources, such as the server invoker
44
connector.create();
45
46       // create the handler to receive the invocation request from the client for processing
47
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
48       // first parameter is sub-system name. can be any String value.
49
connector.addInvocationHandler("sample", invocationHandler);
50
51       // start with a new non daemon thread so
52
// server will wait for request and not exit
53
connector.start();
54
55    }
56
57    /**
58     * Can pass transport and port to be used as parameters.
59     * Valid transports are 'rmi' and 'socket'.
60     *
61     * @param args
62     */

63    public static void main(String JavaDoc[] args)
64    {
65       if(args != null && args.length == 3)
66       {
67          transport = args[0];
68          host = args[1];
69          port = Integer.parseInt(args[2]);
70       }
71       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
72       SimpleServer server = new SimpleServer();
73       try
74       {
75          server.setupServer(locatorURI);
76
77          // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
78
while(true)
79          {
80             Thread.sleep(1000);
81          }
82
83       }
84       catch(Exception JavaDoc e)
85       {
86          e.printStackTrace();
87       }
88    }
89
90    /**
91     * Simple invocation handler implementation.
92     * This is the code that will be called with the invocation payload from the client.
93     */

94    public static class SampleInvocationHandler implements ServerInvocationHandler
95    {
96       /**
97        * called to handle a specific invocation
98        *
99        * @param invocation
100        * @return
101        * @throws Throwable
102        */

103       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
104       {
105          // Print out the invocation request
106
System.out.println("Invocation request is: " + invocation.getParameter());
107          System.out.println("Returning response of: " + RESPONSE_VALUE);
108          // Just going to return static string as this is just simple example code.
109
return RESPONSE_VALUE;
110       }
111
112       /**
113        * Adds a callback handler that will listen for callbacks from
114        * the server invoker handler.
115        *
116        * @param callbackHandler
117        */

118       public void addListener(InvokerCallbackHandler callbackHandler)
119       {
120          // NO OP as do not handling callback listeners in this example
121
}
122
123       /**
124        * Removes the callback handler that was listening for callbacks
125        * from the server invoker handler.
126        *
127        * @param callbackHandler
128        */

129       public void removeListener(InvokerCallbackHandler callbackHandler)
130       {
131          // NO OP as do not handling callback listeners in this example
132
}
133
134       /**
135        * set the mbean server that the handler can reference
136        *
137        * @param server
138        */

139       public void setMBeanServer(MBeanServer JavaDoc server)
140       {
141          // NO OP as do not need reference to MBeanServer for this handler
142
}
143
144       /**
145        * set the invoker that owns this handler
146        *
147        * @param invoker
148        */

149       public void setInvoker(ServerInvoker invoker)
150       {
151          // NO OP as do not need reference back to the server invoker
152
}
153
154    }
155 }
Popular Tags