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.security;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import javax.management.MBeanServer;
29
import javax.management.MBeanServerFactory;
30
import javax.management.ObjectName;
32
import junit.framework.TestCase;
34
import org.apache.log4j.ConsoleAppender;
35
import org.apache.log4j.Level;
36
import org.apache.log4j.Logger;
37
import org.apache.log4j.PatternLayout;
38
import org.jboss.logging.XLevel;
39
import org.jboss.remoting.Client;
40
import org.jboss.remoting.InvocationRequest;
41
import org.jboss.remoting.InvokerLocator;
42
import org.jboss.remoting.ServerInvocationHandler;
43
import org.jboss.remoting.ServerInvoker;
44
import org.jboss.remoting.callback.Callback;
45
import org.jboss.remoting.callback.HandleCallbackException;
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
47
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.remoting.transport.PortUtil;
53
* Unit test for JBREM-977.
55
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
56
* @version $Revision: 1.1 $
58
* Copyright May 4, 2008
61
public class CallbackErrorHandlerProxyTestCase extends TestCase
63
private static Logger log = Logger.getLogger(CallbackErrorHandlerProxyTestCase.class);
65
private static boolean firstTime = true;
66
protected String host;
68
protected String locatorURI;
69
protected InvokerLocator serverLocator;
70
protected Connector connector;
71
protected TestCallbackErrorHandler errorHandler;
72
protected MBeanServer server;
73
protected ObjectName errorHandlerObjectName;
76
public void setUp() throws Exception
81
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
82
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
83
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
84
PatternLayout layout = new PatternLayout(pattern);
85
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
86
Logger.getRootLogger().addAppender(consoleAppender);
91
public void tearDown()
96
public void testCallbackErrorHandlerProxy() throws Throwable
98
log.info("entering " + getName());
102
log.info("ServerInvocationHandler: " + errorHandler);
105
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
106
HashMap clientConfig = new HashMap();
107
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
108
addExtraClientConfig(clientConfig);
109
Client client = new Client(clientLocator, clientConfig);
111
log.info("client is connected");
114
assertEquals("abc", client.invoke("abc"));
115
log.info("connection is good");
117
// Verify callbacks work.
118
TestCallbackHandler callbackHandler = new TestCallbackHandler();
119
client.addListener(callbackHandler, null, null, true);
120
assertEquals(1, callbackHandler.counter);
122
// Verify CallbackErrorHandler proxy gets used.
123
client.addListener(callbackHandler, null, null, true);
124
assertEquals(1, callbackHandler.counter);
125
assertEquals(1, errorHandler.counter);
126
int counter = ((Integer) server.getAttribute(errorHandlerObjectName, "Counter")).intValue();
127
assertEquals(errorHandler.counter, counter);
131
log.info(getName() + " PASSES");
135
protected String getTransport()
141
protected void addExtraClientConfig(Map config) {}
142
protected void addExtraServerConfig(Map config) {}
145
protected void setupServer() throws Exception
147
host = InetAddress.getLocalHost().getHostAddress();
148
port = PortUtil.findFreePort(host);
149
locatorURI = getTransport() + "://" + host + ":" + port;
150
serverLocator = new InvokerLocator(locatorURI);
151
log.info("Starting remoting server with locator uri of: " + locatorURI);
152
HashMap config = new HashMap();
153
config.put(InvokerLocator.FORCE_REMOTE, "true");
154
addExtraServerConfig(config);
156
// Create CallbackErrorHandler.
157
server = MBeanServerFactory.createMBeanServer();
158
errorHandler = new TestCallbackErrorHandler();
159
String objectNameString = "test:type=TestCallbackErrorHandler";
160
errorHandlerObjectName = new ObjectName(objectNameString);
161
server.registerMBean(errorHandler, errorHandlerObjectName);
162
config.put(ServerInvokerCallbackHandler.CALLBACK_ERROR_HANDLER_KEY, objectNameString);
164
// Create Connector..
165
connector = new Connector(serverLocator, config);
166
server.registerMBean(connector, new ObjectName("test:type=Connector"));
168
connector.addInvocationHandler("test", new TestServerInvocationHandler());
173
protected void shutdownServer() throws Exception
175
if (connector != null)
180
public static class TestServerInvocationHandler implements ServerInvocationHandler
182
static Logger log = Logger.getLogger(TestServerInvocationHandler.class);
185
public void addListener(InvokerCallbackHandler callbackHandler)
191
// First time, send callback.
192
callbackHandler.handleCallback(new Callback("callback"));
196
// Next, generate callback exception.
197
callbackHandler.handleCallback(new Callback(new NotSerializable()));
200
catch (HandleCallbackException e)
203
log.error("Unexpected exception", e);
205
log.info("Expected exception: " + e.getMessage());
209
public Object invoke(final InvocationRequest invocation) throws Throwable
211
return invocation.getParameter();
214
public void removeListener(InvokerCallbackHandler callbackHandler) {}
215
public void setMBeanServer(MBeanServer server) {}
216
public void setInvoker(ServerInvoker invoker) {}
218
public int getCounter()
225
static class TestCallbackHandler implements InvokerCallbackHandler
229
public void handleCallback(Callback callback) throws HandleCallbackException
232
log.info("received callback");
236
static class NotSerializable {}
b'\\ No newline at end of file'