KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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