KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > deploy > DualDeploymentTestCase


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.deploy;
8
9 import javax.management.MBeanServer JavaDoc;
10 import javax.management.MBeanServerFactory JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12 import org.jboss.remoting.Client;
13 import org.jboss.remoting.InvalidConfigurationException;
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.InvokerCallbackHandler;
19 import org.jboss.remoting.transport.Connector;
20
21 import junit.framework.TestCase;
22
23 /**
24  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
25  */

26 public class DualDeploymentTestCase extends TestCase
27 {
28    private static int idCounter = 1;
29
30    public DualDeploymentTestCase(String JavaDoc name)
31    {
32       super(name);
33    }
34
35    /**
36     * This test checks to see if use the exact same locator uri, will get the
37     * exact same server invoker (even though connectors are different). If add handler to
38     * each of the different connectors (with different subsystems), will really be two handlers in the same server invoker
39     *
40     * @throws Throwable
41     */

42    public void testSameLocator() throws Throwable JavaDoc
43    {
44       MBeanServer JavaDoc mbeanServer = MBeanServerFactory.createMBeanServer();
45
46       String JavaDoc locator1 = "socket://localhost:5701";
47       String JavaDoc locator2 = "socket://localhost:5701";
48
49       int id1 = 1;
50       int id2 = 2;
51
52       Connector connector1 = setupServer(locator1, mbeanServer);
53       SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1);
54       connector1.addInvocationHandler("sub1", invocationHandler1);
55
56       Connector connector2 = null;
57       try
58       {
59          connector2 = setupServer(locator2, mbeanServer);
60          SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2);
61          connector2.addInvocationHandler("sub2", invocationHandler2);
62       }
63       catch(InvalidConfigurationException e)
64       {
65          assertTrue("Got InvalidConfigurationException as expected.", true);
66          return;
67       }
68       finally
69       {
70          connector1.stop();
71          connector1.destroy();
72          if(connector2 != null)
73          {
74             connector2.stop();
75             connector2.destroy();
76          }
77       }
78       assertTrue("Did not get InvalidConfigurationException as expected.", false);
79
80    }
81
82    /**
83     * This checks to makes sure if you add a handler with the same subsystem value, it will return the existing
84     * one.
85     *
86     * @throws Throwable
87     */

88    public void testSameSubsystem() throws Throwable JavaDoc
89    {
90       MBeanServer JavaDoc mbeanServer = MBeanServerFactory.createMBeanServer();
91
92       String JavaDoc locator1 = "socket://localhost:5703";
93
94       int id1 = 1;
95       int id2 = 2;
96
97       Connector connector1 = setupServer(locator1, mbeanServer);
98       SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1);
99       ServerInvocationHandler previous = connector1.addInvocationHandler("sub1", invocationHandler1);
100
101       assertNull(previous);
102
103       SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2);
104       previous = connector1.addInvocationHandler("sub1", invocationHandler2);
105
106       assertEquals(invocationHandler1, previous);
107
108       ServerInvocationHandler[] handlers1 = connector1.getInvocationHandlers();
109
110       assertEquals(1, handlers1.length);
111
112       connector1.stop();
113       connector1.destroy();
114
115    }
116
117    /**
118     * If multiple handlers added to connector (thus server invoker) and subsystem is NOT
119     * specified in client, then will be processed by last handler added.
120     *
121     * @throws Throwable
122     */

123    public void testNoSubsystem() throws Throwable JavaDoc
124    {
125       MBeanServer JavaDoc mbeanServer = MBeanServerFactory.createMBeanServer();
126
127       String JavaDoc locator1 = "socket://localhost:5704";
128       String JavaDoc locator2 = "socket://localhost:5705";
129
130       int id1 = 1;
131       int id2 = 2;
132       int id3 = 3;
133       int id4 = 4;
134
135       Connector connector1 = setupServer(locator1, mbeanServer);
136       SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1);
137       connector1.addInvocationHandler("sub1", invocationHandler1);
138
139       SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2);
140       connector1.addInvocationHandler("sub2", invocationHandler2);
141       SampleInvocationHandler invocationHandler3 = new SampleInvocationHandler(id3);
142       connector1.addInvocationHandler("sub3", invocationHandler3);
143
144       Connector connector2 = setupServer(locator2, mbeanServer);
145       SampleInvocationHandler invocationHandler4 = new SampleInvocationHandler(id4);
146       connector2.addInvocationHandler("sub4", invocationHandler4);
147
148       Client client = new Client(new InvokerLocator(locator1));
149       Client client2 = new Client(new InvokerLocator(locator2));
150
151       Object JavaDoc ret1 = client.invoke("Do something");
152       Object JavaDoc ret2 = client2.invoke("Do something");
153
154       // should always use the last handler added
155
assertEquals("" + id3, ret1);
156       assertEquals("" + id4, ret2);
157
158       connector1.stop();
159       connector1.destroy();
160       connector2.stop();
161       connector2.destroy();
162
163    }
164
165    public Connector setupServer(String JavaDoc locatorURI, MBeanServer JavaDoc mbeanServer) throws Exception JavaDoc
166    {
167       InvokerLocator locator = new InvokerLocator(locatorURI);
168       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
169       Connector connector = new Connector();
170       connector.setInvokerLocator(locator.getLocatorURI());
171       int randomInt = idCounter++;
172       ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol() + ",id=" + randomInt);
173       mbeanServer.registerMBean(connector, obj);
174       connector.start();
175
176       return connector;
177    }
178
179    /**
180     * Simple invocation handler implementation.
181     */

182    public static class SampleInvocationHandler implements ServerInvocationHandler
183    {
184       private int id = 0;
185
186       public SampleInvocationHandler(int id)
187       {
188          this.id = id;
189       }
190
191       public int getId()
192       {
193          return id;
194       }
195
196       /**
197        * called to handle a specific invocation
198        *
199        * @param invocation
200        * @return
201        * @throws Throwable
202        */

203       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
204       {
205          // Print out the invocation request
206
System.out.println("Invocation request is: " + invocation.getParameter());
207
208          // Just going to return static string as this is just simple example code.
209
return "" + id;
210       }
211
212       /**
213        * Adds a callback handler that will listen for callbacks from
214        * the server invoker handler.
215        *
216        * @param callbackHandler
217        */

218       public void addListener(InvokerCallbackHandler callbackHandler)
219       {
220          // NO OP as do not handling callback listeners in this example
221
}
222
223       /**
224        * Removes the callback handler that was listening for callbacks
225        * from the server invoker handler.
226        *
227        * @param callbackHandler
228        */

229       public void removeListener(InvokerCallbackHandler callbackHandler)
230       {
231          // NO OP as do not handling callback listeners in this example
232
}
233
234       /**
235        * set the mbean server that the handler can reference
236        *
237        * @param server
238        */

239       public void setMBeanServer(MBeanServer JavaDoc server)
240       {
241          // NO OP as do not need reference to MBeanServer for this handler
242
}
243
244       /**
245        * set the invoker that owns this handler
246        *
247        * @param invoker
248        */

249       public void setInvoker(ServerInvoker invoker)
250       {
251          // NO OP as do not need reference back to the server invoker
252
}
253
254    }
255
256
257 }
Popular Tags