KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > socket > InvokerServerTest


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.transport.socket;
8
9 import org.apache.log4j.Level;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.AbstractInvokerTest;
12 import org.jboss.remoting.InvokerLocator;
13 import org.jboss.remoting.ServerInvocationHandler;
14 import org.jboss.remoting.transport.Connector;
15 import org.jboss.remoting.transport.mock.MockServerInvocationHandler;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Random JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * This is the concrete test for invoker server.
24  *
25  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
26  */

27 public class InvokerServerTest extends AbstractInvokerTest
28 {
29    private static final Logger log = Logger.getLogger(InvokerServerTest.class);
30
31    public InvokerServerTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    public InvokerServerTest(int instances)
37    {
38       super(InvokerServerTest.class.getName(), instances);
39    }
40
41    public InvokerServerTest(String JavaDoc transport, int port)
42    {
43       super(InvokerServerTest.class.getName(), transport, port);
44    }
45
46    public InvokerServerTest(String JavaDoc transport, int port, int instances)
47    {
48       super(InvokerServerTest.class.getName(), transport, port, instances);
49    }
50
51    public void init(Map JavaDoc metatdata) throws Exception JavaDoc
52    {
53       if(port < 0)
54       {
55          port = Math.abs(new Random JavaDoc().nextInt(2000) + 2000);
56       }
57       log.debug("port = " + port);
58
59 // InvokerRegistry.registerInvoker(getTransport(), SocketClientInvoker.class, SocketServerInvoker.class);
60
// InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
61
Connector connector = new Connector();
62       InvokerLocator locator = new InvokerLocator(buildLocatorURI(metatdata));
63       connector.setInvokerLocator(locator.getLocatorURI());
64       connector.start();
65       connector.addInvocationHandler(getSubsystem(), getServerInvocationHandler());
66    }
67
68    private String JavaDoc buildLocatorURI(Map JavaDoc metadata)
69    {
70       if(metadata == null || metadata.size() == 0)
71       {
72          return getTransport() + "://localhost:" + port;
73       }
74       else
75       {
76          StringBuffer JavaDoc uriBuffer = new StringBuffer JavaDoc(getTransport() + "://localhost:" + port);
77
78          Set JavaDoc keys = metadata.keySet();
79          Iterator JavaDoc itr = keys.iterator();
80          while(itr.hasNext())
81          {
82             String JavaDoc key = (String JavaDoc) itr.next();
83             String JavaDoc value = (String JavaDoc) metadata.get(key);
84             uriBuffer.append(key + "=" + value + ",");
85          }
86          return uriBuffer.substring(0, uriBuffer.length() - 1);
87       }
88    }
89
90    protected String JavaDoc getSubsystem()
91    {
92       return "mock";
93    }
94
95    protected ServerInvocationHandler getServerInvocationHandler()
96    {
97       return new MockServerInvocationHandler();
98    }
99
100    public void serverTest() throws Exception JavaDoc
101    {
102       try
103       {
104          init(null);
105          startup(getNumberOfInstances());
106          shutdown();
107       }
108       catch(Exception JavaDoc e)
109       {
110          log.error("Error in serverTest() of " + this.getClass().getName(), e);
111          throw e;
112       }
113    }
114
115    public static void main(String JavaDoc[] args)
116    {
117       org.apache.log4j.BasicConfigurator.configure();
118       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
119       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
120       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
121       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
122
123       InvokerServerTest server = null;
124       if(args.length == 1)
125       {
126          int instances = Integer.parseInt(args[0]);
127          server = new InvokerServerTest(instances);
128       }
129       else if(args.length == 2)
130       {
131          String JavaDoc transport = args[0];
132          int port = Integer.parseInt(args[1]);
133          server = new InvokerServerTest(transport, port);
134       }
135       else if(args.length == 3)
136       {
137          String JavaDoc transport = args[0];
138          int port = Integer.parseInt(args[1]);
139          int instances = Integer.parseInt(args[2]);
140          server = new InvokerServerTest(transport, port, instances);
141       }
142       else
143       {
144          server = new InvokerServerTest(InvokerServerTest.class.getName());
145          System.out.println("Using default transport (" + server.getTransport() +
146                             ") and default port (" + server.getPort() + ") and " +
147                             "default number of instances (" + server.getNumberOfInstances() + ")" +
148                             "\nCan enter transport, port, and instances via command line.");
149       }
150
151       try
152       {
153          server.serverTest();
154       }
155       catch(Exception JavaDoc e)
156       {
157          e.printStackTrace();
158          System.exit(1);
159       }
160       System.exit(0);
161    }
162
163 }
164
Popular Tags