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.ipv6;
24
import java.net.InetAddress;
25
import java.net.ServerSocket;
26
import java.security.AccessController;
27
import java.security.PrivilegedActionException;
28
import java.security.PrivilegedExceptionAction;
29
import java.util.HashMap;
30
import java.util.Iterator;
34
import javax.management.MBeanServer;
35
import javax.management.MBeanServerFactory;
36
import javax.management.ObjectInstance;
37
import javax.management.ObjectName;
39
import junit.framework.TestCase;
41
import org.apache.log4j.ConsoleAppender;
42
import org.apache.log4j.Level;
43
import org.apache.log4j.Logger;
44
import org.apache.log4j.PatternLayout;
45
import org.jboss.remoting.Client;
46
import org.jboss.remoting.InvocationRequest;
47
import org.jboss.remoting.InvokerLocator;
48
import org.jboss.remoting.ServerInvocationHandler;
49
import org.jboss.remoting.ServerInvoker;
50
import org.jboss.remoting.callback.Callback;
51
import org.jboss.remoting.callback.HandleCallbackException;
52
import org.jboss.remoting.callback.InvokerCallbackHandler;
53
import org.jboss.remoting.network.NetworkRegistry;
54
import org.jboss.remoting.transport.Connector;
55
import org.jboss.remoting.transport.PortUtil;
58
public class ObjectNameWithIPv6AddressTestCase extends TestCase
60
private static Logger log = Logger.getLogger(ObjectNameWithIPv6AddressTestCase.class);
62
private static boolean firstTime = true;
63
private static boolean ipv6IsAvailable = true;
65
protected String host;
67
protected String locatorURI;
68
protected InvokerLocator serverLocator;
69
protected Connector connector;
70
protected TestInvocationHandler invocationHandler;
73
public void setUp() throws Exception
78
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
79
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
80
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
81
PatternLayout layout = new PatternLayout(pattern);
82
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
83
Logger.getRootLogger().addAppender(consoleAppender);
84
log.info("java.version: " + System.getProperty("java.version"));
88
InetAddress addr = InetAddress.getByName("[::1]");
89
new ServerSocket(3333, 200, addr);
93
if ("Protocol family unavailable".equalsIgnoreCase(e.getMessage()) ||
94
"Protocol family not supported".equalsIgnoreCase(e.getMessage()))
96
ipv6IsAvailable = false;
97
log.info("ipV6 is not available");
104
public void tearDown()
109
public void testIPv6Loopback() throws Throwable
111
log.info("entering " + getName());
113
log.info(getName() + " PASSES");
117
public void testIPv6Any() throws Throwable
119
log.info("entering " + getName());
121
log.info(getName() + " PASSES");
125
public void testIPv4Mapped() throws Throwable
127
log.info("entering " + getName());
128
doTest("[::ffff:127.0.0.1]");
129
log.info(getName() + " PASSES");
133
protected void doTest(String host) throws Throwable
135
if (!ipv6IsAvailable) return;
137
MBeanServer server = null;
140
server = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
142
public Object run() throws Exception
144
return MBeanServerFactory.createMBeanServer();
148
catch (PrivilegedActionException e)
150
throw (Exception) e.getCause();
153
Connector connector = new Connector();
154
port = PortUtil.findFreePort(host);
155
locatorURI = getTransport() + "://" + host + ":" + port;
156
serverLocator = new InvokerLocator(locatorURI);
157
connector.setInvokerLocator(locatorURI);
158
ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + getTransport());
159
server.registerMBean(connector, obj);
161
invocationHandler = new TestInvocationHandler();
162
connector.addInvocationHandler("test", invocationHandler);
165
ServerInvoker serverInvoker = connector.getServerInvoker();
166
ObjectName objectName = new ObjectName(serverInvoker.getMBeanObjectName());
167
assertTrue(objectName.getCanonicalName().indexOf(host) >= 0);
168
assertTrue(server.isRegistered(objectName));
169
log.info(objectName + " is registered");
172
HashMap clientConfig = new HashMap();
173
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
174
addExtraClientConfig(clientConfig);
175
Client client = new Client(serverLocator, clientConfig);
177
log.info("client is connected");
180
assertEquals("abc", client.invoke("abc"));
181
log.info("connection is good");
188
protected String getTransport()
194
protected void addExtraClientConfig(Map config) {}
195
protected void addExtraServerConfig(Map config) {}
198
protected void setupServer() throws Exception
200
host = InetAddress.getLocalHost().getHostAddress();
201
port = PortUtil.findFreePort(host);
202
locatorURI = getTransport() + "://" + host + ":" + port;
203
serverLocator = new InvokerLocator(locatorURI);
204
log.info("Starting remoting server with locator uri of: " + locatorURI);
205
HashMap config = new HashMap();
206
config.put(InvokerLocator.FORCE_REMOTE, "true");
207
addExtraServerConfig(config);
208
connector = new Connector(serverLocator, config);
210
invocationHandler = new TestInvocationHandler();
211
connector.addInvocationHandler("test", invocationHandler);
216
protected void shutdownServer() throws Exception
218
if (connector != null)
223
static class TestInvocationHandler implements ServerInvocationHandler
225
public void addListener(InvokerCallbackHandler callbackHandler) {}
226
public Object invoke(final InvocationRequest invocation) throws Throwable
228
return invocation.getParameter();
230
public void removeListener(InvokerCallbackHandler callbackHandler) {}
231
public void setMBeanServer(MBeanServer server) {}
232
public void setInvoker(ServerInvoker invoker) {}
236
static class TestCallbackHandler implements InvokerCallbackHandler
238
public void handleCallback(Callback callback) throws HandleCallbackException
240
log.info("received callback");
b'\\ No newline at end of file'