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 2014/04/28 13:29:56 UTC

svn commit: r1590606 - in /commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote: RemoteUtils.java server/RemoteCacheServerFactory.java server/RemoteCacheStartupServlet.java

Author: tv
Date: Mon Apr 28 11:29:55 2014
New Revision: 1590606

URL: http://svn.apache.org/r1590606
Log:
Remove duplicate code

Added:
    commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java
      - copied, changed from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java
    commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
      - copied, changed from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
    commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
      - copied, changed from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java

Copied: commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java (from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java)
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java?p2=commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java&p1=commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java&r1=1587508&r2=1590606&rev=1590606&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java (original)
+++ commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/RemoteUtils.java Mon Apr 28 11:29:55 2014
@@ -24,7 +24,6 @@ import java.io.InputStream;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.rmi.RMISecurityManager;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
@@ -53,33 +52,57 @@ public class RemoteUtils
      * Creates and exports a registry on the specified port of the local host.
      * <p>
      * @param port
-     * @return the port the registry was started on
+     * @return the registry
      * @throws RemoteException
      */
-    public static int createRegistry( int port )
+    public static Registry createRegistry( int port )
         throws RemoteException
     {
-        if ( log.isInfoEnabled() )
-        {
-            log.info( "createRegistry> setting security manager" );
-        }
-        System.setSecurityManager( new RMISecurityManager() );
+    	Registry registry = null;
+
+//        if ( log.isInfoEnabled() )
+//        {
+//            log.info( "createRegistry> Setting security manager" );
+//        }
+//
+//        System.setSecurityManager( new RMISecurityManager() );
 
         if ( port < 1024 )
         {
-            if ( log.isInfoEnabled() )
+            if ( log.isWarnEnabled() )
             {
-                log.info( "Port chosen was less than 1024, will use default [" + Registry.REGISTRY_PORT + "] instead." );
+                log.warn( "createRegistry> Port chosen was less than 1024, will use default [" + Registry.REGISTRY_PORT + "] instead." );
             }
             port = Registry.REGISTRY_PORT;
         }
 
-        if ( log.isInfoEnabled() )
+        try
+        {
+            registry = LocateRegistry.createRegistry( port );
+            log.info("createRegistry> Created the registry on port " + port);
+        }
+        catch ( RemoteException e )
+        {
+            log.warn( "createRegistry> Problem creating registry. It may already be started. " + e.getMessage() );
+        }
+        catch ( Throwable t )
+        {
+            log.error( "createRegistry> Problem creating registry.", t );
+        }
+
+        if (registry == null)
         {
-            log.info( "createRegistry> creating registry on port [" + port + "]" );
+        	try
+        	{
+            	registry = LocateRegistry.getRegistry( port );
+			}
+        	catch (RemoteException e)
+        	{
+                log.error( "createRegistry> Problem getting a registry reference.", e );
+			}
         }
-        LocateRegistry.createRegistry( port );
-        return port;
+
+        return registry;
     }
 
     /**

Copied: commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java (from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java)
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java?p2=commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java&p1=commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java&r1=1587508&r2=1590606&rev=1590606&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java (original)
+++ commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java Mon Apr 28 11:29:55 2014
@@ -25,9 +25,9 @@ import java.net.MalformedURLException;
 import java.rmi.Naming;
 import java.rmi.NotBoundException;
 import java.rmi.RemoteException;
-import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
 import java.rmi.server.RMISocketFactory;
+import java.rmi.server.UnicastRemoteObject;
 import java.util.Properties;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -63,6 +63,9 @@ public class RemoteCacheServerFactory
     /** Executes the registry keep alive. */
     private static ScheduledExecutorService keepAliveDaemon;
 
+    /** A reference to the registry. */
+    private static Registry registry = null;
+
     /** Constructor for the RemoteCacheServerFactory object. */
     private RemoteCacheServerFactory()
     {
@@ -180,7 +183,10 @@ public class RemoteCacheServerFactory
             remoteCacheServer.setCacheEventLogger( cacheEventLogger );
 
             // START THE REGISTRY
-            startTheRegistry( port, rcsa );
+            if (rcsa.isStartRegistry())
+            {
+            	registry = RemoteUtils.createRegistry(port);
+            }
 
             // REGISTER THE SERVER
             registerServer( host, port, serviceName );
@@ -251,32 +257,6 @@ public class RemoteCacheServerFactory
     }
 
     /**
-     * Starts the registry if needed
-     * <p>
-     * @param registryPort
-     * @param rcsa
-     */
-    private static void startTheRegistry( int registryPort, RemoteCacheServerAttributes rcsa )
-    {
-        if ( rcsa.isStartRegistry() )
-        {
-            try
-            {
-                LocateRegistry.createRegistry( registryPort );
-                log.info("Created the registry on port " + registryPort);
-            }
-            catch ( RemoteException e )
-            {
-                log.warn( "Problem creating registry.  It may already be started. " + e.getMessage() );
-            }
-            catch ( Throwable t )
-            {
-                log.error( "Problem creating registry.", t );
-            }
-        }
-    }
-
-    /**
      * 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>
@@ -290,9 +270,7 @@ public class RemoteCacheServerFactory
     {
         if ( remoteCacheServer == null )
         {
-            String message = "Cannot register the server until it is created.  Please start the server first.";
-            log.error( message );
-            throw new RemoteException( message );
+            throw new RemoteException( "Cannot register the server until it is created. Please start the server first." );
         }
 
         if ( log.isInfoEnabled() )
@@ -422,16 +400,20 @@ public class RemoteCacheServerFactory
             }
             remoteCacheServer.release();
             remoteCacheServer = null;
-            // TODO: safer exit ?
-            try
+
+            // Shut down keepalive scheduler
+            if ( keepAliveDaemon != null )
             {
-                Thread.sleep( 2000 );
+                keepAliveDaemon.shutdownNow();
+                keepAliveDaemon = null;
             }
-            catch ( InterruptedException ex )
+
+            // Try to release registry
+            if (registry != null)
             {
-                // swallow
+            	UnicastRemoteObject.unexportObject(registry, true);
+            	registry = null;
             }
-            System.exit( 0 );
         }
     }
 
@@ -529,7 +511,7 @@ public class RemoteCacheServerFactory
         if ( host == null || host.trim().equals( "" ) || host.trim().equals( "localhost" ) )
         {
             log.debug( "main> creating registry on the localhost" );
-            port = RemoteUtils.createRegistry( port );
+            RemoteUtils.createRegistry( port );
         }
         log.debug( "main> starting up RemoteCacheServer" );
         RemoteCacheServerFactory.startup( host, port, args.length > 0 ? args[0] : null );

Copied: commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java (from r1587508, commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java)
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java?p2=commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java&p1=commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java&r1=1587508&r2=1590606&rev=1590606&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java (original)
+++ commons/proper/jcs/trunk/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java Mon Apr 28 11:29:55 2014
@@ -22,8 +22,6 @@ package org.apache.commons.jcs.auxiliary
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.UnknownHostException;
-import java.rmi.RemoteException;
-import java.rmi.registry.LocateRegistry;
 import java.util.Properties;
 
 import javax.servlet.ServletException;
@@ -100,30 +98,19 @@ public class RemoteCacheStartupServlet
         // all three
         int registryPort = DEFAULT_REGISTRY_PORT;
 
-        try
+        Properties props = PropertyLoader.loadProperties( propsFileName );
+        if ( props != null )
         {
-            Properties props = PropertyLoader.loadProperties( propsFileName );
-            if ( props != null )
-            {
-                String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
+            String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
 
-                try
-                {
-                    registryPort = Integer.parseInt( portS );
-                }
-                catch ( NumberFormatException e )
-                {
-                    log.error( "Problem converting port to an int.", e );
-                }
+            try
+            {
+                registryPort = Integer.parseInt( portS );
+            }
+            catch ( NumberFormatException e )
+            {
+                log.error( "Problem converting port to an int.", e );
             }
-        }
-        catch ( Exception e )
-        {
-            log.error( "Problem loading props.", e );
-        }
-        catch ( Throwable t )
-        {
-            log.error( "Problem loading props.", t );
         }
 
         // we will always use the local machine for the registry
@@ -145,19 +132,6 @@ public class RemoteCacheStartupServlet
 
             try
             {
-                LocateRegistry.createRegistry( registryPort );
-            }
-            catch ( RemoteException e )
-            {
-                log.error( "Problem creating registry.  It may already be started. " + e.getMessage() );
-            }
-            catch ( Throwable t )
-            {
-                log.error( "Problem creating registry.", t );
-            }
-
-            try
-            {
                 RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + fullPropsFileName );
                 if ( log.isInfoEnabled() )
                 {
@@ -168,11 +142,6 @@ public class RemoteCacheStartupServlet
             {
                 log.error( "Problem starting remote cache server.", e );
             }
-
-            catch ( Throwable t )
-            {
-                log.error( "Problem starting remote cache server.", t );
-            }
         }
         catch ( UnknownHostException e )
         {