commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r358692 - in /jakarta/commons/proper/resources/trunk: src/java/org/apache/commons/resources/impl/ src/java/org/apache/commons/resources/util/ src/test/org/apache/commons/resources/util/ xdocs/
Date Fri, 23 Dec 2005 03:37:45 GMT
Author: niallp
Date: Thu Dec 22 19:37:26 2005
New Revision: 358692

URL: http://svn.apache.org/viewcvs?rev=358692&view=rev
Log:
Refactor ResourceBundleResources for issues described in Bug 38021.

Added:
    jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/
    jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
  (with props)
    jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
  (with props)
    jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/
    jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
  (with props)
Modified:
    jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
    jakarta/commons/proper/resources/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java?rev=358692&r1=358691&r2=358692&view=diff
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
(original)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
Thu Dec 22 19:37:26 2005
@@ -24,20 +24,16 @@
 package org.apache.commons.resources.impl;
 
 import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Locale;
-import java.util.Map;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
-import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.resources.ResourcesException;
 import org.apache.commons.resources.ResourcesKeyException;
+import org.apache.commons.resources.util.IteratorEnumeration;
 
 /**
  * <p>Concrete implementation of 
@@ -83,62 +79,6 @@
     private String base = null;
 
 
-    /**
-     * <p>Two-level cache of <code>ResourceBundle</code> instances
-     * that have previously been acquired by this {@link org.apache.commons.resources.Resources}
-     * instance.  The first-level key is the <code>ClassLoader</code>
-     * instance used to load the bundles.  The first-level value is a
-     * <code>java.util.Map</code> that is then, in turn, keyed by the
-     * <code>java.util.Locale</code> to which a particular bundle belongs.</p>
-     */
-    private Map bundles = new HashMap();
-
-
-    // ------------------------------------------------------ Lifecycle Methods
-
-
-    /**
-     * <p>This must be called to initialize the data content of this
-     * {@link org.apache.commons.resources.Resources} instance, before any of the <code>getXxx()</code>
-     * methods are called.</p>
-     *
-     * @exception ResourcesException if an error occurs during initialization
-     */
-    public void init() throws ResourcesException {
-
-        if (getLog().isDebugEnabled()) {
-            getLog().debug("Initializing for base name '" + base + "'");
-        }
-
-    }
-
-
-    /**
-     * <p>This method must be called when the manager of this resource
-     * decides that it's no longer needed.  After this method is called,
-     * no further calls to any of the <code>getXxx()</code> methods are
-     * allowed.</p>
-     *
-     * @exception ResourcesException if an error occurs during finalization
-     */
-    public void destroy() throws ResourcesException {
-
-        if (getLog().isDebugEnabled()) {
-            getLog().debug("Finalizing for base name '" + base + "'");
-        }
-        
-        synchronized (bundles) {
-            Iterator loaders = bundles.keySet().iterator();
-            while (loaders.hasNext()) {
-                ClassLoader loader = (ClassLoader) loaders.next();
-                ((Map) bundles.get(loader)).clear();
-            }
-            bundles.clear();
-        }
-
-    }
-
-
     // ------------------------------------------------------------- Properties
 
 
@@ -161,26 +101,12 @@
      */
     public Iterator getKeys() {
 
-        synchronized (bundles) {
-
-            ClassLoader loader = getClassLoader();
-            Map loaderMap = (Map) bundles.get(loader);
-            if (loaderMap == null) {
-                return (Collections.EMPTY_SET.iterator());
-            }
-            Set results = new HashSet();
-            Iterator locales = loaderMap.keySet().iterator();
-            while (locales.hasNext()) {
-                Locale locale = (Locale) locales.next();
-                ResourceBundle bundle = (ResourceBundle)
-                    loaderMap.get(locale);
-                Enumeration keys = bundle.getKeys();
-                while (keys.hasMoreElements()) {
-                    results.add(keys.nextElement());
-                }
-            }
-            return (results.iterator());
-
+        try {
+            ResourceBundle bundle = getBundle(null);
+            return new IteratorEnumeration(bundle.getKeys());
+            
+        } catch (MissingResourceException e) {
+            return Collections.EMPTY_LIST.iterator();
         }
 
     }
@@ -224,7 +150,7 @@
             
         } catch (MissingResourceException e) {
             if (getLog().isTraceEnabled()) {
-                getLog().trace("No message found for key '" + key + 
+                getLog().trace("No resource found for key '" + key + 
                                "' and locale '" + locale + "'");
             }
             if (isReturnNull()) {
@@ -260,26 +186,13 @@
             locale = Locale.getDefault();
         }
 
-        synchronized (bundles) {
+        // Locate the appropriate ClassLoader
+        ClassLoader loader = getClassLoader();
 
-            // Locate or create the Map for the appropriate ClassLoader
-            ClassLoader loader = getClassLoader();
-            Map loaderMap = (Map) bundles.get(loader);
-            if (loaderMap == null) {
-                loaderMap = new HashMap();
-                bundles.put(loader, loaderMap);
-            }
-
-            // Locate or create the requested ResourceBundle instance
-            ResourceBundle bundle =
-                (ResourceBundle) loaderMap.get(locale);
-            if (bundle == null) {
-                bundle = ResourceBundle.getBundle(base, locale, loader);
-                loaderMap.put(locale, bundle);
-            }
-            return (bundle);
+        // Locate the requested ResourceBundle instance
+        ResourceBundle bundle = ResourceBundle.getBundle(base, locale, loader);
 
-        }
+        return (bundle);
 
     }
 

Added: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java?rev=358692&view=auto
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
(added)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
Thu Dec 22 19:37:26 2005
@@ -0,0 +1,119 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.commons.resources.util;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+
+/**
+ * Two way Adaptor class for java.util.Iterator and java.util.Enumeration.
+ */
+public class IteratorEnumeration implements Iterator, Enumeration {
+    
+    private Iterator iterator;
+    private Enumeration enumeration;
+
+    // -------------------------- Constructors
+
+    /**
+     * Construct an instance which wraps an Iterator.
+     *
+     * @param iterator The <code>Iterator</code> to wrap.
+     */
+    public IteratorEnumeration(Iterator iterator) {
+        this.iterator = iterator;
+    }
+
+    /**
+     * Construct an instance which wraps an Enumeration.
+     *
+     * @param enumeration The <code>Enumeration</code> to wrap.
+     */
+    public IteratorEnumeration(Enumeration enumeration) {
+        this.enumeration = enumeration;
+    }
+
+    // -------------------------- Iterator Methods
+
+    /**
+     * <code>remove()<code> is not supported.
+     * @throws UnsupportedOperationException always.
+     */
+    public void remove() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Indicates whether the wrapped
+     * <code>Iterator/Enumeration</code> has more elements.
+     *
+     * @return <code>true</code> if the wrapped
+     * <code>Iterator/Enumeration</code> has more elements.
+     */
+    public boolean hasNext() {
+        if (iterator == null) {
+            return enumeration.hasMoreElements();
+        } else {
+            return iterator.hasNext();
+        }
+    }
+
+    /**
+     * Returns the next element in the wrapped
+     * <code>Iterator/Enumeration</code>.
+     *
+     * @return the next element.
+     */
+    public Object next() {
+        if (iterator == null) {
+            return enumeration.nextElement();
+        } else {
+            return iterator.next();
+        }
+    }
+
+    // -------------------------- Enumeration Methods
+
+    /**
+     * Indicates whether the wrapped
+     * <code>Iterator/Enumeration</code> has more elements.
+     *
+     * @return <code>true</code> if the wrapped
+     * <code>Iterator/Enumeration</code> has more elements.
+     */
+    public boolean hasMoreElements() {
+        return hasNext();
+    }
+
+    /**
+     * Returns the next element in the wrapped
+     * <code>Iterator/Enumeration</code>.
+     *
+     * @return the next element.
+     */
+    public Object nextElement() {
+        return next();
+    }
+
+}

Propchange: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/IteratorEnumeration.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html?rev=358692&view=auto
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
(added)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
Thu Dec 22 19:37:26 2005
@@ -0,0 +1,10 @@
+<html>
+<head>
+<title>Package Documentation for org.apache.commons.resources.util Package</title>
+</head>
+<body bgcolor="white">
+<p>
+This package contains utility classes used by Commons Resources.
+</p>
+</body>
+</html>

Propchange: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/util/package.html
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java?rev=358692&view=auto
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
(added)
+++ jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
Thu Dec 22 19:37:26 2005
@@ -0,0 +1,119 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 2005 The Apache Software Foundation
+ * 
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.commons.resources.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Enumeration;
+
+/**
+ * Unit tests for the <code>org.apache.commons.resources.util.IteratorEnumeration</code>
class.
+ */
+public class IteratorEnumerationTestCase extends TestCase {
+    
+    /**
+     * Defines the testcase name for JUnit.
+     *
+     * @param theName the testcase's name.
+     */
+    public IteratorEnumerationTestCase(String theName) {
+        super(theName);
+    }
+
+    /**
+     * Start the tests.
+     *
+     * @param theArgs the arguments. Not used
+     */
+    public static void main(String[] theArgs) {
+        junit.awtui.TestRunner.main(
+            new String[] { IteratorEnumerationTestCase.class.getName()});
+    }
+
+    /**
+     * @return a test suite (<code>TestSuite</code>) that includes all methods
+     *         starting with "test"
+     */
+    public static Test suite() {
+        return new TestSuite(IteratorEnumerationTestCase.class);
+    }
+
+    public void testIterator() {
+
+        // Wrap an Iterator
+        ArrayList list = new ArrayList();
+        list.add("one");
+        Enumeration enumeration = new IteratorEnumeration(list.iterator());
+        Iterator iterator       = new IteratorEnumeration(list.iterator());
+
+        // Test Enumeration
+        assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
+        assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
+        assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
+
+        // Test Iterator
+        assertTrue("Iterator.hasNext(A)", iterator.hasNext());
+        assertEquals("Iterator.next",  "one", iterator.next());
+        assertFalse("Iterator.hasNext(B)", iterator.hasNext());
+        try {
+           iterator.remove();
+           fail("Expected UnsupportedOperationException");
+        } catch(UnsupportedOperationException e) {
+           // expected result
+        }
+
+    }
+
+    public void testEnumeration() {
+
+        // Wrap an Enumeration
+        Hashtable hashtable = new Hashtable();
+        hashtable.put("one", "XXX");
+        Enumeration enumeration = new IteratorEnumeration(hashtable.keys());
+        Iterator iterator       = new IteratorEnumeration(hashtable.keys());
+
+        // Test Enumeration
+        assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
+        assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
+        assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
+
+        // Test Iterator
+        assertTrue("Iterator.hasNext(A)", iterator.hasNext());
+        assertEquals("Iterator.next",  "one", iterator.next());
+        assertFalse("Iterator.hasNext(B)", iterator.hasNext());
+
+        try {
+           iterator.remove();
+           fail("Expected UnsupportedOperationException");
+        } catch(UnsupportedOperationException e) {
+           // expected result
+        }
+
+        
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/util/IteratorEnumerationTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/resources/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/xdocs/changes.xml?rev=358692&r1=358691&r2=358692&view=diff
==============================================================================
--- jakarta/commons/proper/resources/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/resources/trunk/xdocs/changes.xml Thu Dec 22 19:37:26 2005
@@ -39,6 +39,12 @@
 <body>
 
     <release version="1.0.0" date="(not yet released)" description="Initial Release">
+        <action dev="niallp" type="fix" issue="38021">
+            Refactoring of ResourceBundleResources implementation to remove
+            caching of ResourceBundle instances - makes this implementation
+            Serializable and prevents a potential memory leak.
+            <i>(Dec 2005)</i>.
+        </action>
         <action dev="niallp" type="fix" issue="37679" due-to="Rahul Akolkar">
             Remove unused imports.
             <i>(Dec 2005)</i>.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message