KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > samples > detection > SimpleDetectorServer


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.detection;
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.detection.multicast.MulticastDetector;
15 import org.jboss.remoting.network.NetworkRegistry;
16 import org.jboss.remoting.transport.Connector;
17
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.ObjectName JavaDoc;
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 SimpleDetectorServer
29 {
30    // Default locator values
31
private static String JavaDoc transport = "socket";
32    private static String JavaDoc host = "localhost";
33    private static int port = 5400;
34
35    // String to be returned from invocation handler upon client invocation calls.
36
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
37
38
39    public void setupDetector() throws Exception JavaDoc
40    {
41       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
42
43       NetworkRegistry registry = NetworkRegistry.getInstance();
44       server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
45
46       MulticastDetector detector = new MulticastDetector();
47       server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=MulticastDetector"));
48       detector.start();
49    }
50
51    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
52    {
53       InvokerLocator locator = new InvokerLocator(locatorURI);
54       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
55       Connector connector = new Connector();
56       connector.setInvokerLocator(locator.getLocatorURI());
57       connector.start();
58
59       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
60       // first parameter is sub-system name. can be any String value.
61
connector.addInvocationHandler("sample", invocationHandler);
62    }
63
64    /**
65     * Can pass transport and port to be used as parameters.
66     * Valid transports are 'rmi' and 'socket'.
67     *
68     * @param args
69     */

70    public static void main(String JavaDoc[] args)
71    {
72       if(args != null && args.length == 2)
73       {
74          transport = args[0];
75          port = Integer.parseInt(args[1]);
76       }
77       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
78       SimpleDetectorServer server = new SimpleDetectorServer();
79       try
80       {
81          server.setupDetector();
82          server.setupServer(locatorURI);
83
84          // sleep the thread for 10 seconds while waiting for client to call
85
Thread.sleep(10000);
86
87       }
88       catch(Exception JavaDoc e)
89       {
90          e.printStackTrace();
91       }
92    }
93
94    /**
95     * Simple invocation handler implementation.
96     */

97    public static class SampleInvocationHandler implements ServerInvocationHandler
98    {
99       /**
100        * called to handle a specific invocation
101        *
102        * @param invocation
103        * @return
104        * @throws Throwable
105        */

106       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
107       {
108          // Print out the invocation request
109
System.out.println("Invocation request is: " + invocation.getParameter());
110
111          // Just going to return static string as this is just simple example code.
112
return RESPONSE_VALUE;
113       }
114
115       /**
116        * Adds a callback handler that will listen for callbacks from
117        * the server invoker handler.
118        *
119        * @param callbackHandler
120        */

121       public void addListener(InvokerCallbackHandler callbackHandler)
122       {
123          // NO OP as do not handling callback listeners in this example
124
}
125
126       /**
127        * Removes the callback handler that was listening for callbacks
128        * from the server invoker handler.
129        *
130        * @param callbackHandler
131        */

132       public void removeListener(InvokerCallbackHandler callbackHandler)
133       {
134          // NO OP as do not handling callback listeners in this example
135
}
136
137       /**
138        * set the mbean server that the handler can reference
139        *
140        * @param server
141        */

142       public void setMBeanServer(MBeanServer JavaDoc server)
143       {
144          // NO OP as do not need reference to MBeanServer for this handler
145
}
146
147       /**
148        * set the invoker that owns this handler
149        *
150        * @param invoker
151        */

152       public void setInvoker(ServerInvoker invoker)
153       {
154          // NO OP as do not need reference back to the server invoker
155
}
156
157    }
158 }
Popular Tags