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