KickJava   Java API By Example, From Geeks To Geeks.

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

24 public class SimpleServer
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    // String to be returned from invocation handler upon client invocation calls.
32
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
33
34
35    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
36    {
37       InvokerLocator locator = new InvokerLocator(locatorURI);
38       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
39       Connector connector = new Connector();
40       connector.setInvokerLocator(locator.getLocatorURI());
41       connector.start();
42
43       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
44       // first parameter is sub-system name. can be any String value.
45
connector.addInvocationHandler("sample", invocationHandler);
46    }
47
48    /**
49     * Can pass transport and port to be used as parameters.
50     * Valid transports are 'rmi' and 'socket'.
51     *
52     * @param args
53     */

54    public static void main(String JavaDoc[] args)
55    {
56       if(args != null && args.length == 2)
57       {
58          transport = args[0];
59          port = Integer.parseInt(args[1]);
60       }
61       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
62       SimpleServer server = new SimpleServer();
63       try
64       {
65          server.setupServer(locatorURI);
66
67          // sleep the thread for 10 seconds while waiting for client to call
68
Thread.sleep(10000);
69       }
70       catch(Exception JavaDoc e)
71       {
72          e.printStackTrace();
73       }
74    }
75
76    /**
77     * Simple invocation handler implementation.
78     */

79    public static class SampleInvocationHandler implements ServerInvocationHandler
80    {
81       /**
82        * called to handle a specific invocation
83        *
84        * @param invocation
85        * @return
86        * @throws Throwable
87        */

88       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
89       {
90          // Print out the invocation request
91
System.out.println("Invocation request is: " + invocation.getParameter());
92
93          // Just going to return static string as this is just simple example code.
94
return RESPONSE_VALUE;
95       }
96
97       /**
98        * Adds a callback handler that will listen for callbacks from
99        * the server invoker handler.
100        *
101        * @param callbackHandler
102        */

103       public void addListener(InvokerCallbackHandler callbackHandler)
104       {
105          // NO OP as do not handling callback listeners in this example
106
}
107
108       /**
109        * Removes the callback handler that was listening for callbacks
110        * from the server invoker handler.
111        *
112        * @param callbackHandler
113        */

114       public void removeListener(InvokerCallbackHandler callbackHandler)
115       {
116          // NO OP as do not handling callback listeners in this example
117
}
118
119       /**
120        * set the mbean server that the handler can reference
121        *
122        * @param server
123        */

124       public void setMBeanServer(MBeanServer JavaDoc server)
125       {
126          // NO OP as do not need reference to MBeanServer for this handler
127
}
128
129       /**
130        * set the invoker that owns this handler
131        *
132        * @param invoker
133        */

134       public void setInvoker(ServerInvoker invoker)
135       {
136          // NO OP as do not need reference back to the server invoker
137
}
138
139    }
140 }
Popular Tags