KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > handler > mbean > ServerTest


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.handler.mbean;
8
9 import org.jboss.remoting.AbstractInvokerTest;
10 import org.jboss.remoting.InvokerLocator;
11 import org.jboss.remoting.transport.Connector;
12
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.MBeanServerFactory JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
19  */

20 public class ServerTest extends AbstractInvokerTest
21 {
22    // Default locator values
23
private static String JavaDoc transport = "socket";
24    private static String JavaDoc host = "localhost";
25    private static int port = 5400;
26
27    private String JavaDoc locatorURI = null;
28
29    // String to be returned from invocation handler upon client invocation calls.
30
public static final String JavaDoc RESPONSE_VALUE = "This is the return from the TestServer invocation";
31
32    public ServerTest(String JavaDoc name)
33    {
34       super(name);
35    }
36
37    public ServerTest(String JavaDoc name, String JavaDoc locatorURI)
38    {
39       super(name);
40       this.locatorURI = locatorURI;
41    }
42
43    public ServerTest(String JavaDoc name, int numberOfInstances)
44    {
45       super(name, numberOfInstances);
46    }
47
48    public ServerTest(String JavaDoc name, String JavaDoc transport, int port)
49    {
50       super(name, transport, port);
51    }
52
53    public ServerTest(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
54    {
55       super(name, transport, port, numberOfInstances);
56    }
57
58
59    public void setupServer() throws Exception JavaDoc
60    {
61       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
62
63       InvokerLocator locator = new InvokerLocator(locatorURI);
64       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
65       Connector connector = new Connector();
66       connector.setInvokerLocator(locator.getLocatorURI());
67       connector.start();
68
69       server.registerMBean(connector, new ObjectName JavaDoc("test:type=connector,transport=socket"));
70
71       // now create Mbean handler and register with mbean server
72
MBeanHandler handler = new MBeanHandler();
73       ObjectName JavaDoc objName = new ObjectName JavaDoc("test:type=handler");
74       server.registerMBean(handler, objName);
75
76       connector.addInvocationHandler("test", objName);
77    }
78
79    public void serverTest() throws Exception JavaDoc
80    {
81       try
82       {
83          setupServer();
84          startup(getNumberOfInstances());
85          shutdown();
86       }
87       catch(Exception JavaDoc e)
88       {
89          throw e;
90       }
91    }
92
93
94    /**
95     * Can pass transport and port to be used as parameters.
96     * Valid transports are 'rmi' and 'socket'.
97     *
98     * @param args
99     */

100    public static void main(String JavaDoc[] args)
101    {
102       if(args != null && args.length == 2)
103       {
104          transport = args[0];
105          port = Integer.parseInt(args[1]);
106       }
107       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
108       ServerTest serverTest = new ServerTest(ServerTest.class.getName(), locatorURI);
109       try
110       {
111          serverTest.serverTest();
112
113          // sleep the thread for 10 seconds while waiting for client to call
114
Thread.sleep(10000);
115       }
116       catch(Exception JavaDoc e)
117       {
118          e.printStackTrace();
119          System.exit(1);
120       }
121       System.exit(0);
122    }
123 }
Popular Tags