Author: markt
Date: Wed Sep 11 14:27:27 2013
New Revision: 1521871

URL: http://svn.apache.org/r1521871
Log:
Put a limit on the number of Locale to StringManager mappings that will be 
cached.

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/res/StringManager.java

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1521840

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/res/StringManager.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/res/StringManager.java?rev=1521871&r1=1521870&r2=1521871&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/res/StringManager.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/res/StringManager.java Wed 
Sep 11 14:27:27 2013
@@ -20,6 +20,7 @@ package org.apache.tomcat.util.res;
 import java.text.MessageFormat;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.MissingResourceException;
@@ -54,6 +55,8 @@ import java.util.ResourceBundle;
 
 public class StringManager {
 
+    private static int LOCALE_CACHE_SIZE = 10;
+
     /**
      * The ResourceBundle for this StringManager.
      */
@@ -198,7 +201,25 @@ public class StringManager {
 
         Map<Locale,StringManager> map = managers.get(packageName);
         if (map == null) {
-            map = new Hashtable<Locale, StringManager>();
+            /*
+             * Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE.
+             * Expansion occurs when size() exceeds capacity. Therefore keep
+             * size at or below capacity.
+             * removeEldestEntry() executes after insertion therefore the test
+             * for removal needs to use one less than the maximum desired size
+             *
+             */
+            map = new LinkedHashMap<Locale,StringManager>(LOCALE_CACHE_SIZE, 
1, true) {
+                private static final long serialVersionUID = 1L;
+                @Override
+                protected boolean removeEldestEntry(
+                        Map.Entry<Locale,StringManager> eldest) {
+                    if (size() > (LOCALE_CACHE_SIZE - 1)) {
+                        return true;
+                    }
+                    return false;
+                }
+            };
             managers.put(packageName, map);
         }
 
@@ -230,6 +251,4 @@ public class StringManager {
         // Return the default
         return getManager(packageName);
     }
-
-
 }



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

Reply via email to