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.
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.
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.
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.
22
package org.jboss.test.remoting.multihome;
24
import java.net.InetAddress;
25
import java.net.NetworkInterface;
26
import java.util.HashMap;
27
import java.util.Iterator;
28
import java.util.List;
30
import java.util.Enumeration;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.logging.XLevel;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.Home;
43
import org.jboss.remoting.InvocationRequest;
44
import org.jboss.remoting.InvokerLocator;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.Callback;
48
import org.jboss.remoting.callback.HandleCallbackException;
49
import org.jboss.remoting.callback.InvokerCallbackHandler;
50
import org.jboss.remoting.transport.AddressUtil;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
55
public abstract class MultihomeTestParent extends TestCase
57
protected static Logger log = Logger.getLogger(MultihomeTestParent.class);
59
private static boolean firstTime = true;
61
protected String host;
63
protected String locatorURI;
64
protected InvokerLocator serverLocator;
65
protected Connector connector;
66
protected TestInvocationHandler invocationHandler;
69
public void setUp() throws Exception
74
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
75
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
76
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
77
PatternLayout layout = new PatternLayout(pattern);
78
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
79
Logger.getRootLogger().addAppender(consoleAppender);
84
public void tearDown()
89
public void testConnectToEachHome() throws Throwable
91
log.info("entering " + getName());
96
Iterator it = serverLocator.getConnectHomeList().iterator();
100
Home h = (Home) it.next();
101
String path = serverLocator.getPath();
102
Map parameters = serverLocator.getParameters();
103
InvokerLocator clientLocator = new InvokerLocator(getTransport(), h.host, h.port, path, parameters);
104
log.info("locator: " + clientLocator);
105
HashMap clientConfig = new HashMap();
106
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
107
addExtraClientConfig(clientConfig);
108
Client client = new Client(clientLocator, clientConfig);
110
log.info("client is connected to " + clientLocator);
113
assertEquals("abc", client.invoke("abc"));
114
log.info("invocation successful");
117
Map metadata = new HashMap();
118
addExtraCallbackConfig(metadata);
119
TestCallbackHandler callbackHandler = new TestCallbackHandler();
120
client.addListener(callbackHandler, metadata, null, true);
121
assertTrue(callbackHandler.ok);
122
log.info("callback successful");
123
client.removeListener(callbackHandler);
128
log.info(getName() + " PASSES");
132
public void testConnectToAnyHome() throws Throwable
134
log.info("entering " + getName());
140
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
141
HashMap clientConfig = new HashMap();
142
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
143
addExtraClientConfig(clientConfig);
144
Client client = new Client(clientLocator, clientConfig);
146
log.info("client is connected to " + clientLocator);
149
assertEquals("abc", client.invoke("abc"));
150
log.info("invocation successful");
153
Map metadata = new HashMap();
154
addExtraCallbackConfig(metadata);
155
TestCallbackHandler callbackHandler = new TestCallbackHandler();
156
client.addListener(callbackHandler, metadata, null, true);
157
assertTrue(callbackHandler.ok);
158
log.info("callback successful");
160
client.removeListener(callbackHandler);
163
log.info(getName() + " PASSES");
167
public void testInvocationHandlersAreShared() throws Throwable
169
log.info("entering " + getName());
174
List homes = serverLocator.getConnectHomeList();
175
for (int i = 0; i < homes.size(); i++)
178
Home h = (Home) homes.get(i);
179
String path = serverLocator.getPath();
180
Map parameters = serverLocator.getParameters();
181
InvokerLocator clientLocator = new InvokerLocator(getTransport(), h.host, h.port, path, parameters);
182
log.info("locator: " + clientLocator);
183
HashMap clientConfig = new HashMap();
184
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
185
addExtraClientConfig(clientConfig);
186
Client client = new Client(clientLocator, clientConfig);
188
log.info("client is connected to " + clientLocator);
191
client.invoke("reset");
194
assertEquals("count", client.invoke("count"));
195
log.info("invocation successful");
198
Map metadata = new HashMap();
199
addExtraCallbackConfig(metadata);
200
TestCallbackHandler callbackHandler = new TestCallbackHandler();
201
client.addListener(callbackHandler, metadata, null, true);
202
assertTrue(callbackHandler.ok);
203
log.info("callback successful");
205
// Verify that a single ServerInvocationHandler handles invocation
206
// for all interfaces.
207
Integer counter = (Integer) client.invoke("getCounter");
208
assertEquals(i + 1, counter.intValue());
210
client.removeListener(callbackHandler);
215
log.info(getName() + " PASSES");
219
protected abstract String getTransport();
222
protected String getPath() {return "";}
223
protected void addExtraClientConfig(Map config) {}
224
protected void addExtraServerConfig(Map config) {}
225
protected void addExtraCallbackConfig(Map config) {}
228
protected void setupServer() throws Exception
230
StringBuffer sb = new StringBuffer();
231
Enumeration e1 = NetworkInterface.getNetworkInterfaces();
232
boolean first = true;
235
loop: while (e1.hasMoreElements())
237
NetworkInterface iface = (NetworkInterface) e1.nextElement();
238
Enumeration e2 = iface.getInetAddresses();
239
while (e2.hasMoreElements())
241
InetAddress address = (InetAddress) e2.nextElement();
242
String host = address.getHostAddress();
243
if (host.indexOf(':') != host.lastIndexOf(':'))
244
host = "[" + host + "]";
245
if (AddressUtil.checkAddress(host))
247
log.info("host is functional: " + host);
248
int port = PortUtil.findFreePort(host);
253
sb.append(host).append(':').append(port);
254
if (++counter > 10) break loop;
258
log.info("skipping host: " + host);
263
locatorURI = getTransport() + "://" + InvokerLocator.MULTIHOME + getPath() + "/?";
264
locatorURI += InvokerLocator.HOMES_KEY + "=" + sb.toString() + "&";
265
locatorURI += InvokerLocator.CONNECT_HOMES_KEY + "=" + sb.toString();
266
serverLocator = new InvokerLocator(locatorURI);
267
log.info("Starting remoting server with locator uri of: " + locatorURI);
268
HashMap config = new HashMap();
269
config.put(InvokerLocator.FORCE_REMOTE, "true");
270
addExtraServerConfig(config);
271
connector = new Connector(serverLocator, config);
273
invocationHandler = new TestInvocationHandler();
274
connector.addInvocationHandler("test", invocationHandler);
279
protected void shutdownServer() throws Exception
281
if (connector != null)
286
static class TestCallbackHandler implements InvokerCallbackHandler
290
public void handleCallback(Callback callback) throws HandleCallbackException
292
log.info("received callback");
b'\\ No newline at end of file'