You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2010/11/10 16:17:31 UTC

svn commit: r1033507 - /sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java

Author: fmeschbe
Date: Wed Nov 10 15:17:25 2010
New Revision: 1033507

URL: http://svn.apache.org/viewvc?rev=1033507&view=rev
Log:
SLING-1871 Use a ConcurrentHashMap to store the resources to make access thread safe. In addition synchronize on the JcrResourceBundle instance when doing the full load to prevent excess load on the system. Finally implement the Java 6 handleKeySet() method to just return the key set view of the fully loaded resources.

Modified:
    sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java

Modified: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java?rev=1033507&r1=1033506&r2=1033507&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java (original)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java Wed Nov 10 15:17:25 2010
@@ -26,6 +26,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.jcr.query.Query;
 
@@ -36,7 +38,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class JcrResourceBundle extends ResourceBundle {
-    
+
     private static final Logger log = LoggerFactory.getLogger(JcrResourceBundle.class);
 
     private static final String JCR_PATH = "jcr:path";
@@ -70,7 +72,7 @@ public class JcrResourceBundle extends R
 
     private final ResourceResolver resourceResolver;
 
-    private Map<String, Object> resources;
+    private final ConcurrentHashMap<String, Object> resources;
 
     private boolean fullyLoaded;
 
@@ -85,7 +87,7 @@ public class JcrResourceBundle extends R
         this.locale = locale;
         this.baseName = baseName;
         this.resourceResolver = resourceResolver;
-        this.resources = new HashMap<String, Object>();
+        this.resources = new ConcurrentHashMap<String, Object>();
         this.fullyLoaded = false;
     }
 
@@ -99,6 +101,21 @@ public class JcrResourceBundle extends R
         return locale;
     }
 
+    /**
+     * Returns a Set of all resource keys provided by this resource bundle only.
+     * <p>
+     * This method is a new Java 1.6 method to implement the
+     * ResourceBundle.keySet() method.
+     *
+     * @return The keys of the resources provided by this resource bundle
+     */
+    protected Set<String> handleKeySet() {
+        // ensure all keys are loaded
+        loadFully();
+
+        return resources.keySet();
+    }
+
     @Override
     public Enumeration<String> getKeys() {
         // ensure all keys are loaded
@@ -126,48 +143,53 @@ public class JcrResourceBundle extends R
     private void loadFully() {
         if (!fullyLoaded) {
 
-            final String fullLoadQuery = getFullLoadQuery();
-            if (log.isDebugEnabled()) {
-                log.debug("Executing full load query {}", fullLoadQuery);
-            }
-            Iterator<Map<String, Object>> bundles = resourceResolver.queryResources(
-                fullLoadQuery, Query.XPATH);
+            synchronized (this) {
+                if (!fullyLoaded) {
 
-            String[] path = getSearchPath();
+                    final String fullLoadQuery = getFullLoadQuery();
+                    if (log.isDebugEnabled()) {
+                        log.debug("Executing full load query {}", fullLoadQuery);
+                    }
+                    Iterator<Map<String, Object>> bundles = resourceResolver.queryResources(
+                        fullLoadQuery, Query.XPATH);
 
-            List<Map<String, Object>> res0 = new ArrayList<Map<String, Object>>();
-            for (int i = 0; i < path.length; i++) {
-                res0.add(new HashMap<String, Object>());
-            }
-            Map<String, Object> rest = new HashMap<String, Object>();
+                    String[] path = getSearchPath();
 
-            while (bundles.hasNext()) {
-                Map<String, Object> row = bundles.next();
-                String jcrPath = (String) row.get(JCR_PATH);
-                String key = (String) row.get(PROP_KEY);
+                    List<Map<String, Object>> res0 = new ArrayList<Map<String, Object>>();
+                    for (int i = 0; i < path.length; i++) {
+                        res0.add(new HashMap<String, Object>());
+                    }
+                    Map<String, Object> rest = new HashMap<String, Object>();
 
-                if (key == null) {
-                    key = ResourceUtil.getName(jcrPath);
-                }
+                    while (bundles.hasNext()) {
+                        Map<String, Object> row = bundles.next();
+                        String jcrPath = (String) row.get(JCR_PATH);
+                        String key = (String) row.get(PROP_KEY);
+
+                        if (key == null) {
+                            key = ResourceUtil.getName(jcrPath);
+                        }
+
+                        Map<String, Object> dst = rest;
+                        for (int i = 0; i < path.length; i++) {
+                            if (jcrPath.startsWith(path[i])) {
+                                dst = res0.get(i);
+                                break;
+                            }
+                        }
 
-                Map<String, Object> dst = rest;
-                for (int i = 0; i < path.length; i++) {
-                    if (jcrPath.startsWith(path[i])) {
-                        dst = res0.get(i);
-                        break;
+                        dst.put(key, row.get(PROP_VALUE));
                     }
-                }
-
-                dst.put(key, row.get(PROP_VALUE));
-            }
 
-            for (int i = path.length - 1; i >= 0; i--) {
-                rest.putAll(res0.get(i));
-            }
+                    for (int i = path.length - 1; i >= 0; i--) {
+                        rest.putAll(res0.get(i));
+                    }
 
-            resources.putAll(rest);
+                    resources.putAll(rest);
 
-            fullyLoaded = true;
+                    fullyLoaded = true;
+                }
+            }
         }
     }
 
@@ -188,7 +210,7 @@ public class JcrResourceBundle extends R
             while (bundles.hasNext() && currentWeight > 0) {
                 Map<String, Object> resource = bundles.next();
                 String jcrPath = (String) resource.get(JCR_PATH);
-                
+
                 // skip resources without sling:key and non-matching nodename
                 if (resource.get(PROP_KEY) == null) {
                     if (!key.equals(Text.getName(jcrPath))) {
@@ -210,7 +232,7 @@ public class JcrResourceBundle extends R
                     currentValue = resource;
                 }
             }
-            
+
             if (currentValue != null) {
                 return currentValue.get(PROP_VALUE);
             }