You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2016/09/22 10:19:33 UTC

svn commit: r1761879 - in /sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl: JcrResourceBundle.java JcrResourceBundleProvider.java

Author: cziegeler
Date: Thu Sep 22 10:19:33 2016
New Revision: 1761879

URL: http://svn.apache.org/viewvc?rev=1761879&view=rev
Log:
SLING-5999 : JcrResourceBundleProvider should move to new ResourceChangeListener API

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

Modified: sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java?rev=1761879&r1=1761878&r2=1761879&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java (original)
+++ sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java Thu Sep 22 10:19:33 2016
@@ -210,9 +210,7 @@ public class JcrResourceBundle extends R
                 loadSlingMessageDictionary(dictionaryResource, dictionary);
             }
 
-            if (!dictionary.isEmpty()) {
-                languageRoots.add(root);
-            }
+            languageRoots.add(root);
         }
 
         // linked hash map to keep order (not functionally important, but helpful for dictionary debugging)

Modified: sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java?rev=1761879&r1=1761878&r2=1761879&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java (original)
+++ sling/trunk/bundles/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java Thu Sep 22 10:19:33 2016
@@ -67,8 +67,7 @@ import org.slf4j.LoggerFactory;
  * repository.
  */
 @Component(immediate = true, metatype = true, label = "%provider.name", description = "%provider.description")
-@Service({ResourceBundleProvider.class, ResourceChangeListener.class})
-@Property(name=ResourceChangeListener.PATHS, value="/", propertyPrivate=true)
+@Service({ResourceBundleProvider.class})
 public class JcrResourceBundleProvider implements ResourceBundleProvider, ResourceChangeListener, ExternalResourceChangeListener {
 
     private static final boolean DEFAULT_PRELOAD_BUNDLES = false;
@@ -127,7 +126,7 @@ public class JcrResourceBundleProvider i
     /**
      * paths from which JCR resource bundles have been loaded
      */
-    private final Set<String> languageRootPaths = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
+    private final Set<LanguageRoot> languageRootPaths = Collections.newSetFromMap(new ConcurrentHashMap<LanguageRoot, Boolean>());
 
     /**
      * Return root resource bundle as created on-demand by
@@ -140,7 +139,7 @@ public class JcrResourceBundleProvider i
     /**
      * Each ResourceBundle is registered as a service. Each registration is stored in this map with the locale & base name used as a key.
      */
-    private Map<Key, ServiceRegistration<ResourceBundle>> bundleServiceRegistrations;
+    private final Map<Key, ServiceRegistration<ResourceBundle>> bundleServiceRegistrations = new HashMap<Key, ServiceRegistration<ResourceBundle>>();
 
     private boolean preloadBundles;
 
@@ -199,8 +198,8 @@ public class JcrResourceBundleProvider i
                 scheduleReloadBundles(true);
             } else {
                 // if it is only a change below a root path, only messages of one resource bundle can be affected!
-                for (final String root : languageRootPaths) {
-                    if (change.getPath().startsWith(root)) {
+                for (final LanguageRoot root : languageRootPaths) {
+                    if (change.getPath().startsWith(root.path)) {
                         // figure out which JcrResourceBundle from the cached ones is affected
                         for (JcrResourceBundle bundle : resourceBundleCache.values()) {
                             if (bundle.getLanguageRootPaths().contains(root)) {
@@ -377,7 +376,6 @@ public class JcrResourceBundleProvider i
         this.preloadBundles = PropertiesUtil.toBoolean(props.get(PROP_PRELOAD_BUNDLES), DEFAULT_PRELOAD_BUNDLES);
 
         this.bundleContext = context;
-        this.bundleServiceRegistrations = new HashMap<Key, ServiceRegistration<ResourceBundle>>();
         invalidationDelay = PropertiesUtil.toLong(props.get(PROP_INVALIDATION_DELAY), DEFAULT_INVALIDATION_DELAY);
         if (this.resourceResolverFactory != null) { // this is only null during test execution!
             if (repoCredentials == null) {
@@ -451,7 +449,18 @@ public class JcrResourceBundleProvider i
 
         // register language root paths
         final Set<String> languageRoots = resourceBundle.getLanguageRootPaths();
-        languageRootPaths.addAll(languageRoots);
+        for(final String path : languageRoots) {
+            boolean found = this.languageRootPaths.contains(path);
+            if ( !found ) {
+                final Dictionary<String, Object> properties = new Hashtable<>();
+                properties.put(ResourceChangeListener.PATHS, path);
+                final ServiceRegistration<ResourceChangeListener> reg =
+                        this.bundleContext.registerService(ResourceChangeListener.class,
+                                this, properties);
+                final LanguageRoot root = new LanguageRoot(path, reg);
+                languageRootPaths.add(root);
+            }
+        }
         log.debug("registerResourceBundle({}, ...): added service registration and language roots {}", key, languageRoots);
         log.info("Currently loaded dictionaries across all locales: {}", languageRootPaths);
     }
@@ -524,6 +533,9 @@ public class JcrResourceBundleProvider i
 
     private void clearCache() {
         resourceBundleCache.clear();
+        for(final LanguageRoot root : this.languageRootPaths) {
+            root.registration.unregister();
+        }
         languageRootPaths.clear();
 
         synchronized (this) {
@@ -691,4 +703,29 @@ public class JcrResourceBundleProvider i
             return "Key(" + baseName + ", " + locale + ")";
         }
     }
+
+    public static final class LanguageRoot {
+
+        public final String path;
+
+        public final ServiceRegistration<ResourceChangeListener> registration;
+
+        public LanguageRoot(final String path, final ServiceRegistration<ResourceChangeListener> reg) {
+            this.path = path;
+            this.registration = reg;
+        }
+
+        @Override
+        public int hashCode() {
+            return this.path.hashCode();
+        }
+
+        @Override
+        public boolean equals(final Object obj) {
+            if ( obj == null ) {
+                return false;
+            }
+            return ((obj instanceof LanguageRoot) && this.path.equals(((LanguageRoot)obj).path)) || this.path.equals(obj.toString());
+        }
+    }
 }