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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/socketpool/SocketPoolTestServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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.transport.socket.socketpool;
 
23
 
 
24
import org.jboss.jrunit.extensions.ServerTestCase;
 
25
import org.jboss.logging.Logger;
 
26
import org.jboss.remoting.InvocationRequest;
 
27
import org.jboss.remoting.InvokerLocator;
 
28
import org.jboss.remoting.ServerInvocationHandler;
 
29
import org.jboss.remoting.ServerInvoker;
 
30
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
31
import org.jboss.remoting.transport.Connector;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
 
 
35
/** 
 
36
 * See SocketPoolTestCase for description.
 
37
 *  
 
38
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
39
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
40
 * 
 
41
 * @version $Revision: 2911 $
 
42
 * <p>
 
43
 * Copyright Nov 2, 2006
 
44
 * </p>
 
45
 */
 
46
public class SocketPoolTestServer extends ServerTestCase
 
47
{
 
48
   public final static int NUMBER_OF_CALLS = 5;
 
49
   private static Logger log = Logger.getLogger(SocketPoolTestServer.class);
 
50
   private static Object lock = new Object();
 
51
   private static Object stopLock = new Object();
 
52
   private static int callCounter;
 
53
   private static boolean done;
 
54
   
 
55
   // Default locator values
 
56
   private static String transport = "socket";
 
57
   private static String host = "localhost";
 
58
   private static int port = 6413;
 
59
 
 
60
   private Connector connector = null;
 
61
 
 
62
   public void setupServer(String locatorURI) throws Exception
 
63
   {
 
64
      log.warn("EXCEPTIONS ARE EXPECTED");
 
65
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
66
      
 
67
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
68
      connector = new Connector(locator);
 
69
      connector.create();
 
70
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
71
      connector.addInvocationHandler("sample", invocationHandler);
 
72
      connector.start();
 
73
      
 
74
      // This thread will stop the Connector when NUMBER_OF_CALLS
 
75
      // invocations have been received.
 
76
      new Thread()
 
77
      {
 
78
         public void run()
 
79
         {
 
80
            synchronized (lock)
 
81
            {
 
82
               while (!done)
 
83
               {
 
84
                  try
 
85
                  {
 
86
                     lock.wait();
 
87
                  }
 
88
                  catch (InterruptedException e)
 
89
                  {
 
90
                     log.error(e);
 
91
                     e.printStackTrace();
 
92
                  }
 
93
               }
 
94
               
 
95
               if(connector != null)
 
96
               {
 
97
                  connector.stop();
 
98
                  connector.destroy();
 
99
                  log.info("Connector stopped");
 
100
                  
 
101
                  synchronized(stopLock)
 
102
                  {
 
103
                     stopLock.notify();
 
104
                  }
 
105
               }
 
106
            }
 
107
         }
 
108
      }.start();   
 
109
   }
 
110
 
 
111
   public void tearDown()
 
112
   {
 
113
   }
 
114
 
 
115
   public void setUp() throws Exception
 
116
   {
 
117
      String locatorURI = getTransport() + "://" + host + ":" + getPort();
 
118
      setupServer(locatorURI);
 
119
   }
 
120
 
 
121
   /**
 
122
    * Can pass transport and port to be used as parameters.
 
123
    * Valid transports are 'rmi' and 'socket'.
 
124
    *
 
125
    * @param args
 
126
    */
 
127
   public static void main(String[] args)
 
128
   {
 
129
      if(args != null && args.length == 2)
 
130
      {
 
131
         host = args[0];
 
132
         port = Integer.parseInt(args[1]);
 
133
      }
 
134
      SocketPoolTestServer server = new SocketPoolTestServer();
 
135
      try
 
136
      {
 
137
         server.setUp();
 
138
         
 
139
         synchronized (stopLock)
 
140
         {
 
141
            while (!done)
 
142
            {
 
143
               try
 
144
               {
 
145
                  stopLock.wait();
 
146
                  break;
 
147
               }
 
148
               catch (InterruptedException ignored) {}
 
149
            }
 
150
         }
 
151
      }
 
152
      catch(Exception e)
 
153
      {
 
154
         e.printStackTrace();
 
155
      }
 
156
   }
 
157
 
 
158
   protected String getTransport()
 
159
   {
 
160
      return transport;
 
161
   }
 
162
   
 
163
   protected int getPort()
 
164
   {
 
165
      return port;
 
166
   }
 
167
   
 
168
   /**
 
169
    * Simple invocation handler implementation.
 
170
    * This is the code that will be called with the invocation payload from the client.
 
171
    */
 
172
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
173
   {
 
174
      public Object invoke(InvocationRequest invocation) throws Throwable
 
175
      {
 
176
         synchronized (lock)
 
177
         {
 
178
            ++callCounter;
 
179
            log.info("callCounter: " + callCounter);
 
180
         }
 
181
 
 
182
         // Waiting for 4 seconds will cause the client to timeout.
 
183
         Thread.sleep(4000);
 
184
         
 
185
         synchronized (lock)
 
186
         {
 
187
            if (callCounter == NUMBER_OF_CALLS)
 
188
            {
 
189
               done = true;
 
190
               lock.notify();
 
191
            }
 
192
         }
 
193
         return "response";
 
194
      }
 
195
 
 
196
      public void addListener(InvokerCallbackHandler callbackHandler)
 
197
      {
 
198
      }
 
199
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
200
      {
 
201
      }
 
202
      public void setMBeanServer(MBeanServer server)
 
203
      {
 
204
      }
 
205
      public void setInvoker(ServerInvoker invoker)
 
206
      {
 
207
      }
 
208
   }
 
209
}
 
 
b'\\ No newline at end of file'