~ubuntu-branches/ubuntu/wily/libjboss-remoting-java/wily

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/multiple/CallbackTestServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.test.remoting.callback.multiple;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.List;
 
6
 
 
7
import javax.management.MBeanServer;
 
8
 
 
9
import org.jboss.jrunit.extensions.ServerTestCase;
 
10
import org.jboss.remoting.InvocationRequest;
 
11
import org.jboss.remoting.InvokerLocator;
 
12
import org.jboss.remoting.ServerInvocationHandler;
 
13
import org.jboss.remoting.ServerInvoker;
 
14
import org.jboss.remoting.callback.Callback;
 
15
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
16
import org.jboss.remoting.transport.Connector;
 
17
 
 
18
/**
 
19
 * Simple remoting server.  Uses inner class SampleInvocationHandler
 
20
 * as the invocation target handler class.
 
21
 *
 
22
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
23
 */
 
24
public class CallbackTestServer extends ServerTestCase
 
25
{
 
26
   // Default locator values
 
27
   private static String transport = "socket";
 
28
   private static String host = "localhost";
 
29
   private static int port = 5530;
 
30
 
 
31
   private String locatorURI = transport + "://" + host + ":" + port;
 
32
   private Connector connector;
 
33
 
 
34
   // String to be returned from invocation handler upon client invocation calls.
 
35
   public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
 
36
   public static final String CALLBACK_VALUE = "This is the payload of callback invocation.";
 
37
 
 
38
 
 
39
   public void setupServer() throws Exception
 
40
   {
 
41
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
42
      System.out.println("Starting remoting server with locator uri of: " + locatorURI);
 
43
      connector = new Connector();
 
44
      connector.setInvokerLocator(locator.getLocatorURI());
 
45
      connector.start();
 
46
 
 
47
      CallbackTestServer.SampleInvocationHandler invocationHandler = new CallbackTestServer.SampleInvocationHandler();
 
48
      // first parameter is sub-system name.  can be any String value.
 
49
      connector.addInvocationHandler("sample", invocationHandler);
 
50
   }
 
51
 
 
52
   protected void setUp() throws Exception
 
53
   {
 
54
      setupServer();
 
55
   }
 
56
 
 
57
   protected void tearDown() throws Exception
 
58
   {
 
59
      if(connector != null)
 
60
      {
 
61
         connector.stop();
 
62
         connector.destroy();
 
63
      }
 
64
   }
 
65
 
 
66
   public static void main(String[] args)
 
67
   {
 
68
      CallbackTestServer server = new CallbackTestServer();
 
69
      try
 
70
      {
 
71
         server.setUp();
 
72
 
 
73
         Thread.sleep(600000);
 
74
      }
 
75
      catch (Exception e)
 
76
      {
 
77
         e.printStackTrace();
 
78
      }
 
79
   }
 
80
 
 
81
   /**
 
82
    * Simple invocation handler implementation.
 
83
    */
 
84
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
85
   {
 
86
 
 
87
      private List listeners = new ArrayList();
 
88
 
 
89
 
 
90
      /**
 
91
       * called to handle a specific invocation
 
92
       *
 
93
       * @param invocation
 
94
       * @return
 
95
       * @throws Throwable
 
96
       */
 
97
      public Object invoke(InvocationRequest invocation) throws Throwable
 
98
      {
 
99
         // Just going to return static string as this is just simple example code.
 
100
 
 
101
         // Will also fire callback to listeners if they were to exist using
 
102
         // simple invocation request.
 
103
         Callback callback = new Callback(CALLBACK_VALUE);
 
104
         Iterator itr = listeners.iterator();
 
105
         while(itr.hasNext())
 
106
         {
 
107
            InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
 
108
            callbackHandler.handleCallback(callback);
 
109
         }
 
110
 
 
111
         return RESPONSE_VALUE;
 
112
 
 
113
      }
 
114
 
 
115
      /**
 
116
       * Adds a callback handler that will listen for callbacks from
 
117
       * the server invoker handler.
 
118
       *
 
119
       * @param callbackHandler
 
120
       */
 
121
      public void addListener(InvokerCallbackHandler callbackHandler)
 
122
      {
 
123
         listeners.add(callbackHandler);
 
124
      }
 
125
 
 
126
      /**
 
127
       * Removes the callback handler that was listening for callbacks
 
128
       * from the server invoker handler.
 
129
       *
 
130
       * @param callbackHandler
 
131
       */
 
132
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
133
      {
 
134
         listeners.remove(callbackHandler);
 
135
      }
 
136
 
 
137
      /**
 
138
       * set the mbean server that the handler can reference
 
139
       *
 
140
       * @param server
 
141
       */
 
142
      public void setMBeanServer(MBeanServer server)
 
143
      {
 
144
         // NO OP as do not need reference to MBeanServer for this handler
 
145
      }
 
146
 
 
147
      /**
 
148
       * set the invoker that owns this handler
 
149
       *
 
150
       * @param invoker
 
151
       */
 
152
      public void setInvoker(ServerInvoker invoker)
 
153
      {
 
154
         // NO OP as do not need reference back to the server invoker
 
155
      }
 
156
 
 
157
   }
 
158
}