JCS-166 Specify interface to listen on
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
index a2c2c36..e33e82e 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
@@ -25,8 +25,10 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Map;
@@ -76,9 +78,6 @@
/** Configuration attributes */
private ITCPLateralCacheAttributes tcpLateralCacheAttributes;
- /** Listening port */
- private int port;
-
/** The processor. We should probably use an event queue here. */
private ExecutorService pooledExecutor;
@@ -149,16 +148,31 @@
{
try
{
- this.port = getTcpLateralCacheAttributes().getTcpListenerPort();
+ int port = getTcpLateralCacheAttributes().getTcpListenerPort();
+ String host = getTcpLateralCacheAttributes().getTcpListenerHost();
pooledExecutor = Executors.newCachedThreadPool(
new DaemonThreadFactory("JCS-LateralTCPListener-"));
terminated = new AtomicBoolean(false);
shutdown = new AtomicBoolean(false);
- log.info( "Listening on port {0}", port );
+ ServerSocket serverSocket;
+ if (host != null && host.length() > 0)
+ {
+ log.info( "Listening on {0}:{1}", host, port );
+ // Resolve host name
+ InetAddress inetAddress = InetAddress.getByName(host);
+ //Bind the SocketAddress with inetAddress and port
+ SocketAddress endPoint = new InetSocketAddress(inetAddress, port);
- ServerSocket serverSocket = new ServerSocket( port );
+ serverSocket = new ServerSocket();
+ serverSocket.bind(endPoint);
+ }
+ else
+ {
+ log.info( "Listening on port {0}", port );
+ serverSocket = new ServerSocket( port );
+ }
serverSocket.setSoTimeout( acceptTimeOut );
receiver = new ListenerThread(serverSocket);
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java
index cac34db..26ed199 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java
@@ -66,9 +66,12 @@
/** used to identify the service that this manager will be operating on */
private String tcpServer = "";
- /** The pot */
+ /** The port */
private int tcpListenerPort = 0;
+ /** The host */
+ private String tcpListenerHost = "";
+
/** udp discovery for tcp server */
private String udpDiscoveryAddr = DEFAULT_UDP_DISCOVERY_ADDRESS;
@@ -163,6 +166,29 @@
}
/**
+ * Sets the tcpListenerHost attribute of the ILateralCacheAttributes object
+ * <p>
+ * @param val
+ * The new tcpListenerHost value
+ */
+ @Override
+ public void setTcpListenerHost( String val )
+ {
+ this.tcpListenerHost = val;
+ }
+
+ /**
+ * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object
+ * <p>
+ * @return The tcpListenerHost value
+ */
+ @Override
+ public String getTcpListenerHost()
+ {
+ return this.tcpListenerHost;
+ }
+
+ /**
* Can setup UDP Discovery. This only works for TCp laterals right now. It allows TCP laterals
* to find each other by broadcasting to a multicast port.
* <p>
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java
index db12800..7ffd3bc 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java
@@ -77,6 +77,21 @@
int getTcpListenerPort();
/**
+ * Sets the tcpListenerHost attribute of the ILateralCacheAttributes object
+ * <p>
+ * @param val
+ * The new tcpListenerHost value
+ */
+ void setTcpListenerHost( String val );
+
+ /**
+ * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object
+ * <p>
+ * @return The tcpListenerHost value
+ */
+ String getTcpListenerHost();
+
+ /**
* Can setup UDP Discovery. This only works for TCp laterals right now. It
* allows TCP laterals to find each other by broadcasting to a multicast
* port.
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java
index 4f34f80..631ff0a 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java
@@ -1,5 +1,22 @@
package org.apache.commons.jcs.auxiliary.lateral.socket.tcp;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.jcs.JCS;
+import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes;
+import org.apache.commons.jcs.auxiliary.lateral.LateralCommand;
+import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor;
+import org.apache.commons.jcs.engine.CacheElement;
+import org.apache.commons.jcs.engine.behavior.ICacheElement;
+import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
+import org.apache.commons.jcs.engine.control.CompositeCache;
+import org.apache.commons.jcs.engine.control.CompositeCacheManager;
+import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
+import org.apache.commons.jcs.engine.control.group.GroupAttrName;
+import org.apache.commons.jcs.engine.control.group.GroupId;
+import org.apache.commons.jcs.utils.timing.SleepUtil;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,22 +37,6 @@
*/
import junit.framework.TestCase;
-import org.apache.commons.jcs.JCS;
-import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes;
-import org.apache.commons.jcs.auxiliary.lateral.LateralCommand;
-import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor;
-import org.apache.commons.jcs.engine.CacheElement;
-import org.apache.commons.jcs.engine.behavior.ICacheElement;
-import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
-import org.apache.commons.jcs.engine.control.CompositeCache;
-import org.apache.commons.jcs.engine.control.CompositeCacheManager;
-import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
-import org.apache.commons.jcs.engine.control.group.GroupAttrName;
-import org.apache.commons.jcs.engine.control.group.GroupId;
-import org.apache.commons.jcs.utils.timing.SleepUtil;
-
-import java.util.Map;
-import java.util.Set;
/**
* Basic unit tests for the sending and receiving portions of the lateral cache.
@@ -313,7 +314,7 @@
// VERIFY
assertNotNull( "Result should not be null.", result );
- assertEquals( "Didn't get the correct object", "key", result.toArray()[0] );
+ assertEquals( "Didn't get the correct object", "key", result.iterator().next().attrName );
}
/**
diff --git a/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf b/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf
index 230c1cc..bf6e941 100644
--- a/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf
+++ b/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf
@@ -38,6 +38,7 @@
jcs.auxiliary.LTCP.attributes=org.apache.commons.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes
jcs.auxiliary.LTCP.attributes.TcpServers=localhost:1111
jcs.auxiliary.LTCP.attributes.TcpListenerPort=1110
+jcs.auxiliary.LTCP.attributes.TcpListenerHost=localhost
jcs.auxiliary.LTCP.attributes.AllowGet=false
jcs.auxiliary.LTCP.attributes.SocketTimeOut=1001
jcs.auxiliary.LTCP.attributes.OpenTimeOut=2002
diff --git a/xdocs/LateralTCPProperties.xml b/xdocs/LateralTCPProperties.xml
index 9444d8e..7c265a0 100644
--- a/xdocs/LateralTCPProperties.xml
+++ b/xdocs/LateralTCPProperties.xml
@@ -37,6 +37,12 @@
<td>N</td>
<td>none</td>
</tr>
+ <tr>
+ <td>TcpListenerHost</td>
+ <td> This is the host this cache should listen on (for multi-homed hosts).</td>
+ <td>Y</td>
+ <td>listen on all interfaces</td>
+ </tr>
<tr>
<td>TcpListenerPort</td>
<td> This is the port this cache should listen on.</td>