Author: asmuts Date: Wed Aug 27 09:45:46 2008 New Revision: 689520 URL: http://svn.apache.org/viewvc?rev=689520&view=rev Log: Added more event logging. Formatting and javadocs. And JCS-40 host address resolution work-around.
https://issues.apache.org/jira/browse/JCS-40 Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/HostNameUtilUnitTest.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/ZombieLateralCacheService.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPService.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryMessage.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryReceiver.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryService.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheListener.java jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java jakarta/jcs/trunk/src/java/org/apache/jcs/utils/net/HostNameUtil.java jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryUnitTest.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/ZombieLateralCacheService.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/ZombieLateralCacheService.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/ZombieLateralCacheService.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/ZombieLateralCacheService.java Wed Aug 27 09:45:46 2008 @@ -28,54 +28,54 @@ import org.apache.jcs.engine.behavior.ICacheElement; /** - * The ZombieLateralCacheService is used as a facade when the lateral is not - * available. It balks when the lateral is in error. When lateral service is - * restored, this is replaced by a live facade. - * + * The ZombieLateralCacheService is used as a facade when the lateral is not available. It balks + * when the lateral is in error. When lateral service is restored, this is replaced by a live + * facade. */ public class ZombieLateralCacheService extends ZombieCacheService implements ILateralCacheService { - - /* - * (non-Javadoc) - * - * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#update(org.apache.jcs.engine.behavior.ICacheElement, - * long) + /** + * Balks + * <p> + * @param item + * @param listenerId */ public void update( ICacheElement item, long listenerId ) { // zombies have no inner life } - /* - * (non-Javadoc) - * - * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#remove(java.lang.String, - * java.io.Serializable, long) + /** + * Balks + * <p> + * @param cacheName + * @param key + * @param listenerId */ public void remove( String cacheName, Serializable key, long listenerId ) { // zombies have no inner life } - /* - * (non-Javadoc) - * - * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#removeAll(java.lang.String, - * long) + /** + * Balks + * <p> + * @param cacheName + * @param listenerId */ public void removeAll( String cacheName, long listenerId ) { // zombies have no inner life } - /* - * (non-Javadoc) - * - * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#getGroupKeys(java.lang.String, - * java.lang.String) + /** + * Balks + * <p> + * @param cacheName + * @param groupName + * @return empty set */ public Set getGroupKeys( String cacheName, String groupName ) { Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java Wed Aug 27 09:45:46 2008 @@ -19,9 +19,7 @@ * under the License. */ -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; @@ -32,7 +30,6 @@ import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor; import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; import org.apache.jcs.auxiliary.lateral.socket.tcp.utils.SocketOpener; -import org.apache.jcs.engine.CacheElement; import org.apache.jcs.engine.behavior.ICacheElement; /** @@ -409,36 +406,4 @@ { return remoteHost; } - - /** - * This is a Testing Method. It should be moved to a unit test. - * @param args - */ - public static void main( String args[] ) - { - try - { - LateralTCPSender lur = null; - // new LateralTCPSender( "localhost", 1111 ); - - // process user input till done - boolean notDone = true; - String message = null; - // wait to dispose - BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); - - while ( notDone ) - { - System.out.println( "enter mesage:" ); - message = br.readLine(); - CacheElement ce = new CacheElement( "test", "test", message ); - LateralElementDescriptor led = new LateralElementDescriptor( ce ); - lur.send( led ); - } - } - catch ( Exception e ) - { - System.out.println( e.toString() ); - } - } } Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPService.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPService.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPService.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/LateralTCPService.java Wed Aug 27 09:45:46 2008 @@ -175,7 +175,8 @@ } /** - * Does nothing. + * Does nothing. <p. + * @throws IOException */ public void release() throws IOException @@ -215,9 +216,11 @@ // nothing needs to be done } - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheService#get(java.lang.String, java.io.Serializable) + /** + * @param cacheName + * @param key + * @return ICacheElement if found. + * @throws IOException */ public ICacheElement get( String cacheName, Serializable key ) throws IOException @@ -288,9 +291,9 @@ return null; } - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheService#removeAll(java.lang.String) + /** + * @param cacheName + * @throws IOException */ public void removeAll( String cacheName ) throws IOException @@ -298,10 +301,10 @@ removeAll( cacheName, getListenerId() ); } - /* - * (non-Javadoc) - * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#removeAll(java.lang.String, - * long) + /** + * @param cacheName + * @param requesterId + * @throws IOException */ public void removeAll( String cacheName, long requesterId ) throws IOException @@ -347,10 +350,10 @@ // the connection is not registered, the udp service is // is not registered. - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheObserver#addCacheListener(java.lang.String, - * org.apache.jcs.engine.behavior.ICacheListener) + /** + * @param cacheName + * @param obj + * @throws IOException */ public void addCacheListener( String cacheName, ICacheListener obj ) throws IOException @@ -358,9 +361,9 @@ // Empty } - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheObserver#addCacheListener(org.apache.jcs.engine.behavior.ICacheListener) + /** + * @param obj + * @throws IOException */ public void addCacheListener( ICacheListener obj ) throws IOException @@ -368,10 +371,10 @@ // Empty } - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheObserver#removeCacheListener(java.lang.String, - * org.apache.jcs.engine.behavior.ICacheListener) + /** + * @param cacheName + * @param obj + * @throws IOException */ public void removeCacheListener( String cacheName, ICacheListener obj ) throws IOException @@ -379,9 +382,9 @@ // Empty } - /* - * (non-Javadoc) - * @see org.apache.jcs.engine.behavior.ICacheObserver#removeCacheListener(org.apache.jcs.engine.behavior.ICacheListener) + /** + * @param obj + * @throws IOException */ public void removeCacheListener( ICacheListener obj ) throws IOException Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java Wed Aug 27 09:45:46 2008 @@ -27,15 +27,12 @@ * This interface defines functions that are particular to the TCP Lateral Cache * plugin. It extends the generic LateralCacheAttributes interface which in turn * extends the AuxiliaryCache interface. - * - * @author Aaron Smuts - * */ public class TCPLateralCacheAttributes extends LateralCacheAttributes implements ITCPLateralCacheAttributes { - + /** Don't change. */ private static final long serialVersionUID = 1077889204513905220L; private static final String DEFAULT_UDP_DISCOVERY_ADDRESS = "228.5.6.7"; Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryMessage.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryMessage.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryMessage.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryMessage.java Wed Aug 27 09:45:46 2008 @@ -24,25 +24,22 @@ import java.util.Iterator; /** - * - * @author Aaron Smuts - * + * The message sent by the discovery mechanism. */ public class UDPDiscoveryMessage implements Serializable { - + /** Don't change */ private static final long serialVersionUID = -5332377899560951794L; /** - * This is the periodic broadcast of a servers location. This type of - * message is also sent in response to a REQUEST_BROADCAST. + * This is the periodic broadcast of a servers location. This type of message is also sent in + * response to a REQUEST_BROADCAST. */ public static final int PASSIVE_BROADCAST = 0; /** - * This asks recipients to broadcast their location. This is used on - * startup. + * This asks recipients to broadcast their location. This is used on startup. */ public static final int REQUEST_BROADCAST = 1; @@ -58,8 +55,7 @@ private ArrayList cacheNames = new ArrayList(); /** - * @param port - * The port to set. + * @param port The port to set. */ public void setPort( int port ) { @@ -75,8 +71,7 @@ } /** - * @param host - * The host to set. + * @param host The host to set. */ public void setHost( String host ) { @@ -92,8 +87,7 @@ } /** - * @param requesterId - * The requesterId to set. + * @param requesterId The requesterId to set. */ public void setRequesterId( long requesterId ) { @@ -109,8 +103,7 @@ } /** - * @param messageType - * The messageType to set. + * @param messageType The messageType to set. */ public void setMessageType( int messageType ) { @@ -126,8 +119,7 @@ } /** - * @param cacheNames - * The cacheNames to set. + * @param cacheNames The cacheNames to set. */ public void setCacheNames( ArrayList cacheNames ) { @@ -142,10 +134,8 @@ return cacheNames; } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() + /** + * @return debugging string */ public String toString() { @@ -164,5 +154,4 @@ } return buf.toString(); } - } Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryReceiver.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryReceiver.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryReceiver.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryReceiver.java Wed Aug 27 09:45:46 2008 @@ -46,16 +46,17 @@ import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; import EDU.oswego.cs.dl.util.concurrent.ThreadFactory; -/** - * Receives UDP Discovery messages. - */ +/** Receives UDP Discovery messages. */ public class UDPDiscoveryReceiver implements Runnable, IShutdownObserver { + /** The log factory */ private final static Log log = LogFactory.getLog( UDPDiscoveryReceiver.class ); + /** buffer */ private final byte[] m_buffer = new byte[65536]; + /** The socket used for communication. */ private MulticastSocket m_socket; // todo consider using the threadpool manager to @@ -280,10 +281,8 @@ this.message = message; } - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() + /** + * Process the message. */ public void run() { @@ -384,15 +383,15 @@ /** * Allows us to set the daemon status on the executor threads - * @author aaronsm */ class MyThreadFactory implements ThreadFactory { - /* - * (non-Javadoc) - * - * @see EDU.oswego.cs.dl.util.concurrent.ThreadFactory#newThread(java.lang.Runnable) + /** + * Sets the thread to daemon. + * <p> + * @param runner + * @return a daemon thread */ public Thread newThread( Runnable runner ) { @@ -403,11 +402,7 @@ } } - /* - * (non-Javadoc) - * - * @see org.apache.jcs.engine.behavior.ShutdownObserver#shutdown() - */ + /** Shuts down the socket. */ public void shutdown() { try Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java Wed Aug 27 09:45:46 2008 @@ -25,23 +25,21 @@ import org.apache.commons.logging.LogFactory; /** - * Used to periodically broadcast our location to other caches that might be - * listening. - * - * @author Aaron Smuts - * + * Used to periodically broadcast our location to other caches that might be listening. */ public class UDPDiscoverySenderThread implements Runnable { + /** The logger. */ private final static Log log = LogFactory.getLog( UDPDiscoverySenderThread.class ); - // the UDP multicast port + /** the UDP multicast port */ private String discoveryAddress = ""; + /** The port */ private int discoveryPort = 0; - // the host and port we listen on for TCP socket connections + /** the host and port we listen on for TCP socket connections */ private String myHostName = null; private int myPort = 0; @@ -49,8 +47,7 @@ private ArrayList cacheNames = new ArrayList(); /** - * @param cacheNames - * The cacheNames to set. + * @param cacheNames The cacheNames to set. */ protected void setCacheNames( ArrayList cacheNames ) { @@ -72,22 +69,15 @@ /** * Constructs the sender with the port to tell others to connect to. * <p> - * On construction the sender will request that the other caches let it know - * their addresses. - * - * @param discoveryAddress - * host to broadcast to - * @param discoveryPort - * port to broadcast to - * @param myHostName - * host name we can be found at - * @param myPort - * port we are listening on - * @param cacheNames - * List of strings of the names of the regiond participating. + * On construction the sender will request that the other caches let it know their addresses. + * @param discoveryAddress host to broadcast to + * @param discoveryPort port to broadcast to + * @param myHostName host name we can be found at + * @param myPort port we are listening on + * @param cacheNames List of strings of the names of the regiond participating. */ public UDPDiscoverySenderThread( String discoveryAddress, int discoveryPort, String myHostName, int myPort, - ArrayList cacheNames ) + ArrayList cacheNames ) { this.discoveryAddress = discoveryAddress; this.discoveryPort = discoveryPort; @@ -135,10 +125,8 @@ } } - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() + /** + * Send a message. */ public void run() { Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryService.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryService.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryService.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryService.java Wed Aug 27 09:45:46 2008 @@ -19,7 +19,6 @@ * under the License. */ -import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; @@ -37,6 +36,7 @@ import org.apache.jcs.engine.behavior.IShutdownObservable; import org.apache.jcs.engine.behavior.IShutdownObserver; import org.apache.jcs.engine.logging.behavior.ICacheEventLogger; +import org.apache.jcs.utils.net.HostNameUtil; import EDU.oswego.cs.dl.util.concurrent.ClockDaemon; import EDU.oswego.cs.dl.util.concurrent.ThreadFactory; @@ -52,12 +52,13 @@ public class UDPDiscoveryService implements IShutdownObserver { + /** The logger */ private final static Log log = LogFactory.getLog( UDPDiscoveryService.class ); - // The background broadcaster. + /** The background broadcaster. */ private static ClockDaemon senderDaemon; - // thread that listens for messages + /** thread that listens for messages */ private Thread udpReceiverThread; // the runanble that the receiver thread runs @@ -110,11 +111,7 @@ try { // todo, you should be able to set this - hostAddress = InetAddress.getLocalHost().getHostAddress(); - if ( log.isDebugEnabled() ) - { - log.debug( "hostAddress = [" + hostAddress + "]" ); - } + hostAddress = HostNameUtil.getLocalHostAddress(); } catch ( UnknownHostException e1 ) { Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheListener.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheListener.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheListener.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheListener.java Wed Aug 27 09:45:46 2008 @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.Serializable; +import java.net.UnknownHostException; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; @@ -53,13 +54,13 @@ private final static Log log = LogFactory.getLog( RemoteCacheListener.class ); - private static String localHostName = HostNameUtil.getLocalHostAddress(); + private static String localHostName = null; boolean disposed = false; /** - * The cache manager used to put items in differnt regions. This is set lazily and should not be - * sent to the remote server. + * The cache manager used to put items in different regions. This is set lazily and should not + * be sent to the remote server. */ protected transient ICompositeCacheManager cacheMgr; @@ -349,11 +350,22 @@ * This is for debugging. It allows the remote server to log the address of clients. * <p> * @return String - * @throws IOException + * @throws IOException */ - public String getLocalHostAddress() + public synchronized String getLocalHostAddress() throws IOException { + if ( localHostName == null ) + { + try + { + localHostName = HostNameUtil.getLocalHostAddress(); + } + catch ( UnknownHostException uhe ) + { + localHostName = "unknown"; + } + } return localHostName; } Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java Wed Aug 27 09:45:46 2008 @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.OutputStream; -import java.net.InetAddress; import java.net.UnknownHostException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; @@ -35,14 +34,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.jcs.engine.control.CompositeCacheManager; +import org.apache.jcs.utils.net.HostNameUtil; import org.apache.jcs.utils.props.PropertyLoader; /** - * This servlet can be used to startup the JCS remote cache. It is easy to - * deploy the remote server in a tomcat base. This give you an easy way to - * monitor its activity. + * This servlet can be used to startup the JCS remote cache. It is easy to deploy the remote server + * in a tomcat base. This give you an easy way to monitor its activity. * <p> - * * <code> * <servlet> <servlet-name>JCSRemoteCacheStartupServlet</servlet-name> @@ -58,10 +56,7 @@ <url-pattern>/jcs</url-pattern> </servlet-mapping> * </code> - * - * * @author Aaron Smuts - * */ public class RemoteCacheStartupServlet extends HttpServlet @@ -77,10 +72,10 @@ /** * Starts the registry and then tries to bind to it. * <p> - * Gets the port from a props file. Uses the local host name for the registry - * host. Tries to start the registry, ignoring failure. Starts the server. + * Gets the port from a props file. Uses the local host name for the registry host. Tries to + * start the registry, ignoring failure. Starts the server. * <p> - * @throws ServletException + * @throws ServletException */ public void init() throws ServletException @@ -120,7 +115,7 @@ String registryHost; try { - registryHost = InetAddress.getLocalHost().getHostAddress(); + registryHost = HostNameUtil.getLocalHostAddress(); if ( log.isDebugEnabled() ) { Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/net/HostNameUtil.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/net/HostNameUtil.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/utils/net/HostNameUtil.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/net/HostNameUtil.java Wed Aug 27 09:45:46 2008 @@ -20,7 +20,9 @@ */ import java.net.InetAddress; +import java.net.NetworkInterface; import java.net.UnknownHostException; +import java.util.Enumeration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -37,25 +39,107 @@ /** * Gets the address for the local machine. * <p> - * @return InetAddress.getLocalHost().getHostAddress(), or unknown if there - * is an error. + * @return InetAddress.getLocalHost().getHostAddress() + * @throws UnknownHostException */ - public static String getLocalHostAddress() + public static String getLocalHostAddress() throws UnknownHostException { - String hostAddress = "unknown"; try { - // todo, you should be able to set this - hostAddress = InetAddress.getLocalHost().getHostAddress(); + String hostAddress = getLocalHostLANAddress().getHostAddress(); ; if ( log.isDebugEnabled() ) { log.debug( "hostAddress = [" + hostAddress + "]" ); } + return hostAddress; } catch ( UnknownHostException e1 ) { log.error( "Couldn't get localhost address", e1 ); + throw e1; + } + } + + /** + * Returns an <code>InetAddress</code> object encapsulating what is most likely the machine's + * LAN IP address. + * <p> + * This method is intended for use as a replacement of JDK method + * <code>InetAddress.getLocalHost</code>, because that method is ambiguous on Linux systems. + * Linux systems enumerate the loopback network interface the same way as regular LAN network + * interfaces, but the JDK <code>InetAddress.getLocalHost</code> method does not specify the + * algorithm used to select the address returned under such circumstances, and will often return + * the loopback address, which is not valid for network communication. Details <a + * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037">here</a>. + * <p> + * This method will scan all IP addresses on all network interfaces on the host machine to + * determine the IP address most likely to be the machine's LAN address. If the machine has + * multiple IP addresses, this method will prefer a site-local IP address (e.g. 192.168.x.x or + * 10.10.x.x, usually IPv4) if the machine has one (and will return the first site-local address + * if the machine has more than one), but if the machine does not hold a site-local address, + * this method will return simply the first non-loopback address found (IPv4 or IPv6). <p/> If + * this method cannot find a non-loopback address using this selection algorithm, it will fall + * back to calling and returning the result of JDK method <code>InetAddress.getLocalHost</code>. + * <p> + * <a href="http://issues.apache.org/jira/browse/JCS-40">JIR ISSUE JCS-40</a> + * <p> + * @throws UnknownHostException If the LAN address of the machine cannot be found. + */ + private static InetAddress getLocalHostLANAddress() + throws UnknownHostException + { + try + { + InetAddress candidateAddress = null; + // Iterate all NICs (network interface cards)... + for ( Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) + { + NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); + // Iterate all IP addresses assigned to each card... + for ( Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) + { + InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); + if ( !inetAddr.isLoopbackAddress() ) + { + if ( inetAddr.isSiteLocalAddress() ) + { + // Found non-loopback site-local address. Return it immediately... + return inetAddr; + } + else if ( candidateAddress == null ) + { + // Found non-loopback address, but not necessarily site-local. + // Store it as a candidate to be returned if site-local address is not subsequently found... + candidateAddress = inetAddr; + // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates, + // only the first. For subsequent iterations, candidate will be non-null. + } + } + } + } + if ( candidateAddress != null ) + { + // We did not find a site-local address, but we found some other non-loopback address. + // Server might have a non-site-local address assigned to its NIC (or it might be running + // IPv6 which deprecates the "site-local" concept). + // Return this non-loopback candidate address... + return candidateAddress; + } + // At this point, we did not find a non-loopback address. + // Fall back to returning whatever InetAddress.getLocalHost() returns... + InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); + if ( jdkSuppliedAddress == null ) + { + throw new UnknownHostException( "The JDK InetAddress.getLocalHost() method unexpectedly returned null." ); + } + return jdkSuppliedAddress; + } + catch ( Exception e ) + { + UnknownHostException unknownHostException = new UnknownHostException( "Failed to determine LAN address: " + + e ); + unknownHostException.initCause( e ); + throw unknownHostException; } - return hostAddress; } } Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryUnitTest.java?rev=689520&r1=689519&r2=689520&view=diff ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryUnitTest.java (original) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoveryUnitTest.java Wed Aug 27 09:45:46 2008 @@ -36,14 +36,11 @@ import org.apache.jcs.utils.serialization.StandardSerializer; /** - * - * @author Aaron Smuts - * + * Unit tests for discovery */ public class UDPDiscoveryUnitTest extends TestCase { - /** * Test setup */ @@ -65,15 +62,14 @@ * <p> * 6. create a sender * <p> - * 7.create more names than we have no wait facades for the only one that - * gets added should be testCache1 + * 7.create more names than we have no wait facades for the only one that gets added should be + * testCache1 * <p> * 8. send 10 messages * <p> * 9. check to see that we got 10 messages * <p> * 10. check to see if the testCache1 facade got a nowait. - * * @throws Exception */ public void testSimpleUDPDiscovery() @@ -87,7 +83,9 @@ ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance(); // create the service - UDPDiscoveryService service = new UDPDiscoveryService( lac.getUdpDiscoveryAddr(), lac.getUdpDiscoveryPort(), lac.getTcpListenerPort(), cacheMgr, new MockCacheEventLogger(), new StandardSerializer() ); + UDPDiscoveryService service = new UDPDiscoveryService( lac.getUdpDiscoveryAddr(), lac.getUdpDiscoveryPort(), + lac.getTcpListenerPort(), cacheMgr, + new MockCacheEventLogger(), new StandardSerializer() ); service.setTcpLateralCacheAttributes( lac ); // create a no wait facade for the service @@ -102,7 +100,8 @@ service.addNoWaitFacade( lcnwf, "testCache1" ); // create a receiver with the service - UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, "228.5.6.7", 6789, cacheMgr, new MockCacheEventLogger(), new StandardSerializer() ); + UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, "228.5.6.7", 6789, cacheMgr, + new MockCacheEventLogger(), new StandardSerializer() ); Thread t = new Thread( receiver ); t.start(); @@ -152,7 +151,6 @@ /** * Verify that the config does not throw any errors. - * * @throws Exception */ public void testUDPDiscoveryConfig() @@ -201,6 +199,5 @@ // try adding the same one again lcnwf.addNoWait( noWait2 ); assertEquals( "Facade should still have 2 no waits", 2, lcnwf.noWaits.length ); - } } Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/HostNameUtilUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/HostNameUtilUnitTest.java?rev=689520&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/HostNameUtilUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/net/HostNameUtilUnitTest.java Wed Aug 27 09:45:46 2008 @@ -0,0 +1,25 @@ +package org.apache.jcs.utils.net; + +import java.net.UnknownHostException; + +import junit.framework.TestCase; + +/** Tests for the host name util. */ +public class HostNameUtilUnitTest + extends TestCase +{ + /** + * It's nearly impossible to unit test the getLocalHostLANAddress method. + * <p> + * @throws UnknownHostException + */ + public void testGetLocalHostAddress_Simple() throws UnknownHostException + { + // DO WORK + String result = HostNameUtil.getLocalHostAddress(); + + // VERIFY + //System.out.print( result ); + assertNotNull( "Should have a host address.", result ); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]