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

« back to all changes in this revision

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