KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > callback > CallbackTestServer


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

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

102    public static void main(String JavaDoc[] args)
103    {
104       if(args != null && args.length == 2)
105       {
106          transport = args[0];
107          port = Integer.parseInt(args[1]);
108       }
109       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
110       CallbackTestServer server = new CallbackTestServer(CallbackTestServer.class.getName(), locatorURI);
111       try
112       {
113          server.serverTest();
114       }
115       catch(Exception JavaDoc e)
116       {
117          e.printStackTrace();
118          System.exit(1);
119       }
120       System.exit(0);
121    }
122
123    /**
124     * Simple invocation handler implementation.
125     */

126    public static class SampleInvocationHandler implements ServerInvocationHandler
127    {
128
129       private List JavaDoc listeners = new ArrayList JavaDoc();
130
131
132       /**
133        * called to handle a specific invocation
134        *
135        * @param invocation
136        * @return
137        * @throws Throwable
138        */

139       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
140       {
141          // Just going to return static string as this is just simple example code.
142

143          // Will also fire callback to listeners if they were to exist using
144
// simple invocation request.
145
Callback callback = new Callback(CALLBACK_VALUE);
146          Iterator JavaDoc itr = listeners.iterator();
147          while(itr.hasNext())
148          {
149             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
150             callbackHandler.handleCallback(callback);
151          }
152
153          return RESPONSE_VALUE;
154
155       }
156
157       /**
158        * Adds a callback handler that will listen for callbacks from
159        * the server invoker handler.
160        *
161        * @param callbackHandler
162        */

163       public void addListener(InvokerCallbackHandler callbackHandler)
164       {
165          listeners.add(callbackHandler);
166       }
167
168       /**
169        * Removes the callback handler that was listening for callbacks
170        * from the server invoker handler.
171        *
172        * @param callbackHandler
173        */

174       public void removeListener(InvokerCallbackHandler callbackHandler)
175       {
176          listeners.remove(callbackHandler);
177       }
178
179       /**
180        * set the mbean server that the handler can reference
181        *
182        * @param server
183        */

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

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