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 2016/02/07 12:29:17 UTC

svn commit: r1728947 - in /commons/proper/jcs/trunk: commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/ commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/ src/changes/

Author: tv
Date: Sun Feb  7 11:29:17 2016
New Revision: 1728947

URL: http://svn.apache.org/viewvc?rev=1728947&view=rev
Log:
JCS-78 Fix: RemoteCacheStartupServlet can't start with config outside classpath

Modified:
    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/RemoteCacheStartupServlet.java
    commons/proper/jcs/trunk/src/changes/changes.xml

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=1728947&r1=1728946&r2=1728947&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 Feb  7 11:29:17 2016
@@ -19,11 +19,14 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.URL;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
@@ -35,12 +38,13 @@ 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.
+ * This class provides some basic utilities for doing things such as starting
+ * the registry properly.
  */
 public class RemoteUtils
 {
     /** The logger. */
-    private static final Log log = LogFactory.getLog( RemoteUtils.class );
+    private static final Log log = LogFactory.getLog(RemoteUtils.class);
 
     /** No instances please. */
     private RemoteUtils()
@@ -51,53 +55,54 @@ public class RemoteUtils
     /**
      * Creates and exports a registry on the specified port of the local host.
      * <p>
+     *
      * @param port
      * @return the registry
      */
-    public static Registry createRegistry( int port )
+    public static Registry createRegistry(int port)
     {
-    	Registry registry = null;
+        Registry registry = null;
 
-//        if ( log.isInfoEnabled() )
-//        {
-//            log.info( "createRegistry> Setting security manager" );
-//        }
-//
-//        System.setSecurityManager( new RMISecurityManager() );
+        // if ( log.isInfoEnabled() )
+        // {
+        // log.info( "createRegistry> Setting security manager" );
+        // }
+        //
+        // System.setSecurityManager( new RMISecurityManager() );
 
-        if ( port < 1024 )
+        if (port < 1024)
         {
-            if ( log.isWarnEnabled() )
+            if (log.isWarnEnabled())
             {
-                log.warn( "createRegistry> 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;
         }
 
         try
         {
-            registry = LocateRegistry.createRegistry( port );
+            registry = LocateRegistry.createRegistry(port);
             log.info("createRegistry> Created the registry on port " + port);
         }
-        catch ( RemoteException e )
+        catch (RemoteException e)
         {
-            log.warn( "createRegistry> Problem creating registry. It may already be started. " + e.getMessage() );
+            log.warn("createRegistry> Problem creating registry. It may already be started. " + e.getMessage());
         }
-        catch ( Throwable t )
+        catch (Throwable t)
         {
-            log.error( "createRegistry> Problem creating registry.", t );
+            log.error("createRegistry> Problem creating registry.", t);
         }
 
         if (registry == null)
         {
-        	try
-        	{
-            	registry = LocateRegistry.getRegistry( port );
-			}
-        	catch (RemoteException e)
-        	{
-                log.error( "createRegistry> Problem getting a registry reference.", e );
-			}
+            try
+            {
+                registry = LocateRegistry.getRegistry(port);
+            }
+            catch (RemoteException e)
+            {
+                log.error("createRegistry> Problem getting a registry reference.", e);
+            }
         }
 
         return registry;
@@ -105,44 +110,61 @@ public class RemoteUtils
 
     /**
      * Loads properties for the named props file.
+     * First tries class path, then file, then URL
      * <p>
+     *
      * @param propFile
      * @return The properties object for the file
      * @throws IOException
      */
-    public static Properties loadProps( String propFile )
-        throws IOException
+    public static Properties loadProps(String propFile)
+            throws IOException
     {
-        InputStream is = RemoteUtils.class.getResourceAsStream( propFile );
+        InputStream is = RemoteUtils.class.getResourceAsStream(propFile);
+
+        if (null == is) // not found in class path
+        {
+            if (new File(propFile).exists())
+            {
+                // file found
+                is = new FileInputStream(propFile);
+            }
+            else
+            {
+                // try URL
+                is = new URL(propFile).openStream();
+            }
+        }
+
         Properties props = new Properties();
         try
         {
-            props.load( is );
-            if ( log.isDebugEnabled() )
+            props.load(is);
+            if (log.isDebugEnabled())
             {
-                log.debug( "props.size=" + props.size() );
+                log.debug("props.size=" + props.size());
             }
 
-            if ( log.isDebugEnabled() )
+            if (log.isDebugEnabled())
             {
                 Enumeration<Object> en = props.keys();
                 StringBuilder buf = new StringBuilder();
-                while ( en.hasMoreElements() )
+                while (en.hasMoreElements())
                 {
                     String key = (String) en.nextElement();
-                    buf.append( "\n" + key + " = " + props.getProperty( key ) );
+                    buf.append("\n" + key + " = " + props.getProperty(key));
                 }
-                log.debug( buf.toString() );
+                log.debug(buf.toString());
             }
 
         }
-        catch ( Exception ex )
+        catch (Exception ex)
         {
-            log.error( "Error loading remote properties, for file name [" + propFile + "]", ex );
+            log.error("Error loading remote properties, for file name [" + propFile + "]", ex);
         }
         finally
         {
-            if ( is != null )
+            if (is != null)
             {
                 is.close();
             }
@@ -151,56 +173,58 @@ public class RemoteUtils
     }
 
     /**
-     * Configure a custom socket factory to set the timeout value. This sets the global socket
-     * factory. It's used only if a custom factory is not configured for the specific object.
+     * Configure a custom socket factory to set the timeout value. This sets the
+     * global socket factory. It's used only if a custom factory is not
+     * configured for the specific object.
      * <p>
+     *
      * @param timeoutMillis
      */
-    public static void configureGlobalCustomSocketFactory( final int timeoutMillis )
+    public static void configureGlobalCustomSocketFactory(final int timeoutMillis)
     {
         try
         {
             // Don't set a socket factory if the setting is -1
-            if ( timeoutMillis > 0 )
+            if (timeoutMillis > 0)
             {
-                if ( log.isInfoEnabled() )
+                if (log.isInfoEnabled())
                 {
-                    log.info( "RmiSocketFactoryTimeoutMillis [" + timeoutMillis + "]. "
-                        + " Configuring a custom socket factory." );
+                    log.info("RmiSocketFactoryTimeoutMillis [" + timeoutMillis + "]. "
+                            + " Configuring a custom socket factory.");
                 }
 
                 // use this socket factory to add a timeout.
-                RMISocketFactory.setSocketFactory( new RMISocketFactory()
+                RMISocketFactory.setSocketFactory(new RMISocketFactory()
                 {
                     @Override
-                    public Socket createSocket( String host, int port )
-                        throws IOException
+                    public Socket createSocket(String host, int port)
+                            throws IOException
                     {
                         Socket socket = new Socket();
-                        socket.setSoTimeout( timeoutMillis );
-                        socket.setSoLinger( false, 0 );
-                        socket.connect( new InetSocketAddress( host, port ), timeoutMillis );
+                        socket.setSoTimeout(timeoutMillis);
+                        socket.setSoLinger(false, 0);
+                        socket.connect(new InetSocketAddress(host, port), timeoutMillis);
                         return socket;
                     }
 
                     @Override
-                    public ServerSocket createServerSocket( int port )
-                        throws IOException
+                    public ServerSocket createServerSocket(int port)
+                            throws IOException
                     {
-                        return new ServerSocket( port );
+                        return new ServerSocket(port);
                     }
-                } );
+                });
             }
         }
-        catch ( IOException e )
+        catch (IOException e)
         {
             // Only try to do it once. Otherwise we
             // Generate errors for each region on construction.
             RMISocketFactory factoryInUse = RMISocketFactory.getSocketFactory();
-            if ( factoryInUse != null && !factoryInUse.getClass().getName().startsWith( "org.apache.commons.jcs" ) )
+            if (factoryInUse != null && !factoryInUse.getClass().getName().startsWith("org.apache.commons.jcs"))
             {
-                log.info( "Could not create new custom socket factory. " + e.getMessage() + " Factory in use = "
-                    + RMISocketFactory.getSocketFactory() );
+                log.info("Could not create new custom socket factory. " + e.getMessage() + " Factory in use = "
+                        + RMISocketFactory.getSocketFactory());
             }
         }
     }
@@ -208,8 +232,10 @@ public class RemoteUtils
     /**
      * Get the naming url used for RMI registration
      *
-     * @param location the remote location
-     * @param serviceName the remote service name
+     * @param location
+     *            the remote location
+     * @param serviceName
+     *            the remote service name
      * @return the URL for RMI lookup
      */
     public static String getNamingURL(final RemoteLocation location, final String serviceName)
@@ -220,14 +246,18 @@ public class RemoteUtils
     /**
      * Get the naming url used for RMI registration
      *
-     * @param registryHost the remote host
-     * @param registryPort the remote port
-     * @param serviceName the remote service name
+     * @param registryHost
+     *            the remote host
+     * @param registryPort
+     *            the remote port
+     * @param serviceName
+     *            the remote service name
      * @return the URL for RMI lookup
      */
     public static String getNamingURL(final String registryHost, final int registryPort, final String serviceName)
     {
-        if (registryHost.contains(":")) { // TODO improve this check? See also JCS-133
+        if (registryHost.contains(":"))
+        { // TODO improve this check? See also JCS-133
             return "//[" + registryHost.replaceFirst("%", "%25") + "]:" + registryPort + "/" + serviceName;
         }
         final String registryURL = "//" + registryHost + ":" + registryPort + "/" + serviceName;

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java?rev=1728947&r1=1728946&r2=1728947&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java Sun Feb  7 11:29:17 2016
@@ -19,25 +19,28 @@ package org.apache.commons.jcs.auxiliary
  * under the License.
  */
 
-import org.apache.commons.jcs.access.exception.CacheException;
-import org.apache.commons.jcs.engine.control.CompositeCacheManager;
-import org.apache.commons.jcs.utils.net.HostNameUtil;
-import org.apache.commons.jcs.utils.props.PropertyLoader;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.UnknownHostException;
+import java.util.Properties;
 
+import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.UnknownHostException;
-import java.util.Properties;
+
+import org.apache.commons.jcs.access.exception.CacheException;
+import org.apache.commons.jcs.auxiliary.remote.RemoteUtils;
+import org.apache.commons.jcs.engine.control.CompositeCacheManager;
+import org.apache.commons.jcs.utils.net.HostNameUtil;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
- * 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&gt;
@@ -54,111 +57,109 @@ import java.util.Properties;
         &lt;url-pattern&gt;/jcs&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
  * </code>
+ *
  * @author Aaron Smuts
  */
 public class RemoteCacheStartupServlet
-    extends HttpServlet
+        extends HttpServlet
 {
     /** Don't change */
     private static final long serialVersionUID = 1L;
 
     /** The logger */
-    private static final Log log = LogFactory.getLog( RemoteCacheStartupServlet.class );
+    private static final Log log = LogFactory.getLog(RemoteCacheStartupServlet.class);
 
-    /** The default port to start the registry on.  */
+    /** The default port to start the registry on. */
     private static final int DEFAULT_REGISTRY_PORT = 1101;
 
     /** properties file name */
-    private static final String DEFAULT_PROPS_FILE_NAME = "cache";
-
-    /** properties file Suffix */
-    private static final String DEFAULT_PROPS_FILE_SUFFIX = "ccf";
+    private static final String DEFAULT_PROPS_FILE_NAME = "/cache.ccf";
 
     /** properties file name, must set prior to calling get instance */
-    private final String propsFileName = DEFAULT_PROPS_FILE_NAME;
+    private String propsFileName = DEFAULT_PROPS_FILE_NAME;
 
-    /** properties file name, must set prior to calling get instance */
-    private final String fullPropsFileName = DEFAULT_PROPS_FILE_NAME + "." + DEFAULT_PROPS_FILE_SUFFIX;
+    /** Configuration properties */
+    private int registryPort = DEFAULT_REGISTRY_PORT;
+
+    /** Configuration properties */
+    private String registryHost = null;
 
     /**
      * 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
      */
     @Override
     public void init()
-        throws ServletException
+            throws ServletException
     {
         super.init();
-        // TODO load from props file or get as init param or get from jndi, or
-        // all three
-        int registryPort = DEFAULT_REGISTRY_PORT;
 
-        Properties props = PropertyLoader.loadProperties( propsFileName );
-        if ( props != null )
-        {
-            String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
+        loadInitParams();
+        Properties props = loadPropertiesFromFile();
 
+        if (registryHost == null)
+        {
+            // we will always use the local machine for the registry
             try
             {
-                registryPort = Integer.parseInt( portS );
+                registryHost = HostNameUtil.getLocalHostAddress();
             }
-            catch ( NumberFormatException e )
+            catch (UnknownHostException e)
             {
-                log.error( "Problem converting port to an int.", e );
+                log.error("Could not get local address to use for the registry!", e);
             }
         }
 
-        // we will always use the local machine for the registry
-        String registryHost;
-        try
+        if (log.isDebugEnabled())
         {
-            registryHost = HostNameUtil.getLocalHostAddress();
+            log.debug("registryHost = [" + registryHost + "]");
+        }
 
-            if ( log.isDebugEnabled() )
-            {
-                log.debug( "registryHost = [" + registryHost + "]" );
-            }
+        if ("localhost".equals(registryHost) || "127.0.0.1".equals(registryHost))
+        {
+            log.warn("The local address [" + registryHost
+                    + "] is INVALID.  Other machines must be able to use the address to reach this server.");
+        }
 
-            if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
+        try
+        {
+            if (props == null)
             {
-                log.warn( "The local address [" + registryHost
-                    + "] is INVALID.  Other machines must be able to use the address to reach this server." );
+                RemoteCacheServerFactory.startup(registryHost, registryPort, propsFileName);
             }
-
-            try
+            else
             {
-                RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + fullPropsFileName );
-                if ( log.isInfoEnabled() )
-                {
-                    log.info( "Remote JCS Server started with properties from " + fullPropsFileName );
-                }
+                RemoteCacheServerFactory.startup(registryHost, registryPort, props, propsFileName);
             }
-            catch ( IOException e )
+            if (log.isInfoEnabled())
             {
-                log.error( "Problem starting remote cache server.", e );
+                log.info("Remote JCS Server started with properties from " + propsFileName);
             }
         }
-        catch ( UnknownHostException e )
+        catch (IOException e)
         {
-            log.error( "Could not get local address to use for the registry!", e );
+            log.error("Problem starting remote cache server.", e);
         }
     }
 
     /**
      * It just dumps the stats.
      * <p>
+     *
      * @param request
      * @param response
      * @throws ServletException
      * @throws IOException
      */
     @Override
-    protected void service( HttpServletRequest request, HttpServletResponse response )
-        throws ServletException, IOException
+    protected void service(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException
     {
         String stats = "";
 
@@ -171,9 +172,9 @@ public class RemoteCacheStartupServlet
             throw new ServletException(e);
         }
 
-        if ( log.isInfoEnabled() )
+        if (log.isInfoEnabled())
         {
-            log.info( stats );
+            log.info(stats);
         }
 
         try
@@ -185,12 +186,12 @@ public class RemoteCacheStartupServlet
                 response.setCharacterEncoding(characterEncoding);
             }
             OutputStream os = response.getOutputStream();
-            os.write( stats.getBytes(characterEncoding) );
+            os.write(stats.getBytes(characterEncoding));
             os.close();
         }
-        catch ( IOException e )
+        catch (IOException e)
         {
-            log.error( "Problem writing response.", e );
+            log.error("Problem writing response.", e);
         }
     }
 
@@ -202,7 +203,16 @@ public class RemoteCacheStartupServlet
     {
         super.destroy();
 
-        log.info( "Shutting down remote cache " );
+        log.info("Shutting down remote cache ");
+
+        try
+        {
+            RemoteCacheServerFactory.shutdownImpl(registryHost, registryPort);
+        }
+        catch (IOException e)
+        {
+            log.error("Problem shutting down.", e);
+        }
 
         try
         {
@@ -213,4 +223,71 @@ public class RemoteCacheStartupServlet
             log.error("Could not retrieve cache manager instance", e);
         }
     }
+
+    /**
+     * Load configuration values from config file if possible
+     */
+    private Properties loadPropertiesFromFile()
+    {
+        Properties props = null;
+
+        try
+        {
+            props = RemoteUtils.loadProps(propsFileName);
+            if (props != null)
+            {
+                registryHost = props.getProperty("registry.host", registryHost);
+                String portS = props.getProperty("registry.port", String.valueOf(registryPort));
+                setRegistryPort(portS);
+            }
+        }
+        catch (IOException e)
+        {
+            log.error("Problem loading props.", e);
+        }
+
+        return props;
+    }
+
+    /**
+     * Load configuration values from init params if possible
+     */
+    private void loadInitParams()
+    {
+        ServletConfig config = getServletConfig();
+        String _propsFileName = config.getInitParameter("propsFileName");
+        if (null != _propsFileName)
+        {
+            this.propsFileName = _propsFileName;
+        }
+        String _registryHost = config.getInitParameter("registryHost");
+        if (null != _registryHost)
+        {
+            this.registryHost = _registryHost;
+        }
+        String regPortString = config.getInitParameter("registryPort");
+        if (null != regPortString)
+        {
+            setRegistryPort(regPortString);
+        }
+    }
+
+    /**
+     * Set registry port from string If the string cannot be parsed, the default
+     * value is used
+     *
+     * @param portS
+     */
+    private void setRegistryPort(String portS)
+    {
+        try
+        {
+            this.registryPort = Integer.parseInt(portS);
+        }
+        catch (NumberFormatException e)
+        {
+            log.error("Problem converting port to an int.", e);
+            this.registryPort = DEFAULT_REGISTRY_PORT;
+        }
+    }
 }

Modified: commons/proper/jcs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/changes/changes.xml?rev=1728947&r1=1728946&r2=1728947&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/changes/changes.xml (original)
+++ commons/proper/jcs/trunk/src/changes/changes.xml Sun Feb  7 11:29:17 2016
@@ -20,6 +20,9 @@
 	</properties>
 	<body>
         <release version="2.0" date="unreleased" description="JDK 1.6 based major release">
+            <action issue="JCS-78" dev="tv" type="fix" due-to="Marko Stipanov">
+                Fix: RemoteCacheStartupServlet can't start with config outside classpath
+            </action>
             <action dev="tv" type="add" due-to="Wiktor Niesiobedzki">
                 Add verification of block disk cache key file.
             </action>