Author: markt
Date: Sat Dec 28 14:06:07 2013
New Revision: 1553826

URL: http://svn.apache.org/r1553826
Log:
Move control of caching and linking options from the Context to the 
WebResourceRoot to enable them to by changed while the web application is 
running.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml
    tomcat/trunk/java/org/apache/catalina/webresources/Cache.java
    tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
    tomcat/trunk/java/org/apache/catalina/webresources/mbeans-descriptors.xml
    tomcat/trunk/test/org/apache/naming/resources/TestWarDirContext.java
    tomcat/trunk/webapps/docs/config/context.xml
    tomcat/trunk/webapps/docs/config/resources.xml

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Sat Dec 28 
14:06:07 2013
@@ -644,36 +644,6 @@ public class StandardContext extends Con
     private String namingContextName = null;
 
 
-    /**
-     * Caching allowed flag.
-     */
-    private boolean cachingAllowed = true;
-
-
-    /**
-     * Allow linking.
-     */
-    protected boolean allowLinking = false;
-
-
-    /**
-     * Cache max size in KB.
-     */
-    protected int cacheMaxSize = 10240; // 10 MB
-
-
-    /**
-     * Cache object max size in KB.
-     */
-    protected int cacheObjectMaxSize = 512; // 512K
-
-
-    /**
-     * Cache TTL in ms.
-     */
-    protected int cacheTTL = 5000;
-
-
     private WebResourceRoot resources;
     private final ReadWriteLock resourcesLock = new ReentrantReadWriteLock();
 
@@ -1027,37 +997,6 @@ public class StandardContext extends Con
 
 
     /**
-     * Is caching allowed ?
-     */
-    public boolean isCachingAllowed() {
-        return cachingAllowed;
-    }
-
-
-    /**
-     * Set caching allowed flag.
-     */
-    public void setCachingAllowed(boolean cachingAllowed) {
-        this.cachingAllowed = cachingAllowed;
-    }
-
-
-    /**
-     * Set allow linking.
-     */
-    public void setAllowLinking(boolean allowLinking) {
-        this.allowLinking = allowLinking;
-    }
-
-
-    /**
-     * Is linking allowed.
-     */
-    public boolean isAllowLinking() {
-        return allowLinking;
-    }
-
-    /**
      * Set to <code>true</code> to allow requests mapped to servlets that
      * do not explicitly declare @MultipartConfig or have
      * &lt;multipart-config&gt; specified in web.xml to parse
@@ -1110,54 +1049,6 @@ public class StandardContext extends Con
     }
 
     /**
-     * Set cache TTL.
-     */
-    public void setCacheTTL(int cacheTTL) {
-        this.cacheTTL = cacheTTL;
-    }
-
-
-    /**
-     * Get cache TTL.
-     */
-    public int getCacheTTL() {
-        return cacheTTL;
-    }
-
-
-    /**
-     * Return the maximum size of the cache in KB.
-     */
-    public int getCacheMaxSize() {
-        return cacheMaxSize;
-    }
-
-
-    /**
-     * Set the maximum size of the cache in KB.
-     */
-    public void setCacheMaxSize(int cacheMaxSize) {
-        this.cacheMaxSize = cacheMaxSize;
-    }
-
-
-    /**
-     * Return the maximum size of objects to be cached in KB.
-     */
-    public int getCacheObjectMaxSize() {
-        return cacheObjectMaxSize;
-    }
-
-
-    /**
-     * Set the maximum size of objects to be placed the cache in KB.
-     */
-    public void setCacheObjectMaxSize(int cacheObjectMaxSize) {
-        this.cacheObjectMaxSize = cacheObjectMaxSize;
-    }
-
-
-    /**
      * Add a ServletContainerInitializer instance to this web application.
      *
      * @param sci       The instance to add
@@ -4907,13 +4798,6 @@ public class StandardContext extends Con
      */
     public void resourcesStart() throws LifecycleException {
 
-        resources.setAllowLinking(isAllowLinking());
-
-        resources.setCachingAllowed(isCachingAllowed());
-        resources.setCacheTtl(getCacheTTL());
-        resources.setCacheMaxSize(getCacheMaxSize());
-        resources.setCacheObjectMaxSize(getCacheObjectMaxSize());
-
         // May have been started (but not fully configured) in init() so no 
need
         // to start the resources if they are already available
         if (!resources.getState().isAvailable()) {

Modified: tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml (original)
+++ tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml Sat Dec 
28 14:06:07 2013
@@ -65,11 +65,6 @@
          type="org.apache.catalina.core.StandardContext"
          className="org.apache.catalina.mbeans.ContextMBean">
 
-    <attribute name="allowLinking"
-               description="Allow symlinking to outside the webapp root 
directory, if the webapp is an exploded directory"
-               is="true"
-               type="boolean"/>
-
     <attribute name="altDDName"
                description="The alternate deployment descriptor name."
                type="java.lang.String" />
@@ -83,23 +78,6 @@
                type="java.lang.String"
                writeable="false"/>
 
-    <attribute name="cacheMaxSize"
-               description="Maximum cache size in KB"
-               type="int"/>
-
-    <attribute name="cacheObjectMaxSize"
-               description="Maximum cached object size in KB"
-               type="int"/>
-
-    <attribute name="cacheTTL"
-               description="Time interval in ms between cache refeshes"
-               type="int"/>
-
-    <attribute name="cachingAllowed"
-               description="Should we cache static resources for this webapp"
-               is="true"
-               type="boolean"/>
-
     <attribute name="children"
                description="Object names of all children"
                type="[Ljavax.management.ObjectName;"/>

Modified: tomcat/trunk/java/org/apache/catalina/webresources/Cache.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/Cache.java?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/Cache.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/Cache.java Sat Dec 28 
14:06:07 2013
@@ -37,13 +37,15 @@ public class Cache {
     private static final long TARGET_FREE_PERCENT_GET = 5;
     private static final long TARGET_FREE_PERCENT_BACKGROUND = 10;
 
+    // objectMaxSize must be < maxSize/20
+    private static final int OBJECT_MAX_SIZE_FACTOR = 20;
+
     private final StandardRoot root;
     private final AtomicLong size = new AtomicLong(0);
 
     private long ttl = 5000;
     private long maxSize = 10 * 1024 * 1024;
-    private int objectMaxSize =
-            (int) (maxSize / 20 > Integer.MAX_VALUE ? Integer.MAX_VALUE : 
maxSize / 20);
+    private int objectMaxSize = (int) maxSize/OBJECT_MAX_SIZE_FACTOR;
 
     private AtomicLong lookupCount = new AtomicLong(0);
     private AtomicLong hitCount = new AtomicLong(0);
@@ -213,7 +215,7 @@ public class Cache {
 
     public void setObjectMaxSize(int objectMaxSize) {
         if (objectMaxSize * 1024L > Integer.MAX_VALUE) {
-            log.warn(sm.getString("cache.objectMaxSizeTooBig", 
Integer.valueOf(objectMaxSize)));
+            log.warn(sm.getString("cache.objectMaxSizeTooBigBytes", 
Integer.valueOf(objectMaxSize)));
             this.objectMaxSize = Integer.MAX_VALUE;
         }
         // Internally bytes, externally kilobytes
@@ -229,6 +231,18 @@ public class Cache {
         return objectMaxSize;
     }
 
+    void enforceObjectMaxSizeLimit() {
+        long limit = maxSize / OBJECT_MAX_SIZE_FACTOR;
+        if (limit > Integer.MAX_VALUE) {
+            return;
+        }
+        if (objectMaxSize > limit) {
+            log.warn(sm.getString("cache.objectMaxSizeTooBig",
+                    Integer.valueOf(objectMaxSize / 1024), 
Integer.valueOf((int)limit / 1024)));
+            objectMaxSize = (int) limit;
+        }
+    }
+
     public void clear() {
         resourceCache.clear();
     }

Modified: 
tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties 
Sat Dec 28 14:06:07 2013
@@ -18,7 +18,8 @@ abstractResource.getContentTooLarge=Unab
 
 cache.addFail=Unable to add the resource at [{0}] to the cache because there 
was insufficient free space available after evicting expired cache entries - 
consider increasing the maximum size of the cache
 cache.backgroundEvictFail=The background cache eviction process was unable to 
free [{0}] percent of the cache for Context [{1}] - consider increasing the 
maximum size of the cache. After eviction approximately [{2}] KB of data 
remained in the cache.
-cache.objectMaxSizeTooBig=The value specified for the maximum object size to 
cache [{0}]kB is greater than Integer.MAX_VALUE bytes which is the maximum size 
that can be cached. The limit will be set to Integer.MAX_VALUE bytes.
+cache.objectMaxSizeTooBig=The value of [{0}]kB for objectMaxSize is larger 
than the limit of maxSize/20 so has been reduced to [{1}]kB
+cache.objectMaxSizeTooBigBytes=The value specified for the maximum object size 
to cache [{0}]kB is greater than Integer.MAX_VALUE bytes which is the maximum 
size that can be cached. The limit will be set to Integer.MAX_VALUE bytes.
 
 dirResourceSet.writeExists=The target of the write already exists
 dirResourceSet.writeNpe=The input stream may not be null

Modified: tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java Sat 
Dec 28 14:06:07 2013
@@ -466,6 +466,11 @@ public class StandardRoot extends Lifecy
     @Override
     public void setCacheObjectMaxSize(int cacheObjectMaxSize) {
         cache.setObjectMaxSize(cacheObjectMaxSize);
+        // Don't enforce the limit when not running as attributes may get set 
in
+        // any order.
+        if (getState().isAvailable()) {
+            cache.enforceObjectMaxSizeLimit();
+        }
     }
 
     @Override
@@ -642,6 +647,8 @@ public class StandardRoot extends Lifecy
             classResource.start();
         }
 
+        cache.enforceObjectMaxSizeLimit();
+
         setState(LifecycleState.STARTING);
     }
 

Modified: 
tomcat/trunk/java/org/apache/catalina/webresources/mbeans-descriptors.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/mbeans-descriptors.xml?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/mbeans-descriptors.xml 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/mbeans-descriptors.xml 
Sat Dec 28 14:06:07 2013
@@ -57,8 +57,13 @@
                 group="WebResourceRoot"
                  type="org.apache.catalina.webresources.Cache">
 
-    <attribute   name="size"
-          description="The current estimate of the cache size in kB"
+    <attribute   name="hitCount"
+          description="The number of requests for resources that were served 
from the cache"
+                 type="long"
+            writeable="false"/>
+
+    <attribute   name="lookupCount"
+          description="The number of requests for resources"
                  type="long"
             writeable="false"/>
 
@@ -72,20 +77,15 @@
                  type="int"
             writeable="true"/>
 
-    <attribute   name="ttl"
-          description="The time-to-live for cache entries in milliseconds"
-                 type="long"
-            writeable="true"/>
-
-    <attribute   name="lookupCount"
-          description="The number of requests for resources"
+    <attribute   name="size"
+          description="The current estimate of the cache size in kB"
                  type="long"
             writeable="false"/>
 
-    <attribute   name="hitCount"
-          description="The number of requests for resources that were served 
from the cache"
+    <attribute   name="ttl"
+          description="The time-to-live for cache entries in milliseconds"
                  type="long"
-            writeable="false"/>
+            writeable="true"/>
 
   </mbean>
 

Modified: tomcat/trunk/test/org/apache/naming/resources/TestWarDirContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/naming/resources/TestWarDirContext.java?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/naming/resources/TestWarDirContext.java 
(original)
+++ tomcat/trunk/test/org/apache/naming/resources/TestWarDirContext.java Sat 
Dec 28 14:06:07 2013
@@ -26,6 +26,7 @@ import org.apache.catalina.core.JreMemor
 import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.catalina.webresources.StandardRoot;
 import org.apache.tomcat.util.buf.ByteChunk;
 
 public class TestWarDirContext extends TomcatBaseTest {
@@ -76,7 +77,9 @@ public class TestWarDirContext extends T
         // app dir is relative to server home
         StandardContext ctxt = (StandardContext) tomcat.addWebapp(
                 null, "/test", appDir.getAbsolutePath());
-        ctxt.setCachingAllowed(true);
+        StandardRoot root = new StandardRoot();
+        root.setCachingAllowed(true);
+        ctxt.setResources(root);
 
         tomcat.start();
 
@@ -106,7 +109,9 @@ public class TestWarDirContext extends T
         // app dir is relative to server home
         StandardContext ctxt = (StandardContext) tomcat.addWebapp(
                 null, "/test", appDir.getAbsolutePath());
-        ctxt.setCachingAllowed(false);
+        StandardRoot root = new StandardRoot();
+        root.setCachingAllowed(true);
+        ctxt.setResources(root);
 
         tomcat.start();
 

Modified: tomcat/trunk/webapps/docs/config/context.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/context.xml?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/context.xml (original)
+++ tomcat/trunk/webapps/docs/config/context.xml Sat Dec 28 14:06:07 2013
@@ -588,17 +588,6 @@
         </p>
       </attribute>
 
-      <attribute name="allowLinking" required="false">
-        <p>If the value of this flag is <code>true</code>, symlinks will be
-        allowed inside the web application, pointing to resources outside the
-        web application base path. If not specified, the default value
-        of the flag is <code>false</code>.</p>
-        <p><b>NOTE: This flag MUST NOT be set to true on the Windows platform
-        (or any other OS which does not have a case sensitive filesystem),
-        as it will disable case sensitivity checks, allowing JSP source code
-        disclosure, among other security problems.</b></p>
-      </attribute>
-
       <attribute name="antiResourceLocking" required="false">
         <p>If true, Tomcat will prevent any file locking.
         This will significantly impact startup time of applications,
@@ -619,32 +608,6 @@
         appBase for its Host.</p>
       </attribute>
 
-      <attribute name="cacheMaxSize" required="false">
-        <p>Maximum size of the static resource cache in kilobytes.
-        If not specified, the default value is <code>10240</code>
-        (10 megabytes).</p>
-      </attribute>
-
-      <attribute name="cacheObjectMaxSize" required="false">
-        <p>Maximum size of the static resource that will be placed in the 
cache.
-        If not specified, the default value is <code>512</code>
-        (512 kilobytes). If this value is greater than
-        <code>cacheMaxSize/20</code> it will be reduced to
-        <code>cacheMaxSize/20</code>.</p>
-      </attribute>
-
-      <attribute name="cacheTTL" required="false">
-        <p>Amount of time in milliseconds between cache entries revalidation.
-        If not specified, the default value is <code>5000</code>
-        (5 seconds).</p>
-      </attribute>
-
-      <attribute name="cachingAllowed" required="false">
-        <p>If the value of this flag is <code>true</code>, the cache for static
-        resources will be used. If not specified, the default value
-        of the flag is <code>true</code>.</p>
-      </attribute>
-
       <attribute name="clearReferencesHttpClientKeepAliveThread" required = 
"false">
         <p>If <code>true</code> and an <code>sun.net.www.http.HttpClient</code>
         keep-alive timer thread has been started by this web application and is

Modified: tomcat/trunk/webapps/docs/config/resources.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/resources.xml?rev=1553826&r1=1553825&r2=1553826&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/resources.xml (original)
+++ tomcat/trunk/webapps/docs/config/resources.xml Sat Dec 28 14:06:07 2013
@@ -42,9 +42,7 @@
   for files stored in other forms such as in a database or a versioned
   repository.</p>
 
-  <p>Resources are cached by default. Caching may be controlled by configuring
-  the appropriate caching attributes on the containing <a href="context.html">
-  Context</a>.</p>
+  <p>Resources are cached by default.</p>
 
   <p><strong>Note: Running a webapp with non-filesystem based
   Resources implementations is only possible when the webapp does not
@@ -63,11 +61,60 @@
 
   <subsection name="Common Attributes">
 
-    <p>All implementations of <strong>Resources</strong>
-    support the following attributes:</p>
+    <p>All implementations of <strong>Resources</strong> support the following
+    attributes:</p>
 
     <attributes>
 
+      <attribute name="allowLinking" required="false">
+        <p>If the value of this flag is <code>true</code>, symlinks will be
+        allowed inside the web application, pointing to resources outside the
+        web application base path. If not specified, the default value
+        of the flag is <code>false</code>.</p>
+        <p><b>NOTE: This flag MUST NOT be set to true on the Windows platform
+        (or any other OS which does not have a case sensitive filesystem),
+        as it will disable case sensitivity checks, allowing JSP source code
+        disclosure, among other security problems.</b></p>
+      </attribute>
+
+      <attribute name="cacheMaxSize" required="false">
+        <p>The maximum size of the static resource cache in kilobytes.
+        If not specified, the default value is <code>10240</code>
+        (10 megabytes). This value may be changed while the web application is
+        running (e.g. via JMX). If the cache is using more memory than the new
+        limit the cache will attempt to reduce in size over time to meet the
+        new limit. If necessary, <strong>cacheObjectMaxSize</strong> will be
+        reduced to ensure that it is no larger than
+        <code>cacheMaxSize/20</code>.</p>
+      </attribute>
+
+      <attribute name="cacheObjectMaxSize" required="false">
+        <p>Maximum size of the static resource that will be placed in the 
cache.
+        If not specified, the default value is <code>512</code>
+        (512 kilobytes). If this value is greater than
+        <code>cacheMaxSize/20</code> it will be reduced to
+        <code>cacheMaxSize/20</code>. This value may be changed while the web
+        application is running (e.g. via JMX).</p>
+      </attribute>
+
+      <attribute name="cacheTTL" required="false">
+        <p>The amount of time in milliseconds between the revalidation of cache
+        entries. If not specified, the default value is <code>5000</code> (5
+        seconds). This value may be changed while the web application is 
running
+        (e.g. via JMX). When a resource is cached it will inherit the TTL in
+        force at the time it was cached and retain that TTL until the resource
+        is evicted from the cache regardless of any subsequent changes that may
+        be made to this attribute.</p>
+      </attribute>
+
+      <attribute name="cachingAllowed" required="false">
+        <p>If the value of this flag is <code>true</code>, the cache for static
+        resources will be used. If not specified, the default value
+        of the flag is <code>true</code>. This value may be changed while the
+        web application is running (e.g. via JMX). When the cache is disabled
+        any resources currently in the cache are cleared from the cache.</p>
+      </attribute>
+
       <attribute name="className" required="false">
         <p>Java class name of the implementation to use. This class must
         implement the <code>org.apache.catalina.WebResourceRoot</code>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to