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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/asynch/AsynchCallbackTestServerRoot.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
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
package org.jboss.test.remoting.callback.asynch;
 
23
 
 
24
import java.lang.reflect.Field;
 
25
import java.net.InetAddress;
 
26
import java.util.HashMap;
 
27
import java.util.Map;
 
28
 
 
29
import javax.management.MBeanServer;
 
30
 
 
31
import org.jboss.jrunit.extensions.ServerTestCase;
 
32
import org.jboss.logging.Logger;
 
33
import org.jboss.remoting.Client;
 
34
import org.jboss.remoting.InvocationRequest;
 
35
import org.jboss.remoting.InvokerLocator;
 
36
import org.jboss.remoting.ServerInvocationHandler;
 
37
import org.jboss.remoting.ServerInvoker;
 
38
import org.jboss.remoting.callback.AsynchInvokerCallbackHandler;
 
39
import org.jboss.remoting.callback.Callback;
 
40
import org.jboss.remoting.callback.HandleCallbackException;
 
41
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
42
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
 
43
import org.jboss.remoting.transport.Connector;
 
44
import org.jboss.util.threadpool.BasicThreadPool;
 
45
 
 
46
/** 
 
47
 * AsynchCallbackTestClientRoot and AsynchCallbackTestServerRoot are the parent classes
 
48
 * for a set of transport specific tests of the asynchronous callback facility.
 
49
 * 
 
50
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
51
 * @version $Revision: 2103 $
 
52
 * <p>
 
53
 * Copyright Nov 25, 2006
 
54
 * </p>
 
55
 */
 
56
public abstract class AsynchCallbackTestServerRoot extends ServerTestCase
 
57
{
 
58
   public static int port = 5413;
 
59
   
 
60
   public static String SYNCHRONOUS_TEST = "synchronousTest";
 
61
   public static String ASYNCHRONOUS_SERVER_SIDE_TEST = "asynchronousServerSideTest";
 
62
   public static String ASYNCHRONOUS_CLIENT_SIDE_TEST = "asynchronousClientSideTest";
 
63
   public static String GET_STATUS = "getStatus";
 
64
   public static String RESET = "reset";
 
65
   public static String GET_THREAD_COUNT = "getThreadCount";
 
66
   public static String GET_QUEUE_SIZE = "getQueueSize";
 
67
   public static String THREAD_COUNT = "17";
 
68
   public static String QUEUE_SIZE = "19";
 
69
   
 
70
   private static Logger log = Logger.getLogger(AsynchCallbackTestServerRoot.class);
 
71
   
 
72
   // remoting server connector
 
73
   static private Connector connector;
 
74
   String serverLocatorURI;
 
75
 
 
76
   
 
77
   /**
 
78
    * Sets up target remoting server.
 
79
    */
 
80
   public void setUp() throws Exception
 
81
   {
 
82
      log.info("entering setUp()");
 
83
      String locatorURI =  getTransport() + "://" + InetAddress.getLocalHost().getHostAddress() + ":" + port; 
 
84
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
85
      System.out.println("Starting remoting server with locator uri of: " + locatorURI);
 
86
      Map config = new HashMap();
 
87
      config.put(Client.MAX_NUM_ONEWAY_THREADS, THREAD_COUNT);
 
88
      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, QUEUE_SIZE);
 
89
      addTransportSpecificConfig(config);
 
90
      connector = new Connector(serverLocator, config);
 
91
      connector.create();
 
92
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
93
      connector.addInvocationHandler("sample", invocationHandler);
 
94
      connector.start();
 
95
   }
 
96
 
 
97
   
 
98
   /**
 
99
    * Shuts down the server
 
100
    */
 
101
   public void tearDown()
 
102
   {
 
103
      connector.stop();
 
104
      connector.destroy();
 
105
   }
 
106
   
 
107
   
 
108
   protected abstract String getTransport();
 
109
   
 
110
   
 
111
   protected void addTransportSpecificConfig(Map config)
 
112
   {
 
113
   }
 
114
 
 
115
   /**
 
116
    * Simple invocation handler implementation.  When callback client's are registered, will
 
117
    * generate callbacks periodically.
 
118
    */
 
119
   static class SampleInvocationHandler implements ServerInvocationHandler
 
120
   {
 
121
      boolean done;
 
122
      AsynchInvokerCallbackHandler callbackHandler;
 
123
      
 
124
      public void addListener(InvokerCallbackHandler callbackHandler)
 
125
      {
 
126
         System.out.println("Adding callback listener.");
 
127
         assertTrue(callbackHandler instanceof AsynchInvokerCallbackHandler);
 
128
         this.callbackHandler = (AsynchInvokerCallbackHandler) callbackHandler;
 
129
      }
 
130
 
 
131
      public Object invoke(InvocationRequest invocation) throws Throwable
 
132
      {
 
133
         try
 
134
         {
 
135
            String test = (String) invocation.getParameter();
 
136
            if (test.equals(SYNCHRONOUS_TEST))
 
137
            {
 
138
               log.info("making synchronous callback");
 
139
               callbackHandler.handleCallback(new Callback(test));
 
140
               log.info("made synchronous callback");
 
141
            }
 
142
            else if (test.equals(ASYNCHRONOUS_SERVER_SIDE_TEST))
 
143
            {
 
144
               log.info("making asynchronous callback - server side");
 
145
               callbackHandler.handleCallbackOneway(new Callback("callback"), true);
 
146
            }
 
147
            else if (test.equals(ASYNCHRONOUS_CLIENT_SIDE_TEST))
 
148
            {
 
149
               log.info("making asynchronous callback - client side");
 
150
               callbackHandler.handleCallbackOneway(new Callback("callback"));
 
151
            }
 
152
            else if (test.equals(GET_STATUS))
 
153
            {
 
154
               synchronized (this)
 
155
               {
 
156
                  log.info("returning status: " + done);
 
157
                  return new Boolean(done);
 
158
               }
 
159
            }
 
160
            else if (test.equals(RESET))
 
161
            {
 
162
               synchronized (this)
 
163
               {
 
164
                  done = false;
 
165
               } 
 
166
            }
 
167
            else if (test.equals(GET_THREAD_COUNT))
 
168
            {
 
169
               ServerInvokerCallbackHandler sich = (ServerInvokerCallbackHandler) callbackHandler;
 
170
               Client callbackClient = sich.getCallbackClient();
 
171
               Field field = Client.class.getDeclaredField("onewayThreadPool");
 
172
               field.setAccessible(true);
 
173
               BasicThreadPool threadPool = (BasicThreadPool) field.get(callbackClient);
 
174
               int size = threadPool.getMaximumPoolSize();
 
175
               return Integer.toString(size);
 
176
            }
 
177
            else if (test.equals(GET_QUEUE_SIZE))
 
178
            {
 
179
               ServerInvokerCallbackHandler sich = (ServerInvokerCallbackHandler) callbackHandler;
 
180
               Client callbackClient = sich.getCallbackClient();
 
181
               Field field = Client.class.getDeclaredField("onewayThreadPool");
 
182
               field.setAccessible(true);
 
183
               BasicThreadPool threadPool = (BasicThreadPool) field.get(callbackClient);
 
184
               int size = threadPool.getMaximumQueueSize();
 
185
               return Integer.toString(size);
 
186
            }
 
187
            else
 
188
            {
 
189
               log.error("unrecognized test: " + test);
 
190
            }
 
191
         }
 
192
         catch (HandleCallbackException e)
 
193
         {
 
194
            log.error("Unable to send callback");
 
195
         }
 
196
         synchronized (this)
 
197
         {
 
198
            done = true;
 
199
            log.info("done");
 
200
         }
 
201
         return null;
 
202
      }
 
203
      
 
204
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
205
      public void setMBeanServer(MBeanServer server) {}
 
206
      public void setInvoker(ServerInvoker invoker) {}
 
207
   }
 
208
}