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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/multiple/client/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.client;
2
 
 
3
 
import org.jboss.jrunit.extensions.ServerTestCase;
4
 
import org.jboss.remoting.transport.Connector;
5
 
import org.jboss.remoting.InvokerLocator;
6
 
import org.jboss.remoting.ServerInvocationHandler;
7
 
import org.jboss.remoting.InvocationRequest;
8
 
import org.jboss.remoting.ServerInvoker;
9
 
import org.jboss.remoting.callback.Callback;
10
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
11
 
import org.apache.log4j.Level;
12
 
 
13
 
import javax.management.MBeanServer;
14
 
import java.util.List;
15
 
import java.util.ArrayList;
16
 
import java.util.Iterator;
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 = 5500;
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
 
      org.apache.log4j.BasicConfigurator.configure();
55
 
      org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
56
 
      org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
57
 
      org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
58
 
 
59
 
      setupServer();
60
 
   }
61
 
 
62
 
   protected void tearDown() throws Exception
63
 
   {
64
 
      if(connector != null)
65
 
      {
66
 
         connector.stop();
67
 
         connector.destroy();
68
 
      }
69
 
   }
70
 
 
71
 
   public static void main(String[] args)
72
 
   {
73
 
      CallbackTestServer server = new CallbackTestServer();
74
 
      try
75
 
      {
76
 
         server.setUp();
77
 
 
78
 
         Thread.sleep(600000);
79
 
      }
80
 
      catch (Exception e)
81
 
      {
82
 
         e.printStackTrace();
83
 
      }
84
 
   }
85
 
 
86
 
   /**
87
 
    * Simple invocation handler implementation.
88
 
    */
89
 
   public static class SampleInvocationHandler implements ServerInvocationHandler
90
 
   {
91
 
 
92
 
      private List listeners = new ArrayList();
93
 
 
94
 
 
95
 
      /**
96
 
       * called to handle a specific invocation
97
 
       *
98
 
       * @param invocation
99
 
       * @return
100
 
       * @throws Throwable
101
 
       */
102
 
      public Object invoke(InvocationRequest invocation) throws Throwable
103
 
      {
104
 
         // Just going to return static string as this is just simple example code.
105
 
 
106
 
         // Will also fire callback to listeners if they were to exist using
107
 
         // simple invocation request.
108
 
         Callback callback = new Callback(CALLBACK_VALUE);
109
 
         Iterator itr = listeners.iterator();
110
 
         while(itr.hasNext())
111
 
         {
112
 
            InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
113
 
            callbackHandler.handleCallback(callback);
114
 
         }
115
 
 
116
 
         return RESPONSE_VALUE;
117
 
 
118
 
      }
119
 
 
120
 
      /**
121
 
       * Adds a callback handler that will listen for callbacks from
122
 
       * the server invoker handler.
123
 
       *
124
 
       * @param callbackHandler
125
 
       */
126
 
      public void addListener(InvokerCallbackHandler callbackHandler)
127
 
      {
128
 
         listeners.add(callbackHandler);
129
 
      }
130
 
 
131
 
      /**
132
 
       * Removes the callback handler that was listening for callbacks
133
 
       * from the server invoker handler.
134
 
       *
135
 
       * @param callbackHandler
136
 
       */
137
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
138
 
      {
139
 
         listeners.remove(callbackHandler);
140
 
      }
141
 
 
142
 
      /**
143
 
       * set the mbean server that the handler can reference
144
 
       *
145
 
       * @param server
146
 
       */
147
 
      public void setMBeanServer(MBeanServer server)
148
 
      {
149
 
         // NO OP as do not need reference to MBeanServer for this handler
150
 
      }
151
 
 
152
 
      /**
153
 
       * set the invoker that owns this handler
154
 
       *
155
 
       * @param invoker
156
 
       */
157
 
      public void setInvoker(ServerInvoker invoker)
158
 
      {
159
 
         // NO OP as do not need reference back to the server invoker
160
 
      }
161
 
 
162
 
   }
163
 
}