KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > local > LocalInvocationTestCase


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.transport.local;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.remoting.Client;
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.Callback;
19 import org.jboss.remoting.callback.InvokerCallbackHandler;
20 import org.jboss.remoting.transport.Connector;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Just a simple example of how to setup remoting to make an invocation to local target,
26  * so are not actually going out of process, thus not really using any transport protocol.
27  *
28  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
29  */

30 public class LocalInvocationTestCase extends TestCase
31 {
32    private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
33
34    public LocalInvocationTestCase(String JavaDoc name)
35    {
36       super(name);
37    }
38
39    public static void setupConfiguration(InvokerLocator locator, ServerInvocationHandler invocationHandler) throws Exception JavaDoc
40    {
41       Connector connector = new Connector();
42       connector.setInvokerLocator(locator.getLocatorURI());
43       connector.start();
44       connector.addInvocationHandler("mock", invocationHandler);
45    }
46
47    public void testInvocation() throws Throwable JavaDoc
48    {
49       InvokerLocator locator = new InvokerLocator("socket://localhost:6789");
50       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
51
52       // set up
53
LocalInvocationTestCase.setupConfiguration(locator, invocationHandler);
54
55       // This could have been new Client(locator), but want to show that subsystem param is null
56
Client remotingClient = new Client(locator, null);
57       Object JavaDoc response = remotingClient.invoke("Do something", null);
58
59       System.out.println("Invocation response: " + response);
60       assertEquals(response, RESPONSE_VALUE);
61    }
62
63    public static class SampleInvocationHandler implements ServerInvocationHandler
64    {
65       private List JavaDoc listeners = new ArrayList JavaDoc();
66
67       /**
68        * set the mbean server that the handler can reference
69        *
70        * @param server
71        */

72       public void setMBeanServer(MBeanServer JavaDoc server)
73       {
74          // NO OP as do not need reference to MBeanServer for this handler
75
}
76
77       /**
78        * set the invoker that owns this handler
79        *
80        * @param invoker
81        */

82       public void setInvoker(ServerInvoker invoker)
83       {
84          // NO OP as do not need reference back to the server invoker
85
}
86
87       /**
88        * called to handle a specific invocation
89        *
90        * @param invocation
91        * @return
92        * @throws Throwable
93        */

94       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
95       {
96          // Just going to return static string as this is just simple example code.
97

98          // Will also fire callback to listeners if they were to exist using
99
// simple invocation request.
100
Callback callbackInvocationRequest = new Callback("This is the payload of callback invocation");
101          Iterator JavaDoc itr = listeners.iterator();
102          while(itr.hasNext())
103          {
104             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
105             callbackHandler.handleCallback(callbackInvocationRequest);
106          }
107
108          return RESPONSE_VALUE;
109
110       }
111
112       /**
113        * Adds a callback handler that will listen for callbacks from
114        * the server invoker handler.
115        *
116        * @param callbackHandler
117        */

118       public void addListener(InvokerCallbackHandler callbackHandler)
119       {
120          listeners.add(callbackHandler);
121       }
122
123       /**
124        * Removes the callback handler that was listening for callbacks
125        * from the server invoker handler.
126        *
127        * @param callbackHandler
128        */

129       public void removeListener(InvokerCallbackHandler callbackHandler)
130       {
131          listeners.remove(callbackHandler);
132       }
133    }
134 }
Popular Tags