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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/http/connection/socketfactory/by_instance/SocketFactoryTestServer.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.transport.http.connection.socketfactory.by_instance;
 
23
 
 
24
import org.jboss.jrunit.extensions.ServerTestCase;
 
25
import org.jboss.remoting.InvocationRequest;
 
26
import org.jboss.remoting.InvokerLocator;
 
27
import org.jboss.remoting.ServerInvocationHandler;
 
28
import org.jboss.remoting.ServerInvoker;
 
29
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
30
import org.jboss.remoting.security.ServerSocketFactoryMBean;
 
31
import org.jboss.remoting.security.SocketFactoryMBean;
 
32
import org.jboss.remoting.transport.Connector;
 
33
 
 
34
import javax.management.MBeanServer;
 
35
import javax.net.ServerSocketFactory;
 
36
import javax.net.SocketFactory;
 
37
import java.io.IOException;
 
38
import java.io.Serializable;
 
39
import java.net.InetAddress;
 
40
import java.net.ServerSocket;
 
41
import java.net.Socket;
 
42
import java.net.UnknownHostException;
 
43
 
 
44
/**
 
45
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
46
 */
 
47
public class SocketFactoryTestServer extends ServerTestCase
 
48
{
 
49
   // Default locator values
 
50
   private static String transport = "http";
 
51
   private static String host = "localhost";
 
52
   private static int port = 5400;
 
53
 
 
54
   private static int callCounter = 0;
 
55
 
 
56
   private Connector connector = null;
 
57
 
 
58
   public void tearDown()
 
59
   {
 
60
      if(connector != null)
 
61
      {
 
62
         connector.stop();
 
63
         connector.destroy();
 
64
      }
 
65
   }
 
66
 
 
67
   public void setUp() throws Exception
 
68
   {
 
69
      setupServerWithInstance();
 
70
   }
 
71
 
 
72
   private void setupServerWithInstance() throws Exception
 
73
   {
 
74
      String locatorURI = transport + "://" + host + ":" + port;
 
75
 
 
76
      // create the InvokerLocator based on url string format
 
77
      // to indicate the transport, host, and port to use for the
 
78
      // server invoker.
 
79
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
80
      System.out.println("Starting remoting server with locator uri of: " + locatorURI);
 
81
      connector = new Connector(locator);
 
82
 
 
83
      connector.setServerSocketFactory(ServerSocketFactory.getDefault());
 
84
 
 
85
      // creates all the connector's needed resources, such as the server invoker
 
86
      connector.create();
 
87
 
 
88
      // create the handler to receive the invocation request from the client for processing
 
89
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
90
      // first parameter is sub-system name.  can be any String value.
 
91
      connector.addInvocationHandler("sample", invocationHandler);
 
92
 
 
93
      // start with a new non daemon thread so
 
94
      // server will wait for request and not exit
 
95
      connector.start();
 
96
 
 
97
   }
 
98
 
 
99
   /**
 
100
    * Can pass transport and port to be used as parameters.
 
101
    * Valid transports are 'rmi' and 'socket'.
 
102
    *
 
103
    * @param args
 
104
    */
 
105
   public static void main(String[] args)
 
106
   {
 
107
      if(args != null && args.length == 3)
 
108
      {
 
109
         transport = args[0];
 
110
         host = args[1];
 
111
         port = Integer.parseInt(args[2]);
 
112
      }
 
113
      SocketFactoryTestServer server = new SocketFactoryTestServer();
 
114
      try
 
115
      {
 
116
         server.setUp();
 
117
 
 
118
         // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
 
119
         while(true)
 
120
         {
 
121
            Thread.sleep(1000);
 
122
         }
 
123
 
 
124
      }
 
125
      catch(Exception e)
 
126
      {
 
127
         e.printStackTrace();
 
128
      }
 
129
   }
 
130
 
 
131
   /**
 
132
    * Simple invocation handler implementation.
 
133
    * This is the code that will be called with the invocation payload from the client.
 
134
    */
 
135
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
136
   {
 
137
      /**
 
138
       * called to handle a specific invocation
 
139
       *
 
140
       * @param invocation
 
141
       * @return
 
142
       * @throws Throwable
 
143
       */
 
144
      public Object invoke(InvocationRequest invocation) throws Throwable
 
145
      {
 
146
         // Print out the invocation request
 
147
         System.out.println("Invocation request is: " + invocation.getParameter());
 
148
         Integer resp = new Integer(++callCounter);
 
149
         System.out.println("Returning response of: " + resp);
 
150
         // Just going to return static string as this is just simple example code.
 
151
         return resp;
 
152
      }
 
153
 
 
154
      /**
 
155
       * Adds a callback handler that will listen for callbacks from
 
156
       * the server invoker handler.
 
157
       *
 
158
       * @param callbackHandler
 
159
       */
 
160
      public void addListener(InvokerCallbackHandler callbackHandler)
 
161
      {
 
162
         // NO OP as do not handling callback listeners in this example
 
163
      }
 
164
 
 
165
      /**
 
166
       * Removes the callback handler that was listening for callbacks
 
167
       * from the server invoker handler.
 
168
       *
 
169
       * @param callbackHandler
 
170
       */
 
171
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
172
      {
 
173
         // NO OP as do not handling callback listeners in this example
 
174
      }
 
175
 
 
176
      /**
 
177
       * set the mbean server that the handler can reference
 
178
       *
 
179
       * @param server
 
180
       */
 
181
      public void setMBeanServer(MBeanServer server)
 
182
      {
 
183
         // NO OP as do not need reference to MBeanServer for this handler
 
184
      }
 
185
 
 
186
      /**
 
187
       * set the invoker that owns this handler
 
188
       *
 
189
       * @param invoker
 
190
       */
 
191
      public void setInvoker(ServerInvoker invoker)
 
192
      {
 
193
         // NO OP as do not need reference back to the server invoker
 
194
      }
 
195
 
 
196
   }
 
197
 
 
198
   public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
 
199
   {
 
200
      private ServerSocketFactory factory = null;
 
201
 
 
202
      public ServerSocketFactoryMock()
 
203
      {
 
204
         factory = ServerSocketFactory.getDefault();
 
205
      }
 
206
 
 
207
      public ServerSocket createServerSocket() throws IOException
 
208
      {
 
209
         return factory.createServerSocket();
 
210
      }
 
211
 
 
212
      public ServerSocket createServerSocket(int i) throws IOException
 
213
      {
 
214
         return factory.createServerSocket(i);
 
215
      }
 
216
 
 
217
      public ServerSocket createServerSocket(int i, int i1) throws IOException
 
218
      {
 
219
         return factory.createServerSocket(i, i1);
 
220
      }
 
221
 
 
222
      public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
 
223
      {
 
224
         return factory.createServerSocket(i, i1, inetAddress);
 
225
      }
 
226
   }
 
227
 
 
228
   public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
 
229
   {
 
230
 
 
231
   }
 
232
 
 
233
   public static class SocketFactoryMock extends SocketFactory implements SocketFactoryMockMBean, Serializable
 
234
   {
 
235
      private SocketFactory factory = null;
 
236
 
 
237
      public SocketFactoryMock()
 
238
      {
 
239
      }
 
240
 
 
241
      private void init()
 
242
      {
 
243
         System.out.println("SocketFactoryMock - init() called");
 
244
         this.factory = SocketFactory.getDefault();
 
245
      }
 
246
 
 
247
      public Socket createSocket(String host, int port) throws IOException, UnknownHostException
 
248
      {
 
249
         if(factory == null)
 
250
         {
 
251
            init();
 
252
         }
 
253
         return factory.createSocket(host, port);
 
254
      }
 
255
 
 
256
      public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
 
257
      {
 
258
         if(factory == null)
 
259
         {
 
260
            init();
 
261
         }
 
262
         return factory.createSocket(host, port, localHost, localPort);
 
263
      }
 
264
 
 
265
      public Socket createSocket(InetAddress host, int port) throws IOException
 
266
      {
 
267
         if(factory == null)
 
268
         {
 
269
            init();
 
270
         }
 
271
         return factory.createSocket(host, port);
 
272
      }
 
273
 
 
274
      public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
 
275
      {
 
276
         if(factory == null)
 
277
         {
 
278
            init();
 
279
         }
 
280
         return factory.createSocket(address, port, localAddress, localPort);
 
281
      }
 
282
   }
 
283
 
 
284
   public interface SocketFactoryMockMBean extends SocketFactoryMBean
 
285
   {
 
286
 
 
287
   }
 
288
 
 
289
}