KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > samples > callback > CallbackServer


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

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

85    public static class SampleInvocationHandler implements ServerInvocationHandler
86    {
87
88       private List JavaDoc listeners = new ArrayList JavaDoc();
89
90
91       /**
92        * called to handle a specific invocation
93        *
94        * @param invocation
95        * @return
96        * @throws Throwable
97        */

98       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
99       {
100          // Just going to return static string as this is just simple example code.
101

102          // Will also fire callback to listeners if they were to exist using
103
// simple invocation request.
104
Callback callback = new Callback("This is the payload of callback invocation.");
105          Iterator JavaDoc itr = listeners.iterator();
106          while(itr.hasNext())
107          {
108             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
109             try
110             {
111                callbackHandler.handleCallback(callback);
112             }
113             catch(HandleCallbackException e)
114             {
115                e.printStackTrace();
116             }
117          }
118
119          return RESPONSE_VALUE;
120
121       }
122
123       /**
124        * Adds a callback handler that will listen for callbacks from
125        * the server invoker handler.
126        *
127        * @param callbackHandler
128        */

129       public void addListener(InvokerCallbackHandler callbackHandler)
130       {
131          listeners.add(callbackHandler);
132       }
133
134       /**
135        * Removes the callback handler that was listening for callbacks
136        * from the server invoker handler.
137        *
138        * @param callbackHandler
139        */

140       public void removeListener(InvokerCallbackHandler callbackHandler)
141       {
142          listeners.remove(callbackHandler);
143       }
144
145       /**
146        * set the mbean server that the handler can reference
147        *
148        * @param server
149        */

150       public void setMBeanServer(MBeanServer JavaDoc server)
151       {
152          // NO OP as do not need reference to MBeanServer for this handler
153
}
154
155       /**
156        * set the invoker that owns this handler
157        *
158        * @param invoker
159        */

160       public void setInvoker(ServerInvoker invoker)
161       {
162          // NO OP as do not need reference back to the server invoker
163
}
164
165    }
166 }
Popular Tags