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