You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2015/04/06 20:11:37 UTC

svn commit: r1671625 - in /commons/proper/jcs/trunk/commons-jcs-core/src: main/java/org/apache/commons/jcs/auxiliary/remote/ test/java/org/apache/commons/jcs/auxiliary/remote/

Author: tv
Date: Mon Apr  6 18:11:37 2015
New Revision: 1671625

URL: http://svn.apache.org/r1671625
Log:
Better parameter parsing

Modified:
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFactory.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFailoverRunner.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java
    commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtilsUnitTest.java

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFactory.java?rev=1671625&r1=1671624&r2=1671625&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFactory.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFactory.java Mon Apr  6 18:11:37 2015
@@ -19,6 +19,10 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.StringTokenizer;
+
 import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheFactory;
 import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
@@ -28,10 +32,6 @@ import org.apache.commons.jcs.engine.beh
 import org.apache.commons.jcs.engine.behavior.IElementSerializer;
 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.StringTokenizer;
-
 /**
  * The RemoteCacheFactory creates remote caches for the cache hub. It returns a no wait facade which
  * is a wrapper around a no wait. The no wait object is either an active connection to a remote
@@ -76,10 +76,10 @@ public class RemoteCacheFactory
             // not necessary if a failover list is defined
             // REGISTER PRIMARY LISTENER
             // if it is a primary
-            boolean primayDefined = false;
+            boolean primaryDefined = false;
             if ( rca.getRemoteHost() != null )
             {
-                primayDefined = true;
+                primaryDefined = true;
 
                 failovers.add( rca.getRemoteHost() + ":" + rca.getRemotePort() );
 
@@ -95,20 +95,19 @@ public class RemoteCacheFactory
             {
                 StringTokenizer fit = new StringTokenizer( failoverList, "," );
                 int fCnt = 0;
-                while ( fit.hasMoreElements() )
+                while ( fit.hasMoreTokens() )
                 {
                     fCnt++;
 
-                    String server = (String) fit.nextElement();
+                    String server = fit.nextToken();
                     failovers.add( server );
 
-                    rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
-                    rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
+                    RemoteUtils.parseServerAndPort(server, rca);
                     RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr, cacheEventLogger,
                                                                              elementSerializer );
                     // add a listener if there are none, need to tell rca what
                     // number it is at
-                    if ( ( !primayDefined && fCnt == 1 ) || noWaits.size() <= 0 )
+                    if ( ( !primaryDefined && fCnt == 1 ) || noWaits.size() <= 0 )
                     {
                         ICache<K, V> ic = rcm.getCache( rca );
                         noWaits.add( ic );

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFailoverRunner.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFailoverRunner.java?rev=1671625&r1=1671624&r2=1671625&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFailoverRunner.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheFailoverRunner.java Mon Apr  6 18:11:37 2015
@@ -19,6 +19,8 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
+import java.io.IOException;
+
 import org.apache.commons.jcs.engine.CacheStatus;
 import org.apache.commons.jcs.engine.behavior.ICache;
 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
@@ -27,8 +29,6 @@ import org.apache.commons.jcs.engine.log
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import java.io.IOException;
-
 /**
  * The RemoteCacheFailoverRunner tries to establish a connection with a failover
  * server, if any are defined. Once a failover connection is made, it will
@@ -202,8 +202,7 @@ public class RemoteCacheFailoverRunner<K
                     try
                     {
                         rca = (RemoteCacheAttributes) facade.getRemoteCacheAttributes().copy();
-                        rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
-                        rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
+                        RemoteUtils.parseServerAndPort(server, rca);
                         RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr, cacheEventLogger, elementSerializer );
 
                         if ( log.isDebugEnabled() )
@@ -347,8 +346,7 @@ public class RemoteCacheFailoverRunner<K
         try
         {
             RemoteCacheAttributes rca = (RemoteCacheAttributes) facade.getRemoteCacheAttributes().copy();
-            rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
-            rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
+            RemoteUtils.parseServerAndPort(server, rca);
             RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr, cacheEventLogger, elementSerializer );
 
             // add a listener if there are none, need to tell rca what number it
@@ -388,9 +386,7 @@ public class RemoteCacheFailoverRunner<K
                                 // create attributes that reflect the
                                 // previous failed over configuration.
                                 RemoteCacheAttributes rcaOld = (RemoteCacheAttributes) facade.getRemoteCacheAttributes().copy();
-                                rcaOld.setRemoteHost( serverOld.substring( 0, serverOld.indexOf( ":" ) ) );
-                                rcaOld.setRemotePort( Integer.parseInt( serverOld.substring( serverOld
-                                    .indexOf( ":" ) + 1 ) ) );
+                                RemoteUtils.parseServerAndPort(serverOld, rcaOld);
                                 RemoteCacheManager rcmOld = RemoteCacheManager.getInstance( rcaOld, cacheMgr, cacheEventLogger, elementSerializer );
 
                                 if ( rcmOld != null )

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java?rev=1671625&r1=1671624&r2=1671625&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java Mon Apr  6 18:11:37 2015
@@ -19,9 +19,6 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetSocketAddress;
@@ -33,6 +30,11 @@ import java.rmi.registry.Registry;
 import java.rmi.server.RMISocketFactory;
 import java.util.Enumeration;
 import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * This class provides some basic utilities for doing things such as starting the registry properly.
@@ -221,4 +223,31 @@ public class RemoteUtils
         final String registryURL = "//" + registryHost + ":" + registryPort + "/" + serviceName;
         return registryURL;
     }
+
+    /** Pattern for parsing server:port */
+    private static final Pattern SERVER_COLON_PORT = Pattern.compile("(\\S+)\\s*:\\s*(\\d+)");
+
+    /**
+     * Parse remote server and port from the string representation server:port and store them in
+     * the RemoteCacheAttributes
+     *
+     * @param registryHost
+     * @param registryPort
+     * @param serviceName
+     * @return
+     */
+    public static void parseServerAndPort(final String server, final RemoteCacheAttributes rca)
+    {
+        Matcher match = SERVER_COLON_PORT.matcher(server);
+
+        if (match.find() && match.groupCount() == 2)
+        {
+            rca.setRemoteHost( match.group(1) );
+            rca.setRemotePort( Integer.parseInt( match.group(2) ) );
+        }
+        else
+        {
+            throw new RuntimeException("Invalid server descriptor: " + server);
+        }
+    }
 }

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtilsUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtilsUnitTest.java?rev=1671625&r1=1671624&r2=1671625&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtilsUnitTest.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtilsUnitTest.java Mon Apr  6 18:11:37 2015
@@ -19,10 +19,10 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
-import junit.framework.TestCase;
-
 import java.rmi.registry.Registry;
 
+import junit.framework.TestCase;
+
 /**
  * Simple tests for remote utils. It is difficult to verify most of the things is does.
  *<p>
@@ -42,10 +42,33 @@ public class RemoteUtilsUnitTest
         assertNotNull("Registry should not be null", registry);
     }
 
-    public void testgetNamingURL()
+    public void testGetNamingURL()
     {
         assertEquals("//host:1/servicename", RemoteUtils.getNamingURL("host",1,"servicename"));
         assertEquals("//127.0.0.1:2/servicename", RemoteUtils.getNamingURL("127.0.0.1",2,"servicename"));
         assertEquals("//[0:0:0:0:0:0:0:1%251]:3/servicename", RemoteUtils.getNamingURL("0:0:0:0:0:0:0:1%1",3,"servicename"));
     }
+
+    public void testParseServerAndPort()
+    {
+        RemoteCacheAttributes rca = new RemoteCacheAttributes();
+
+        RemoteUtils.parseServerAndPort("server1:1234", rca);
+        assertEquals("server1", rca.getRemoteHost());
+        assertEquals(1234, rca.getRemotePort());
+
+        RemoteUtils.parseServerAndPort("  server2  :  4567  ", rca);
+        assertEquals("server2", rca.getRemoteHost());
+        assertEquals(4567, rca.getRemotePort());
+
+        try
+        {
+            RemoteUtils.parseServerAndPort("server2  :  port", rca);
+            fail("Parsing should not succeed");
+        }
+        catch (Exception e)
+        {
+            // expected
+        }
+    }
 }