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

« back to all changes in this revision

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