Author: tv
Date: Sun Jun 29 13:58:27 2014
New Revision: 1606505
URL: http://svn.apache.org/r1606505
Log:
Some more code cleanup, remove duplicate code etc.
Modified:
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheListener.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/main/java/org/apache/commons/jcs/auxiliary/remote/server/RegistryKeepAliveRunner.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.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/AbstractRemoteCacheListener.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java?rev=1606505&r1=1606504&r2=1606505&view=diff
==============================================================================
---
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
(original)
+++
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
Sun Jun 29 13:58:27 2014
@@ -52,9 +52,6 @@ public abstract class AbstractRemoteCach
/** The cached name of the local host. The remote server gets this for
logging purposes. */
private static String localHostName = null;
- /** Has this client been shutdown. */
- boolean disposed = false; // TODO not used in this class; move to
RemoteCacheListener?
-
/**
* The cache manager used to put items in different regions. This is set
lazily and should not
* be sent to the remote server.
Modified:
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheListener.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheListener.java?rev=1606505&r1=1606504&r2=1606505&view=diff
==============================================================================
---
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheListener.java
(original)
+++
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteCacheListener.java
Sun Jun 29 13:58:27 2014
@@ -48,6 +48,9 @@ public class RemoteCacheListener<K, V>
/** The logger */
private static final Log log = LogFactory.getLog(
RemoteCacheListener.class );
+ /** Has this client been shutdown. */
+ private boolean disposed = false;
+
/**
* Only need one since it does work for all regions, just reference by
multiple region names.
* <p>
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=1606505&r1=1606504&r2=1606505&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
Sun Jun 29 13:58:27 2014
@@ -53,10 +53,8 @@ public class RemoteUtils
* <p>
* @param port
* @return the registry
- * @throws RemoteException
*/
public static Registry createRegistry( int port )
- throws RemoteException
{
Registry registry = null;
@@ -206,4 +204,18 @@ public class RemoteUtils
}
}
}
+
+ /**
+ * Get the naming url used for RMI registration
+ *
+ * @param registryHost
+ * @param registryPort
+ * @param serviceName
+ * @return
+ */
+ public static String getNamingURL(final String registryHost, final int
registryPort, final String serviceName)
+ {
+ final String registryURL = "//" + registryHost + ":" + registryPort +
"/" + serviceName;
+ return registryURL;
+ }
}
Modified:
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RegistryKeepAliveRunner.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RegistryKeepAliveRunner.java?rev=1606505&r1=1606504&r2=1606505&view=diff
==============================================================================
---
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RegistryKeepAliveRunner.java
(original)
+++
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RegistryKeepAliveRunner.java
Sun Jun 29 13:58:27 2014
@@ -19,16 +19,17 @@ package org.apache.commons.jcs.auxiliary
* under the License.
*/
+import org.apache.commons.jcs.auxiliary.remote.RemoteUtils;
import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.rmi.Naming;
import java.rmi.RemoteException;
-import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
/**
- * This class tries to keep the resitry alive. If if is able to create a
registry, it will also
+ * This class tries to keep the registry alive. If if is able to create a
registry, it will also
* rebind the remote cache server.
*/
public class RegistryKeepAliveRunner
@@ -37,15 +38,12 @@ public class RegistryKeepAliveRunner
/** The logger */
private static final Log log = LogFactory.getLog(
RegistryKeepAliveRunner.class );
- /** Hostname of the registry */
- private String registryHost;
+ /** The URL of the service to look for. */
+ private String namingURL;
/** the port on which to start the registry */
private int registryPort;
- /** The name of the service to look for. */
- private String serviceName;
-
/** An optional event logger */
private ICacheEventLogger cacheEventLogger;
@@ -56,9 +54,8 @@ public class RegistryKeepAliveRunner
*/
public RegistryKeepAliveRunner( String registryHost, int registryPort,
String serviceName )
{
- this.registryHost = registryHost;
+ this.namingURL = RemoteUtils.getNamingURL(registryHost, registryPort,
serviceName);
this.registryPort = registryPort;
- this.serviceName = serviceName;
}
/**
@@ -78,17 +75,16 @@ public class RegistryKeepAliveRunner
*/
protected void checkAndRestoreIfNeeded()
{
- String registry = "//" + registryHost + ":" + registryPort + "/" +
serviceName;
if ( log.isDebugEnabled() )
{
- log.debug( "looking up server " + registry );
+ log.debug( "looking up server " + namingURL );
}
try
{
- Object obj = Naming.lookup( registry );
+ Object obj = Naming.lookup( namingURL );
// Successful connection to the remote server.
- String message = "RMI registry looks fine. Found [" + obj + "] in
registry [" + registry + "]";
+ String message = "RMI registry looks fine. Found [" + obj + "] in
registry [" + namingURL + "]";
if ( cacheEventLogger != null )
{
cacheEventLogger.logApplicationEvent(
"RegistryKeepAliveRunner", "Naming.lookup", message );
@@ -102,14 +98,14 @@ public class RegistryKeepAliveRunner
catch ( Exception ex )
{
// Failed to connect to the remote server.
- String message = "Problem finding server at [" + registry
+ String message = "Problem finding server at [" + namingURL
+ "]. Will attempt to start registry and rebind.";
log.error( message, ex );
if ( cacheEventLogger != null )
{
cacheEventLogger.logError( "RegistryKeepAliveRunner",
"Naming.lookup", message + ":" + ex.getMessage() );
}
- createAndRegister( registry );
+ createAndRegister( namingURL );
}
}
@@ -121,7 +117,6 @@ public class RegistryKeepAliveRunner
protected void createAndRegister( String registry )
{
createReqistry( registry );
-
registerServer( registry );
}
@@ -132,22 +127,19 @@ public class RegistryKeepAliveRunner
*/
protected void createReqistry( String registry )
{
- try
+ Registry reg = RemoteUtils.createRegistry(registryPort);
+
+ if ( cacheEventLogger != null )
{
- LocateRegistry.createRegistry( registryPort );
- String message = "Successfully created registry [" + registry +
"].";
- if ( cacheEventLogger != null )
+ if (reg != null)
{
- cacheEventLogger.logApplicationEvent(
"RegistryKeepAliveRunner", "createRegistry", message );
+ cacheEventLogger.logApplicationEvent(
"RegistryKeepAliveRunner", "createRegistry",
+ "Successfully created registry [" + registry + "]." );
}
- }
- catch ( RemoteException e )
- {
- String message = "Could not start registry [" + registry + "].";
- log.error( message, e );
- if ( cacheEventLogger != null )
+ else
{
- cacheEventLogger.logError( "RegistryKeepAliveRunner",
"createRegistry", message + ":" + e.getMessage() );
+ cacheEventLogger.logError( "RegistryKeepAliveRunner",
"createRegistry",
+ "Could not start registry [" + registry + "]." );
}
}
}
@@ -162,7 +154,7 @@ public class RegistryKeepAliveRunner
try
{
// try to rebind anyway
- RemoteCacheServerFactory.registerServer( registryHost,
registryPort, serviceName );
+ RemoteCacheServerFactory.registerServer( registry,
RemoteCacheServerFactory.getRemoteCacheServer() );
String message = "Successfully rebound server to registry [" +
registry + "].";
if ( cacheEventLogger != null )
{
Modified:
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java?rev=1606505&r1=1606504&r2=1606505&view=diff
==============================================================================
---
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
(original)
+++
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
Sun Jun 29 13:58:27 2014
@@ -35,6 +35,7 @@ import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
+import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
@@ -180,6 +181,7 @@ public class RemoteCacheServerFactory
{
remoteCacheServer = new RemoteCacheServer<Serializable,
Serializable>( rcsa );
}
+
remoteCacheServer.setCacheEventLogger( cacheEventLogger );
// START THE REGISTRY
@@ -189,7 +191,7 @@ public class RemoteCacheServerFactory
}
// REGISTER THE SERVER
- registerServer( host, port, serviceName );
+ registerServer( RemoteUtils.getNamingURL(host, port, serviceName),
remoteCacheServer );
// KEEP THE REGISTRY ALIVE
if ( rcsa.isUseRegistryKeepAlive() )
@@ -261,31 +263,31 @@ public class RemoteCacheServerFactory
* Registers the server with the registry. I broke this off because we
might want to have code
* that will restart a dead registry. It will need to rebind the server.
* <p>
- * @param host
- * @param port
- * @param serviceName
+ * @param namingURL
+ * @param server
* @throws RemoteException
*/
- protected static void registerServer( String host, int port, String
serviceName )
+ protected static void registerServer( String namingURL, Remote server )
throws RemoteException
{
- if ( remoteCacheServer == null )
+ if ( server == null )
{
- throw new RemoteException( "Cannot register the server until it is
created. Please start the server first." );
+ throw new RemoteException( "Cannot register the server until it is
created." );
}
if ( log.isInfoEnabled() )
{
- log.info( "Binding server to " + host + ":" + port + " with the
name " + serviceName );
+ log.info( "Binding server to " + namingURL );
}
+
try
{
- Naming.rebind( "//" + host + ":" + port + "/" + serviceName,
remoteCacheServer );
+ Naming.rebind( namingURL, server );
}
catch ( MalformedURLException ex )
{
// impossible case.
- throw new IllegalArgumentException( ex.getMessage() + "; host=" +
host + ", port=" + port );
+ throw new IllegalArgumentException( ex.getMessage() + "; url=" +
namingURL );
}
}
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=1606505&r1=1606504&r2=1606505&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
Sun Jun 29 13:58:27 2014
@@ -21,7 +21,7 @@ package org.apache.commons.jcs.auxiliary
import junit.framework.TestCase;
-import java.rmi.RemoteException;
+import java.rmi.registry.Registry;
/**
* Simple tests for remote utils. It is difficult to verify most of the things
is does.
@@ -38,13 +38,7 @@ public class RemoteUtilsUnitTest
*/
public void testCreateRegistry()
{
- try
- {
- RemoteUtils.createRegistry( 1102 );
- }
- catch ( RemoteException e )
- {
- fail("RemoteException unexpected " + e);
- }
+ Registry registry = RemoteUtils.createRegistry( 1102 );
+ assertNotNull("Registry should not be null", registry);
}
}