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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/shutdown/ShutdownTestServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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.shutdown;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
 
 
30
import org.apache.log4j.Logger;
 
31
import org.jboss.jrunit.extensions.ServerTestCase;
 
32
import org.jboss.remoting.Client;
 
33
import org.jboss.remoting.ConnectionListener;
 
34
import org.jboss.remoting.InvocationRequest;
 
35
import org.jboss.remoting.InvokerLocator;
 
36
import org.jboss.remoting.ServerInvocationHandler;
 
37
import org.jboss.remoting.ServerInvoker;
 
38
import org.jboss.remoting.callback.Callback;
 
39
import org.jboss.remoting.callback.HandleCallbackException;
 
40
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
41
import org.jboss.remoting.transport.Connector;
 
42
 
 
43
/** 
 
44
 * This is the server half of a unit test designed to verify that a Remoting application
 
45
 * will shut down without any stray threads hanging it up.  To exercise as many
 
46
 * Remoting threads as possible, the server enables leasing and registers a
 
47
 * connection listener.
 
48
 * 
 
49
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
50
 * @version $Revision: 3011 $
 
51
 * <p>
 
52
 * Copyright Jan 19, 2007
 
53
 * </p>
 
54
 */
 
55
public class ShutdownTestServer extends ServerTestCase
 
56
{
 
57
   public static int port = 9876;
 
58
   private static Logger log = Logger.getLogger(ShutdownTestServer.class);
 
59
   private Connector connector;
 
60
   private String transport;
 
61
   private Map extraConfig;
 
62
   
 
63
   public ShutdownTestServer(String transport, Map config)
 
64
   {
 
65
      this.transport = transport;
 
66
      this.extraConfig = config;
 
67
   }
 
68
   
 
69
   
 
70
   public void setUp() throws Exception
 
71
   {
 
72
      String host = InetAddress.getLocalHost().getHostAddress();
 
73
      String portString = System.getProperty("port");
 
74
      log.info("portString: " + portString);
 
75
      int port = Integer.parseInt(portString);
 
76
      String locatorURI = transport + "://" + host + ":" + port;
 
77
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
78
      HashMap serverConfig = new HashMap(extraConfig);
 
79
      log.info("serverConfig: " + serverConfig);
 
80
      connector = new Connector(locator, serverConfig);
 
81
      connector.create();
 
82
      connector.setLeasePeriod(2000);
 
83
      connector.addInvocationHandler("test", new TestHandler());
 
84
      connector.addConnectionListener(new TestListener());
 
85
      connector.start();
 
86
      log.info("server started at: " + locatorURI);
 
87
      log.info("READY");
 
88
   }
 
89
   
 
90
   
 
91
   public void tearDown()
 
92
   {
 
93
      if (connector != null)
 
94
      {
 
95
         connector.stop();
 
96
         log.info("server shut down");
 
97
      }
 
98
   }
 
99
   
 
100
   
 
101
   public static void main(String[] args)
 
102
   {
 
103
      if (args.length == 0)
 
104
         throw new RuntimeException();
 
105
      
 
106
      HashMap config = new HashMap();
 
107
      System.out.println("server args.length: " + args.length);
 
108
      if (args.length > 1)
 
109
         getConfig(config, args[1]);
 
110
      
 
111
      String transport = args[0];
 
112
      ShutdownTestServer server = new ShutdownTestServer(transport, config);
 
113
      try
 
114
      {
 
115
         server.setUp();
 
116
         log.info("server back from setUp()");
 
117
         Thread.sleep(25000);
 
118
         log.info("server calling tearDown()");
 
119
         server.tearDown();
 
120
         log.info("server back from tearDown()");
 
121
      }
 
122
      catch(Exception e)
 
123
      {
 
124
         e.printStackTrace();
 
125
      }
 
126
   }
 
127
 
 
128
   
 
129
   protected static void getConfig(Map config, String configs)
 
130
   {
 
131
      int start = 0;
 
132
      int ampersand = configs.indexOf('&');
 
133
      while (ampersand > 0)
 
134
      {
 
135
         String s = configs.substring(start, ampersand);
 
136
         int equals = s.indexOf('=');
 
137
         String param = s.substring(0, equals);
 
138
         String value = s.substring(equals + 1);
 
139
         config.put(param, value);
 
140
         start = ampersand + 1;
 
141
         ampersand = configs.indexOf('&', start);
 
142
      }
 
143
      log.info("config: " + config);
 
144
      log.info("configs: " + configs);
 
145
   }
 
146
   
 
147
   
 
148
   public class TestHandler implements ServerInvocationHandler
 
149
   {
 
150
      public void setMBeanServer(MBeanServer server) {}
 
151
      public void setInvoker(ServerInvoker invoker) {}
 
152
 
 
153
      public Object invoke(InvocationRequest invocation) throws Throwable
 
154
      {
 
155
         Integer i = (Integer) invocation.getParameter();
 
156
         return new Integer(i.intValue() + 1);
 
157
      }
 
158
 
 
159
      public void addListener(InvokerCallbackHandler callbackHandler)
 
160
      {
 
161
         try
 
162
         {
 
163
            log.info("sending callback");
 
164
            callbackHandler.handleCallback(new Callback("callback"));
 
165
            log.info("sent callback");
 
166
         }
 
167
         catch (HandleCallbackException e)
 
168
         {
 
169
            log.info("error handling callback");
 
170
            e.printStackTrace();
 
171
         }
 
172
      }
 
173
      
 
174
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
175
   }
 
176
   
 
177
   
 
178
   public static class TestListener implements ConnectionListener
 
179
   {
 
180
      public void handleConnectionException(Throwable throwable, Client client)
 
181
      {
 
182
         log.info("got connection exception: " + throwable);
 
183
      }
 
184
   }
 
185
}