KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > connection > ConnectionValidationServer


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

22 public class ConnectionValidationServer extends ServerTestCase
23 {
24    // Default locator values
25
private static String JavaDoc transport = "socket";
26    private static String JavaDoc host = "localhost";
27    private static int port = 5400;
28    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
29
30    private Connector connector = null;
31
32    // String to be returned from invocation handler upon client invocation calls.
33
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
34
35
36    public void setupServer() throws Exception JavaDoc
37    {
38       InvokerLocator locator = new InvokerLocator(locatorURI);
39       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
40       connector = new Connector();
41       connector.setInvokerLocator(locator.getLocatorURI());
42       connector.create();
43       SimpleServer.SampleInvocationHandler invocationHandler = new SimpleServer.SampleInvocationHandler();
44       // first parameter is sub-system name. can be any String value.
45
connector.addInvocationHandler("sample", invocationHandler);
46       connector.start();
47    }
48
49    public void testShutdown() throws Exception JavaDoc
50    {
51       // sleep the thread for 10 seconds while waiting for client to call
52
Thread.currentThread().sleep(10000);
53       connector.stop();
54       connector.destroy();
55       connector = null;
56    }
57
58    public void setUp() throws Exception JavaDoc
59    {
60       setupServer();
61    }
62
63    public void tearDown() throws Exception JavaDoc
64    {
65       if(connector != null)
66       {
67          connector.stop();
68          connector.destroy();
69       }
70    }
71
72    /**
73     * Can pass transport and port to be used as parameters.
74     * Valid transports are 'rmi' and 'socket'.
75     *
76     * @param args
77     */

78    public static void main(String JavaDoc[] args)
79    {
80       if(args != null && args.length == 3)
81       {
82          transport = args[0];
83          host = args[1];
84          port = Integer.parseInt(args[2]);
85       }
86       ConnectionValidationServer server = new ConnectionValidationServer();
87       try
88       {
89          server.setupServer();
90          server.testShutdown();
91       }
92       catch(Exception JavaDoc e)
93       {
94          e.printStackTrace();
95       }
96    }
97
98    /**
99     * Simple invocation handler implementation.
100     */

101    public static class SampleInvocationHandler implements ServerInvocationHandler
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 RESPONSE_VALUE;
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    }
162
163 }
Popular Tags