KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > http > HTTPInvokerTestServer


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.transport.http;
8
9 import org.jboss.remoting.AbstractInvokerTest;
10 import org.jboss.remoting.InvocationRequest;
11 import org.jboss.remoting.InvokerCallbackHandler;
12 import org.jboss.remoting.InvokerLocator;
13 import org.jboss.remoting.ServerInvocationHandler;
14 import org.jboss.remoting.ServerInvoker;
15 import org.jboss.remoting.transport.Connector;
16
17 import javax.management.MBeanServer JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
21  */

22 public class HTTPInvokerTestServer extends AbstractInvokerTest
23 {
24    // Default locator values
25
private static String JavaDoc transport = "http";
26    private static String JavaDoc host = "localhost";
27    private static int port = 8888;
28
29    // String to be returned from invocation handler upon client invocation calls.
30
public static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
31    public static final ComplexObject OBJECT_RESPONSE_VALUE = new ComplexObject(5, "dub", false);
32
33    public static final String JavaDoc NULL_RETURN_PARAM = "return_null";
34    public static final String JavaDoc OBJECT_RETURN_PARAM = "return_object";
35
36    public HTTPInvokerTestServer(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    public HTTPInvokerTestServer(String JavaDoc name, int numberOfInstances)
42    {
43       super(name, numberOfInstances);
44    }
45
46    public HTTPInvokerTestServer(String JavaDoc name, String JavaDoc transport, int port)
47    {
48       super(name, transport, port);
49    }
50
51    public HTTPInvokerTestServer(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
52    {
53       super(name, transport, port, numberOfInstances);
54    }
55
56
57    public void setupServer() throws Exception JavaDoc
58    {
59       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
60       InvokerLocator locator = new InvokerLocator(locatorURI);
61       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
62       Connector connector = new Connector();
63       connector.setInvokerLocator(locator.getLocatorURI());
64       connector.start();
65
66       HTTPInvokerTestServer.SampleInvocationHandler invocationHandler = new HTTPInvokerTestServer.SampleInvocationHandler();
67       // first parameter is sub-system name. can be any String value.
68
connector.addInvocationHandler("sample", invocationHandler);
69    }
70
71    public void serverTest() throws Exception JavaDoc
72    {
73       try
74       {
75          setupServer();
76          startup(getNumberOfInstances());
77          shutdown();
78       }
79       catch(Exception JavaDoc e)
80       {
81          throw e;
82       }
83    }
84
85
86    /**
87     * Can pass transport and port to be used as parameters.
88     * Valid transports are 'rmi' and 'socket'.
89     *
90     * @param args
91     */

92    public static void main(String JavaDoc[] args)
93    {
94       int numOfInstance = 2;
95
96       if(args != null && args.length == 1)
97       {
98          numOfInstance = Integer.parseInt(args[0]);
99       }
100       if(args != null && args.length == 2)
101       {
102          transport = args[0];
103          port = Integer.parseInt(args[1]);
104       }
105       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
106       HTTPInvokerTestServer server = new HTTPInvokerTestServer(HTTPInvokerTestServer.class.getName(), numOfInstance);
107       try
108       {
109          server.serverTest();
110
111          // sleep the thread for 10 seconds while waiting for client to call
112
//Thread.sleep(60000);
113
}
114       catch(Exception JavaDoc e)
115       {
116          e.printStackTrace();
117          System.exit(1);
118       }
119       System.exit(0);
120    }
121
122    /**
123     * Simple invocation handler implementation.
124     */

125    public static class SampleInvocationHandler implements ServerInvocationHandler
126    {
127
128
129       /**
130        * called to handle a specific invocation
131        *
132        * @param invocation
133        * @return
134        * @throws Throwable
135        */

136       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
137       {
138          // Print out the invocation request
139
System.out.println("Invocation request is: " + invocation.getParameter());
140          if(NULL_RETURN_PARAM.equals(invocation.getParameter()))
141          {
142             return null;
143          }
144          else if(invocation.getParameter() instanceof ComplexObject)
145          {
146             return OBJECT_RESPONSE_VALUE;
147          }
148          else
149          {
150             // Just going to return static string as this is just simple example code.
151
return RESPONSE_VALUE;
152          }
153       }
154
155       /**
156        * Adds a callback handler that will listen for callbacks from
157        * the server invoker handler.
158        *
159        * @param callbackHandler
160        */

161       public void addListener(InvokerCallbackHandler callbackHandler)
162       {
163          // NO OP as do not handling callback listeners in this example
164
}
165
166       /**
167        * Removes the callback handler that was listening for callbacks
168        * from the server invoker handler.
169        *
170        * @param callbackHandler
171        */

172       public void removeListener(InvokerCallbackHandler callbackHandler)
173       {
174          // NO OP as do not handling callback listeners in this example
175
}
176
177       /**
178        * set the mbean server that the handler can reference
179        *
180        * @param server
181        */

182       public void setMBeanServer(MBeanServer JavaDoc server)
183       {
184          // NO OP as do not need reference to MBeanServer for this handler
185
}
186
187       /**
188        * set the invoker that owns this handler
189        *
190        * @param invoker
191        */

192       public void setInvoker(ServerInvoker invoker)
193       {
194          // NO OP as do not need reference back to the server invoker
195
}
196
197    }
198
199 }
Popular Tags