KickJava   Java API By Example, From Geeks To Geeks.

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

28 public class CallbackTestServer extends ServerTestCase
29 {
30    // Default locator values
31
private static String JavaDoc transport = "socket";
32    private static String JavaDoc host = "localhost";
33    private static int port = 5500;
34
35    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
36    private Connector connector;
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
43    public void setupServer() throws Exception JavaDoc
44    {
45       InvokerLocator locator = new InvokerLocator(locatorURI);
46       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
47       connector = new Connector();
48       connector.setInvokerLocator(locator.getLocatorURI());
49       connector.start();
50
51       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
52       // first parameter is sub-system name. can be any String value.
53
connector.addInvocationHandler("sample", invocationHandler);
54    }
55
56    protected void setUp() throws Exception JavaDoc
57    {
58       setupServer();
59    }
60
61    protected void tearDown() throws Exception JavaDoc
62    {
63       if(connector != null)
64       {
65          connector.stop();
66          connector.destroy();
67       }
68    }
69
70    /**
71     * Simple invocation handler implementation.
72     */

73    public static class SampleInvocationHandler implements ServerInvocationHandler
74    {
75
76       private List JavaDoc listeners = new ArrayList JavaDoc();
77
78
79       /**
80        * called to handle a specific invocation
81        *
82        * @param invocation
83        * @return
84        * @throws Throwable
85        */

86       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
87       {
88          // Just going to return static string as this is just simple example code.
89

90          // Will also fire callback to listeners if they were to exist using
91
// simple invocation request.
92
Callback callback = new Callback(CALLBACK_VALUE);
93          Iterator JavaDoc itr = listeners.iterator();
94          while(itr.hasNext())
95          {
96             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
97             callbackHandler.handleCallback(callback);
98          }
99
100          return RESPONSE_VALUE;
101
102       }
103
104       /**
105        * Adds a callback handler that will listen for callbacks from
106        * the server invoker handler.
107        *
108        * @param callbackHandler
109        */

110       public void addListener(InvokerCallbackHandler callbackHandler)
111       {
112          listeners.add(callbackHandler);
113       }
114
115       /**
116        * Removes the callback handler that was listening for callbacks
117        * from the server invoker handler.
118        *
119        * @param callbackHandler
120        */

121       public void removeListener(InvokerCallbackHandler callbackHandler)
122       {
123          listeners.remove(callbackHandler);
124       }
125
126       /**
127        * set the mbean server that the handler can reference
128        *
129        * @param server
130        */

131       public void setMBeanServer(MBeanServer JavaDoc server)
132       {
133          // NO OP as do not need reference to MBeanServer for this handler
134
}
135
136       /**
137        * set the invoker that owns this handler
138        *
139        * @param invoker
140        */

141       public void setInvoker(ServerInvoker invoker)
142       {
143          // NO OP as do not need reference back to the server invoker
144
}
145
146    }
147 }
Popular Tags