JBoss Remoting SVN: r3755 - remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2008-03-25 19:15:16 -0400 (Tue, 25 Mar 2008)
New Revision: 3755
Modified:
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java
remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/SrpSaslClientImpl.java
Log:
Correctly detect the maximum key size allowed for AES and Blowfish
Modified: remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java
===================================================================
--- remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java 2008-03-25 21:09:44 UTC (rev 3754)
+++ remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/AbstractSrpSaslParticipant.java 2008-03-25 23:15:16 UTC (rev 3755)
@@ -13,6 +13,7 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
+import java.security.AccessController;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -50,6 +51,7 @@
protected static final Map<String, String> SRP_TO_JCA_HMAC;
protected static final Map<String, String> SRP_TO_JCA_SBC;
protected static final Map<String, int[]> SRP_TO_JCA_KEY_SIZES;
+ protected static final Map<String, Integer> SRP_TO_JCA_IV_SIZES;
protected static final String namePrompt = "SRP authentication ID: ";
protected static final String passwordPrompt = "SRP password: ";
protected static final String mechanismName = "SRP";
@@ -68,6 +70,7 @@
private byte[] sessionKey;
private String jcaEncryptionAlgName;
private int[] cipherKeySizes;
+ private int cipherIvSize;
static {
final Map<String, String> hmacMap = new HashMap<String, String>();
@@ -88,18 +91,42 @@
mdMap.put("sha-512", "SHA-512");
SRP_TO_JCA_MD = Collections.unmodifiableMap(mdMap);
final Map<String, String> sbcMap = new HashMap<String, String>();
- sbcMap.put("aes", "AES/CBC/PKCS5Padding");
- sbcMap.put("blowfish", "Blowfish/CBC/PKCS5Padding");
+ final Map<String, int[]> keySizeMap = new HashMap<String, int[]>();
+ final Map<String, Integer> ivSizeMap = new HashMap<String, Integer>();
+ // Detect the valid algorithms & key sizes
+ try {
+ final int maxAesLength = Cipher.getMaxAllowedKeyLength("AES");
+ sbcMap.put("aes", "AES/CBC/PKCS5Padding");
+ ivSizeMap.put("aes", Integer.valueOf(16));
+ if (maxAesLength < 256) {
+ if (maxAesLength < 192) {
+ keySizeMap.put("aes", new int[] { 16 });
+ } else {
+ keySizeMap.put("aes", new int[] { 16, 24 });
+ }
+ } else {
+ keySizeMap.put("aes", new int[] { 16, 24, 32 });
+ }
+ } catch (NoSuchAlgorithmException e) {
+ // AES is not supported
+ }
+ try {
+ final int maxBlowfishLength = Cipher.getMaxAllowedKeyLength("Blowfish");
+ sbcMap.put("blowfish", "Blowfish/CBC/PKCS5Padding");
+ ivSizeMap.put("blowfish", Integer.valueOf(8));
+ // limit the keysize, since apparently blowfish lets you use really, REALLY big keys
+ final int byteLength = Math.min(maxBlowfishLength / 8, 56);
+ int[] sizes = new int[byteLength - 3];
+ for (int i = 4; i < byteLength && i <= 56; i ++) {
+ sizes[i - 4] = i;
+ }
+ keySizeMap.put("blowfish", sizes);
+ } catch (NoSuchAlgorithmException e) {
+ // Blowfish is not supported
+ }
SRP_TO_JCA_SBC = Collections.unmodifiableMap(sbcMap);
- // This whole thing is lame - there's no way to query valid key sizes - JCA sucks
- final Map<String, int[]> keySizeMap = new HashMap<String, int[]>();
- keySizeMap.put("aes", new int[] { 16, 24, 32 });
- keySizeMap.put("blowfish", new int[] {
- 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
- 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
- 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56
- });
SRP_TO_JCA_KEY_SIZES = Collections.unmodifiableMap(keySizeMap);
+ SRP_TO_JCA_IV_SIZES = Collections.unmodifiableMap(ivSizeMap);
}
protected AbstractSrpSaslParticipant(final CallbackHandler callbackHandler) {
@@ -305,11 +332,18 @@
}
protected void selectIntegrity(String algorithmName, byte[] key) throws SaslException, NoSuchAlgorithmException, InvalidKeyException {
- Mac outboundMac, inboundMac;
- outboundMac = Mac.getInstance(algorithmName);
- outboundMac.init(new SecretKeySpec(key, algorithmName));
- inboundMac = Mac.getInstance(algorithmName);
- inboundMac.init(new SecretKeySpec(key, algorithmName));
+ final Mac outboundMac, inboundMac;
+ final String realAlgorithmName;
+ if (SRP_TO_JCA_HMAC.containsKey(algorithmName)) {
+ realAlgorithmName = SRP_TO_JCA_HMAC.get(algorithmName);
+ } else {
+ // Alogorithm not supported. To add support, update the SRP_TO_JCA_SBC and SRP_TO_JCA_KEY_SIZE maps above
+ throw new NoSuchAlgorithmException("This SRP implementation does not support the HMAC algorithm \"" + algorithmName + "\"");
+ }
+ outboundMac = Mac.getInstance(realAlgorithmName);
+ outboundMac.init(new SecretKeySpec(key, realAlgorithmName));
+ inboundMac = Mac.getInstance(realAlgorithmName);
+ inboundMac.init(new SecretKeySpec(key, realAlgorithmName));
this.outboundMac = outboundMac;
this.inboundMac = inboundMac;
integrityEnabled = true;
@@ -331,20 +365,26 @@
jcaEncryptionAlgName = SRP_TO_JCA_SBC.get(algorithmName);
} else {
// Alogorithm not supported. To add support, update the SRP_TO_JCA_SBC and SRP_TO_JCA_KEY_SIZE maps above
- throw new NoSuchAlgorithmException("This SRP implementation does not support the algorithm \"" + algorithmName + "\"");
+ throw new NoSuchAlgorithmException("This SRP implementation does not support the cipher algorithm \"" + algorithmName + "\"");
}
cipherKeySizes = SRP_TO_JCA_KEY_SIZES.get(algorithmName);
if (cipherKeySizes == null) {
throw new NoSuchAlgorithmException("Algorithm \"" + algorithmName + "\" does not have any valid key sizes specified");
}
+ final Integer integer = SRP_TO_JCA_IV_SIZES.get(algorithmName);
+ if (integer == null) {
+ throw new NoSuchAlgorithmException("Algorithm \"" + algorithmName + "\" does not have a valid IV size specified");
+ }
+ cipherIvSize = integer.intValue();
encryptCipher = Cipher.getInstance(jcaEncryptionAlgName);
decryptCipher = Cipher.getInstance(jcaEncryptionAlgName);
this.encryptCipher = encryptCipher;
this.decryptCipher = decryptCipher;
}
- protected void enableConfidentiality(byte[] key, byte[] encryptIV, byte[] decryptIV) throws InvalidKeyException, InvalidAlgorithmParameterException {
+ protected void enableConfidentiality(final byte[] key, final byte[] encryptIV, final byte[] decryptIV) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (encryptCipher != null && decryptCipher != null && cipherKeySizes != null) {
+ final int keyLength, ivLength;
final int idx = Arrays.binarySearch(cipherKeySizes, key.length);
if (idx < 0) {
// key size isn't exact, let's pick the next smaller key size
@@ -353,14 +393,28 @@
throw new InvalidKeyException("Negotiated key is too short to use with this algorithm");
}
final int size = cipherKeySizes[newIdx];
- byte[] actualKey = new byte[size];
- System.arraycopy(key, 0, actualKey, 0, size);
- key = actualKey;
+ keyLength = size;
+ } else {
+ keyLength = key.length;
}
- encryptCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, jcaEncryptionAlgName), new IvParameterSpec(encryptIV));
- decryptCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, jcaEncryptionAlgName), new IvParameterSpec(decryptIV));
+ ivLength = cipherIvSize;
+ final String simpleName;
+ final int slashPosition = jcaEncryptionAlgName.indexOf('/');
+ if (slashPosition != -1) {
+ simpleName = jcaEncryptionAlgName.substring(0, slashPosition);
+ } else {
+ simpleName = jcaEncryptionAlgName;
+ }
+ encryptCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, keyLength, simpleName), new IvParameterSpec(encryptIV, 0, ivLength));
+ decryptCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, 0, keyLength, simpleName), new IvParameterSpec(decryptIV, 0, ivLength));
confidentialityEnabled = true;
- sessionKey = key;
+ if (keyLength < key.length) {
+ byte[] newSessionKey = new byte[keyLength];
+ System.arraycopy(key, 0, newSessionKey, 0, keyLength);
+ sessionKey = newSessionKey;
+ } else {
+ sessionKey = key;
+ }
}
}
Modified: remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/SrpSaslClientImpl.java
===================================================================
--- remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/SrpSaslClientImpl.java 2008-03-25 21:09:44 UTC (rev 3754)
+++ remoting3/trunk/srp/src/main/java/org/jboss/cx/remoting/core/security/sasl/SrpSaslClientImpl.java 2008-03-25 23:15:16 UTC (rev 3755)
@@ -307,6 +307,7 @@
try {
selectIntegrity(integrity, K_bytes);
clientOptions.getIntegritySet().add(integrity);
+ System.out.println("Adding integrity option " + integrity);
break;
} catch (NoSuchAlgorithmException e) {
if (trace) log.trace("Rejected JCA MAC algorithm '" + integrity + "': " + e.getMessage());
16 years, 9 months
JBoss Remoting SVN: r3754 - remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2008-03-25 17:09:44 -0400 (Tue, 25 Mar 2008)
New Revision: 3754
Modified:
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java
Log:
Connects.... again.... sort of
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java 2008-03-25 20:37:25 UTC (rev 3753)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java 2008-03-25 21:09:44 UTC (rev 3754)
@@ -106,8 +106,10 @@
}
private enum State implements org.jboss.cx.remoting.util.State<State> {
- /** Initial state - unconnected */
+ /** Initial state - unconnected and uninitialized */
NEW,
+ /** Initial state - unconnected but initialized */
+ INITIALIZED,
/** Client side, waiting to receive protocol version info */
AWAITING_SERVER_VERSION,
/** Server side, waiting to receive protocol version info */
@@ -141,18 +143,14 @@
protocolHandler = new RemotingProtocolHandler();
}
- public void initializeClient(final IoSession ioSession, final ProtocolContext protocolContext) {
+ public void initializeClient(final IoSession ioSession) {
if (ioSession == null) {
throw new NullPointerException("ioSession is null");
}
- if (protocolContext == null) {
- throw new NullPointerException("protocolContext is null");
- }
- state.transitionExclusive(State.NEW, State.AWAITING_SERVER_VERSION);
+ state.transitionExclusive(State.NEW, State.INITIALIZED);
try {
ioSession.setAttribute(JRPP_CONNECTION, this);
this.ioSession = ioSession;
- this.protocolContext = protocolContext;
client = true;
remoteRootContextIdentifier = new JrppContextIdentifier(false, 0);
localRootContextIdentifier = new JrppContextIdentifier(true, 0);
@@ -161,18 +159,14 @@
}
}
- public void initializeServer(final IoSession ioSession, final ProtocolContext protocolContext) {
+ public void initializeServer(final IoSession ioSession) {
if (ioSession == null) {
throw new NullPointerException("ioSession is null");
}
- if (protocolContext == null) {
- throw new NullPointerException("protocolContext is null");
- }
- state.transitionExclusive(State.NEW, State.AWAITING_CLIENT_VERSION);
+ state.transitionExclusive(State.NEW, State.INITIALIZED);
try {
ioSession.setAttribute(JRPP_CONNECTION, this);
this.ioSession = ioSession;
- this.protocolContext = protocolContext;
client = false;
remoteRootContextIdentifier = new JrppContextIdentifier(true, 0);
localRootContextIdentifier = new JrppContextIdentifier(false, 0);
@@ -181,6 +175,15 @@
}
}
+ public void start(final ProtocolContext protocolContext) {
+ if (protocolContext == null) {
+ throw new NullPointerException("protocolContext is null");
+ }
+ state.requireTransitionExclusive(State.INITIALIZED, client ? State.AWAITING_SERVER_VERSION : State.AWAITING_CLIENT_VERSION);
+ this.protocolContext = protocolContext;
+ state.releaseExclusive();
+ }
+
private String getNegotiatedMechanism(final String[] clientMechs, final Set<String> serverMechs) throws SaslException {
for (String name : clientMechs) {
if (serverMechs.contains(name)) {
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java 2008-03-25 20:37:25 UTC (rev 3753)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java 2008-03-25 21:09:44 UTC (rev 3754)
@@ -135,7 +135,8 @@
final SocketAddress serverAddress = getSocketAddressFromUri(remoteUri);
final ConnectFuture future = connector.connect(serverAddress, new IoSessionInitializer<ConnectFuture>() {
public void initializeSession(final IoSession ioSession, final ConnectFuture connectFuture) {
- jrppConnection.initializeClient(ioSession, context);
+ jrppConnection.initializeClient(ioSession);
+ jrppConnection.start(context);
}
});
future.awaitUninterruptibly();
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java 2008-03-25 20:37:25 UTC (rev 3753)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java 2008-03-25 21:09:44 UTC (rev 3754)
@@ -17,9 +17,8 @@
import org.jboss.cx.remoting.util.AttributeMap;
import org.jboss.cx.remoting.jrpp.mina.FramingIoFilter;
import org.jboss.cx.remoting.Endpoint;
+import org.jboss.cx.remoting.spi.protocol.ProtocolContext;
-import com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl;
-
/**
*
*/
@@ -118,7 +117,9 @@
private final class ServerSessionHandlerFactory implements SingleSessionIoHandlerFactory {
public SingleSessionIoHandler getHandler(IoSession ioSession) throws IOException {
final JrppConnection connection = new JrppConnection(attributeMap);
- endpoint.openIncomingSession(connection.getProtocolHandler());
+ connection.initializeServer(ioSession);
+ final ProtocolContext protocolContext = endpoint.openIncomingSession(connection.getProtocolHandler());
+ connection.start(protocolContext);
return connection.getIoHandler();
}
}
16 years, 9 months
JBoss Remoting SVN: r3753 - remoting2/tags.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 16:37:25 -0400 (Tue, 25 Mar 2008)
New Revision: 3753
Added:
remoting2/tags/2.2.2-SP6/
Log:
Copied: remoting2/tags/2.2.2-SP6 (from rev 3752, remoting2/branches/2.2)
16 years, 9 months
JBoss Remoting SVN: r3752 - remoting2/branches/2.2/docs.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 15:51:22 -0400 (Tue, 25 Mar 2008)
New Revision: 3752
Modified:
remoting2/branches/2.2/docs/README.txt
Log:
JBREM-939: Added 2.2.2.SP6 release notes.
Modified: remoting2/branches/2.2/docs/README.txt
===================================================================
--- remoting2/branches/2.2/docs/README.txt 2008-03-25 19:33:36 UTC (rev 3751)
+++ remoting2/branches/2.2/docs/README.txt 2008-03-25 19:51:22 UTC (rev 3752)
@@ -27,6 +27,22 @@
in Jira, please create one.
==========================================================================================================
+Release Notes - JBoss Remoting - Version 2.2.2.SP6
+
+Bug
+
+ * [JBREM-915] - NullPointerException in InvokerLocator
+ * [JBREM-937] - Callback BisocketServerInvoker should reuse available ServerThreads
+
+Release
+
+ * [JBREM-939] - Release 2.2.2.SP6
+
+Task
+
+ * [JBREM-940] - Assure version compatibility with earlier versions of Remoting
+
+==========================================================================================================
Release Notes - JBoss Remoting - Version 2.2.2.SP5
Bug
16 years, 9 months
JBoss Remoting SVN: r3751 - remoting2/branches/2.2.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 15:33:36 -0400 (Tue, 25 Mar 2008)
New Revision: 3751
Modified:
remoting2/branches/2.2/build.xml
Log:
JBREM-939: Updated version to 2.2.2.SP6.
Modified: remoting2/branches/2.2/build.xml
===================================================================
--- remoting2/branches/2.2/build.xml 2008-03-25 19:27:14 UTC (rev 3750)
+++ remoting2/branches/2.2/build.xml 2008-03-25 19:33:36 UTC (rev 3751)
@@ -37,9 +37,9 @@
<!-- Module name(s) & version -->
<property name="module.name" value="remoting"/>
<property name="module.Name" value="JBoss Remoting"/>
- <property name="module.version" value="2.2.2.SP5"/>
+ <property name="module.version" value="2.2.2.SP6"/>
<!-- extension is for the file suffix to use for distribution build -->
- <property name="module.version.extension" value="2_2_2_SP5"/>
+ <property name="module.version.extension" value="2_2_2_SP6"/>
<property name="implementation.url" value="http://www.jboss.org/products/remoting"/>
<property name="root.dir" value="${basedir}"/>
16 years, 9 months
JBoss Remoting SVN: r3750 - remoting2/branches/2.2/src/main/org/jboss/remoting.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 15:27:14 -0400 (Tue, 25 Mar 2008)
New Revision: 3750
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/Version.java
Log:
JBREM-939: Updated version to 2.2.2.SP6.
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/Version.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/Version.java 2008-03-25 19:23:59 UTC (rev 3749)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/Version.java 2008-03-25 19:27:14 UTC (rev 3750)
@@ -32,7 +32,7 @@
public static final byte VERSION_2 = 2;
public static final byte VERSION_2_2 = 22;
- public static final String VERSION = "2.2.2.SP5 (Bluto)";
+ public static final String VERSION = "2.2.2.SP6 (Bluto)";
private static final byte byteVersion = VERSION_2_2;
private static byte defaultByteVersion = byteVersion;
private static boolean performVersioning = true;
16 years, 9 months
JBoss Remoting SVN: r3749 - in remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket: threadpool and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 15:23:59 -0400 (Tue, 25 Mar 2008)
New Revision: 3749
Added:
remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/
remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java
Log:
JBREM-937: New unit test.
Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java
===================================================================
--- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java (rev 0)
+++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java 2008-03-25 19:23:59 UTC (rev 3749)
@@ -0,0 +1,260 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.transport.bisocket.threadpool;
+
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.bisocket.Bisocket;
+import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
+import org.jboss.remoting.transport.socket.LRUPool;
+import org.jboss.remoting.transport.socket.ServerThread;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+
+
+/**
+ * Unit test for JBREM-938.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 22, 2008
+ * </p>
+ */
+public class ServerThreadReuseTestCase extends TestCase
+{
+ private static Logger log = Logger.getLogger(ServerThreadReuseTestCase.class);
+
+ private static boolean firstTime = true;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testServerThreadReuse() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer();
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connections.
+ assertEquals("abc", client.invoke("abc"));
+ log.info("connection is good");
+
+ // Add callback handler.
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ HashMap metadata = new HashMap();
+ metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
+ client.addListener(callbackHandler, metadata, null, true);
+ client.invoke("callback");
+ assertEquals(1, callbackHandler.counter);
+ log.info("first callback successful");
+
+ // Get callback server thread pools.
+ Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
+ assertEquals(1, callbackConnectors.size());
+ Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
+ BisocketServerInvoker invoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
+ Field field = SocketServerInvoker.class.getDeclaredField("threadpool");
+ field.setAccessible(true);
+ List threadpool = (List) field.get(invoker);
+ field = SocketServerInvoker.class.getDeclaredField("clientpool");
+ field.setAccessible(true);
+ LRUPool clientpool = (LRUPool) field.get(invoker);
+ assertEquals(0, threadpool.size());
+ assertEquals(1, clientpool.size());
+
+ // Kill worker thread's socket.
+ Set clientpoolContents = clientpool.getContents();
+ ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
+ field = ServerThread.class.getDeclaredField("socket");
+ field.setAccessible(true);
+ Socket socket = (Socket) field.get(serverThread1);
+ socket.close();
+ Thread.sleep(4000);
+ assertEquals(1, threadpool.size());
+ assertEquals(0, clientpool.size());
+
+ // Make second callback and verify that worker thread gets reused.
+ client.invoke("callback");
+ assertEquals(2, callbackHandler.counter);
+ log.info("second callback successful");
+ assertEquals(0, threadpool.size());
+ assertEquals(1, clientpool.size());
+ clientpoolContents = clientpool.getContents();
+ ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
+ assertEquals(serverThread2, serverThread1);
+ log.info("ServerThread was reused");
+
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected String getTransport()
+ {
+ return "bisocket";
+ }
+
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ protected void setupServer() throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port;
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ HashSet callbackHandlers = new HashSet();
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.add(callbackHandler);
+ }
+
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ String command = (String) invocation.getParameter();
+
+ if ("callback".equals(command))
+ {
+ Callback callback = new Callback("callback");
+ Iterator it = callbackHandlers.iterator();
+ while (it.hasNext())
+ {
+ ServerInvokerCallbackHandler handler;
+ handler = (ServerInvokerCallbackHandler) it.next();
+ handler.handleCallback(callback);
+ }
+ }
+
+ return command;
+ }
+
+ public void removeListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.remove(callbackHandler);
+ }
+
+
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public int counter;
+
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ counter++;
+ log.info("received callback");
+ }
+ }
+}
\ No newline at end of file
16 years, 9 months
JBoss Remoting SVN: r3748 - in remoting3/trunk: api/src/main/java/org/jboss/cx/remoting/spi/protocol and 3 other directories.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2008-03-25 14:54:14 -0400 (Tue, 25 Mar 2008)
New Revision: 3748
Removed:
remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/protocol/ProtocolServerContext.java
Modified:
remoting3/trunk/build.xml
remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/CoreEndpoint.java
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java
remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java
remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java
Log:
Move protocol server context into endpoint proper; add a JRPP server support method to standalone
Deleted: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/protocol/ProtocolServerContext.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/protocol/ProtocolServerContext.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/protocol/ProtocolServerContext.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -1,11 +0,0 @@
-package org.jboss.cx.remoting.spi.protocol;
-
-import org.jboss.cx.remoting.Context;
-
-/**
- *
- */
-public interface ProtocolServerContext {
- <I, O> ProtocolContext establishSession(ProtocolHandler handler, Context<I, O> rootContext);
-
-}
Modified: remoting3/trunk/build.xml
===================================================================
--- remoting3/trunk/build.xml 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/build.xml 2008-03-25 18:54:14 UTC (rev 3748)
@@ -863,6 +863,7 @@
<path refid="api.classpath"/>
<path refid="core.classpath"/>
<path refid="log-jul.classpath"/>
+ <path refid="jrpp.classpath"/>
<path refid="util.classpath"/>
</classpath>
</javac>
@@ -873,7 +874,7 @@
<delete dir="standalone/target"/>
</target>
- <target name="standalone" description="Build the standalone module" depends="api,core,log-jul,util,standalone.compile">
+ <target name="standalone" description="Build the standalone module" depends="api,core,jrpp,log-jul,util,standalone.compile">
<path id="standalone.classpath">
<pathelement location="standalone/target/main/classes"/>
</path>
Modified: remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/CoreEndpoint.java
===================================================================
--- remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/CoreEndpoint.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/CoreEndpoint.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -24,7 +24,6 @@
import org.jboss.cx.remoting.spi.protocol.ProtocolContext;
import org.jboss.cx.remoting.spi.protocol.ProtocolHandler;
import org.jboss.cx.remoting.spi.protocol.ProtocolHandlerFactory;
-import org.jboss.cx.remoting.spi.protocol.ProtocolServerContext;
import org.jboss.cx.remoting.spi.Registration;
import javax.security.auth.callback.Callback;
@@ -108,19 +107,7 @@
return orderedExecutorFactory.getOrderedExecutor();
}
- public final class CoreProtocolServerContext implements ProtocolServerContext {
- private CoreProtocolServerContext() {
- }
-
- public <I, O> ProtocolContext establishSession(final ProtocolHandler handler, final Context<I, O> rootContext) {
- final CoreSession session = new CoreSession(CoreEndpoint.this);
- session.initializeServer(handler, rootContext);
- return session.getProtocolContext();
- }
- }
-
public final class CoreProtocolRegistration implements Registration {
- private final CoreProtocolServerContext protocolServerContext = new CoreProtocolServerContext();
private final ProtocolHandlerFactory protocolHandlerFactory;
private CoreProtocolRegistration(final ProtocolHandlerFactory protocolHandlerFactory) {
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppConnection.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -39,7 +39,6 @@
import org.jboss.cx.remoting.spi.protocol.ContextIdentifier;
import org.jboss.cx.remoting.spi.protocol.ProtocolContext;
import org.jboss.cx.remoting.spi.protocol.ProtocolHandler;
-import org.jboss.cx.remoting.spi.protocol.ProtocolServerContext;
import org.jboss.cx.remoting.spi.protocol.RequestIdentifier;
import org.jboss.cx.remoting.spi.protocol.ServiceIdentifier;
import org.jboss.cx.remoting.spi.protocol.StreamIdentifier;
@@ -87,6 +86,9 @@
private final AtomicInteger serviceIdSequence = new AtomicInteger(0);
private final AtomicInteger requestIdSequence = new AtomicInteger(0);
+ private ContextIdentifier localRootContextIdentifier;
+ private ContextIdentifier remoteRootContextIdentifier;
+
private final Set<StreamIdentifier> liveStreamSet = CollectionUtil.synchronizedSet(new WeakHashSet<StreamIdentifier>());
private final Set<ContextIdentifier> liveContextSet = CollectionUtil.synchronizedSet(new WeakHashSet<ContextIdentifier>());
private final Set<RequestIdentifier> liveRequestSet = CollectionUtil.synchronizedSet(new WeakHashSet<RequestIdentifier>());
@@ -140,25 +142,40 @@
}
public void initializeClient(final IoSession ioSession, final ProtocolContext protocolContext) {
+ if (ioSession == null) {
+ throw new NullPointerException("ioSession is null");
+ }
+ if (protocolContext == null) {
+ throw new NullPointerException("protocolContext is null");
+ }
state.transitionExclusive(State.NEW, State.AWAITING_SERVER_VERSION);
try {
ioSession.setAttribute(JRPP_CONNECTION, this);
this.ioSession = ioSession;
this.protocolContext = protocolContext;
client = true;
+ remoteRootContextIdentifier = new JrppContextIdentifier(false, 0);
+ localRootContextIdentifier = new JrppContextIdentifier(true, 0);
} finally {
state.releaseExclusive();
}
}
- public void initializeServer(final IoSession ioSession, final ProtocolServerContext protocolServerContext) {
+ public void initializeServer(final IoSession ioSession, final ProtocolContext protocolContext) {
+ if (ioSession == null) {
+ throw new NullPointerException("ioSession is null");
+ }
+ if (protocolContext == null) {
+ throw new NullPointerException("protocolContext is null");
+ }
state.transitionExclusive(State.NEW, State.AWAITING_CLIENT_VERSION);
try {
ioSession.setAttribute(JRPP_CONNECTION, this);
this.ioSession = ioSession;
- final ProtocolContext protocolContext = protocolServerContext.establishSession(protocolHandler, null /* todo */);
this.protocolContext = protocolContext;
client = false;
+ remoteRootContextIdentifier = new JrppContextIdentifier(true, 0);
+ localRootContextIdentifier = new JrppContextIdentifier(false, 0);
} finally {
state.releaseExclusive();
}
@@ -359,13 +376,15 @@
}
public void waitForUp() throws IOException {
-
- while (! state.in(State.UP, State.FAILED)) {
-// state.waitForAny(); todo
+ try {
+ state.waitFor(State.UP);
+ } catch (IllegalStateException e) {
+ if (state.in(State.FAILED)) {
+ throw failureReason;
+ } else {
+ throw e;
+ }
}
- if (state.in(State.FAILED)) {
- throw failureReason;
- }
}
private void close() {
@@ -574,11 +593,11 @@
}
public ContextIdentifier getLocalRootContextIdentifier() {
- return null;
+ return localRootContextIdentifier;
}
public ContextIdentifier getRemoteRootContextIdentifier() {
- return null;
+ return remoteRootContextIdentifier;
}
public void sendCancelRequest(ContextIdentifier contextIdentifier, RequestIdentifier requestIdentifier, boolean mayInterrupt) throws IOException {
@@ -659,8 +678,12 @@
}
public void messageReceived(Object message) throws Exception {
+ final ObjectMessageInput input = protocolContext.getMessageInput(new IoBufferByteMessageInput((IoBuffer) message));
+ handleMessage(input);
+ }
+
+ private void handleMessage(final ObjectMessageInput input) throws Exception {
final boolean trace = log.isTrace();
- final ObjectMessageInput input = protocolContext.getMessageInput(new IoBufferByteMessageInput((IoBuffer) message));
final MessageType type = MessageType.values()[input.readByte() & 0xff];
if (trace) {
log.trace("Received message of type %s in state %s", type, state.getState());
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppProtocolSupport.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -139,6 +139,14 @@
}
});
future.awaitUninterruptibly();
+ if (! future.isConnected()) {
+ final Throwable t = future.getException();
+ if (t instanceof IOException) {
+ throw (IOException)t;
+ } else {
+ throw new RemotingException("Connection failed due to an unexpected exception", t);
+ }
+ }
jrppConnection.waitForUp();
return jrppConnection.getProtocolHandler();
}
Modified: remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java
===================================================================
--- remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/jrpp/src/main/java/org/jboss/cx/remoting/jrpp/JrppServer.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -16,8 +16,10 @@
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.jboss.cx.remoting.util.AttributeMap;
import org.jboss.cx.remoting.jrpp.mina.FramingIoFilter;
-import org.jboss.cx.remoting.spi.protocol.ProtocolServerContext;
+import org.jboss.cx.remoting.Endpoint;
+import com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl;
+
/**
*
*/
@@ -39,10 +41,10 @@
private IoProcessor ioProcessor;
/** IO Acceptor. Set upon {@code create}. */
private IoAcceptor ioAcceptor;
- /** Protocol server context. Set upon {@code create}. */
- private ProtocolServerContext serverContext;
/** Attribute map. Set before {@code create}. */
private AttributeMap attributeMap;
+ /** Endpoint. Set before {@code create}. */
+ private Endpoint endpoint;
// Accessors
@@ -70,6 +72,14 @@
this.attributeMap = attributeMap;
}
+ public Endpoint getEndpoint() {
+ return endpoint;
+ }
+
+ public void setEndpoint(final Endpoint endpoint) {
+ this.endpoint = endpoint;
+ }
+
// Lifecycle
@SuppressWarnings ({"unchecked"})
@@ -101,7 +111,6 @@
ioAcceptor = null;
ioProcessor = null;
executor = null;
- serverContext = null;
}
// MINA support
@@ -109,7 +118,7 @@
private final class ServerSessionHandlerFactory implements SingleSessionIoHandlerFactory {
public SingleSessionIoHandler getHandler(IoSession ioSession) throws IOException {
final JrppConnection connection = new JrppConnection(attributeMap);
- connection.initializeServer(ioSession, serverContext);
+ endpoint.openIncomingSession(connection.getProtocolHandler());
return connection.getIoHandler();
}
}
Modified: remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java
===================================================================
--- remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java 2008-03-25 09:11:43 UTC (rev 3747)
+++ remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java 2008-03-25 18:54:14 UTC (rev 3748)
@@ -1,12 +1,17 @@
package org.jboss.cx.remoting;
import java.net.URI;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
+import java.io.IOException;
import org.jboss.cx.remoting.log.Logger;
import org.jboss.cx.remoting.core.CoreEndpoint;
import org.jboss.cx.remoting.core.protocol.LocalProtocolHandlerFactory;
import org.jboss.cx.remoting.jrpp.JrppProtocolSupport;
+import org.jboss.cx.remoting.jrpp.JrppServer;
+import org.jboss.cx.remoting.util.AttributeMap;
/**
*
@@ -14,7 +19,9 @@
public final class Remoting {
private static final Logger log = Logger.getLogger(Remoting.class);
- public static <I, O> Endpoint createEndpoint(String name, RequestListener<I, O> listener) throws RemotingException {
+ private static final String JRPP_SUPPORT_KEY = "org.jboss.cx.remoting.standalone.jrpp.support";
+
+ public static <I, O> Endpoint createEndpoint(String name, RequestListener<I, O> listener) throws IOException {
final CoreEndpoint coreEndpoint = new CoreEndpoint(name, listener);
final ExecutorService executorService = Executors.newCachedThreadPool();
coreEndpoint.setExecutor(executorService);
@@ -28,6 +35,7 @@
jrppProtocolSupport.setExecutor(executorService);
jrppProtocolSupport.create();
jrppProtocolSupport.start();
+ userEndpoint.getAttributes().put(JRPP_SUPPORT_KEY, jrppProtocolSupport);
userEndpoint.addCloseHandler(new CloseHandler<Endpoint>() {
public void handleClose(final Endpoint closed) {
executorService.shutdown();
@@ -41,6 +49,26 @@
}
}
+ public static JrppServer addJrppServer(Endpoint endpoint, SocketAddress address, AttributeMap attributeMap) throws IOException {
+ final JrppServer jrppServer = new JrppServer();
+ jrppServer.setProtocolSupport((JrppProtocolSupport) endpoint.getAttributes().get(JRPP_SUPPORT_KEY));
+ jrppServer.setSocketAddress(new InetSocketAddress(12345));
+ jrppServer.setAttributeMap(AttributeMap.EMPTY);
+ jrppServer.setEndpoint(endpoint);
+ jrppServer.create();
+ jrppServer.start();
+ endpoint.addCloseHandler(new CloseHandler<Endpoint>() {
+ public void handleClose(final Endpoint closed) {
+ try {
+ jrppServer.stop();
+ } finally {
+ jrppServer.destroy();
+ }
+ }
+ });
+ return jrppServer;
+ }
+
public static Session createEndpointAndSession(String endpointName, URI remoteUri, final String userName, final char[] password) throws RemotingException {
return null;
}
16 years, 9 months
JBoss Remoting SVN: r3747 - in remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket: threadpool and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 05:11:43 -0400 (Tue, 25 Mar 2008)
New Revision: 3747
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java
Log:
JBREM-938: New unit test.
Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java 2008-03-25 09:11:43 UTC (rev 3747)
@@ -0,0 +1,260 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.transport.bisocket.threadpool;
+
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.bisocket.Bisocket;
+import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
+import org.jboss.remoting.transport.socket.LRUPool;
+import org.jboss.remoting.transport.socket.ServerThread;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+
+
+/**
+ * Unit test for JBREM-938.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 22, 2008
+ * </p>
+ */
+public class ServerThreadReuseTestCase extends TestCase
+{
+ private static Logger log = Logger.getLogger(ServerThreadReuseTestCase.class);
+
+ private static boolean firstTime = true;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testServerThreadReuse() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer();
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connections.
+ assertEquals("abc", client.invoke("abc"));
+ log.info("connection is good");
+
+ // Add callback handler.
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ HashMap metadata = new HashMap();
+ metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
+ client.addListener(callbackHandler, metadata, null, true);
+ client.invoke("callback");
+ assertEquals(1, callbackHandler.counter);
+ log.info("first callback successful");
+
+ // Get callback server thread pools.
+ Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
+ assertEquals(1, callbackConnectors.size());
+ Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
+ BisocketServerInvoker invoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
+ Field field = SocketServerInvoker.class.getDeclaredField("threadpool");
+ field.setAccessible(true);
+ List threadpool = (List) field.get(invoker);
+ field = SocketServerInvoker.class.getDeclaredField("clientpool");
+ field.setAccessible(true);
+ LRUPool clientpool = (LRUPool) field.get(invoker);
+ assertEquals(0, threadpool.size());
+ assertEquals(1, clientpool.size());
+
+ // Kill worker thread's socket.
+ Set clientpoolContents = clientpool.getContents();
+ ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
+ field = ServerThread.class.getDeclaredField("socket");
+ field.setAccessible(true);
+ Socket socket = (Socket) field.get(serverThread1);
+ socket.close();
+ Thread.sleep(4000);
+ assertEquals(1, threadpool.size());
+ assertEquals(0, clientpool.size());
+
+ // Make second callback and verify that worker thread gets reused.
+ client.invoke("callback");
+ assertEquals(2, callbackHandler.counter);
+ log.info("second callback successful");
+ assertEquals(0, threadpool.size());
+ assertEquals(1, clientpool.size());
+ clientpoolContents = clientpool.getContents();
+ ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
+ assertEquals(serverThread2, serverThread1);
+ log.info("ServerThread was reused");
+
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected String getTransport()
+ {
+ return "bisocket";
+ }
+
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ protected void setupServer() throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port;
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ HashSet callbackHandlers = new HashSet();
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.add(callbackHandler);
+ }
+
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ String command = (String) invocation.getParameter();
+
+ if ("callback".equals(command))
+ {
+ Callback callback = new Callback("callback");
+ Iterator it = callbackHandlers.iterator();
+ while (it.hasNext())
+ {
+ ServerInvokerCallbackHandler handler;
+ handler = (ServerInvokerCallbackHandler) it.next();
+ handler.handleCallback(callback);
+ }
+ }
+
+ return command;
+ }
+
+ public void removeListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.remove(callbackHandler);
+ }
+
+
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public int counter;
+
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ counter++;
+ log.info("received callback");
+ }
+ }
+}
\ No newline at end of file
16 years, 9 months
JBoss Remoting SVN: r3746 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/locator.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2008-03-25 05:08:27 -0400 (Tue, 25 Mar 2008)
New Revision: 3746
Modified:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/locator/InvokerLocatorTestCase.java
Log:
JBREM-936: Added testNullHost().
Modified: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/locator/InvokerLocatorTestCase.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/locator/InvokerLocatorTestCase.java 2008-03-25 09:02:21 UTC (rev 3745)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/locator/InvokerLocatorTestCase.java 2008-03-25 09:08:27 UTC (rev 3746)
@@ -22,6 +22,7 @@
package org.jboss.test.remoting.locator;
+import java.net.InetAddress;
import java.net.MalformedURLException;
import java.util.List;
@@ -470,4 +471,35 @@
l = new InvokerLocator("socket://multihome:11/?connecthomes=a.b!c.d:12&" + InvokerLocator.DEFAULT_PORT + "=13&" + InvokerLocator.DEFAULT_CONNECT_PORT + "=14");
assertEquals("a.b:14!c.d:12", l.getConnectHomes());
}
+
+
+ /**
+ * For JBREM-936.
+ */
+ public void testNullHost() throws Exception
+ {
+ InvokerLocator locator = new InvokerLocator("socket://:7777");
+
+ String bindByHost = System.getProperty(InvokerLocator.BIND_BY_HOST, "True");
+ boolean byHost = true;
+ String host = null;
+
+ try
+ {
+ byHost = Boolean.getBoolean(bindByHost);
+ }
+ catch(Exception e)
+ {
+ }
+ if(byHost)
+ {
+ host = InetAddress.getLocalHost().getHostName();
+ }
+ else
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ }
+
+ assertEquals(host, locator.getHost());
+ }
}
\ No newline at end of file
16 years, 9 months