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 2015/01/09 12:09:24 UTC

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

Author: cziegeler
Date: Fri Jan  9 11:09:24 2015
New Revision: 1650497

URL: http://svn.apache.org/r1650497
Log:
SLING-4186 : Make org.apache.sling.i18n independent from JCR APIs

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

Modified: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java?rev=1650497&r1=1650496&r2=1650497&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java (original)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java Fri Jan  9 11:09:24 2015
@@ -37,19 +37,13 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Semaphore;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.observation.Event;
-import javax.jcr.observation.EventIterator;
-import javax.jcr.observation.EventListener;
-import javax.jcr.observation.ObservationManager;
-
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
@@ -58,6 +52,8 @@ import org.apache.sling.i18n.ResourceBun
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -68,8 +64,9 @@ import org.slf4j.LoggerFactory;
  * repository.
  */
 @Component(immediate = true, metatype = true, label = "%provider.name", description = "%provider.description")
-@Service(ResourceBundleProvider.class)
-public class JcrResourceBundleProvider implements ResourceBundleProvider {
+@Service({ResourceBundleProvider.class, EventHandler.class})
+@Property(name=EventConstants.EVENT_TOPIC, value="org/apache/sling/api/resource/Resource/*")
+public class JcrResourceBundleProvider implements ResourceBundleProvider, EventHandler {
 
     private static final boolean DEFAULT_PRELOAD_BUNDLES = false;
 
@@ -173,50 +170,32 @@ public class JcrResourceBundleProvider i
         return getResourceBundleInternal(baseName, locale);
     }
 
-    // ---------- EventListener ------------------------------------------------
-
-    /**
-     * Observation support class that is used whenever something is changed inside of
-     * <code>sling:Message</code> nodes. We just removed all cached
-     * resource bundles in this case to force reloading them.
-     * <p>
-     * This is much simpler than analyzing the events and trying to be clever
-     * about which exact resource bundles to remove from the cache and at the
-     * same time care for any resource bundle dependencies.
-     */
-    private final EventListener messageChangeHandler = new EventListener() {
-
-        @Override
-        public void onEvent(EventIterator events) {
-            log.debug("onEvent: Resource changes, removing cached ResourceBundles");
-            clearCache();
-            preloadBundles();
-        }
-    };
+    // ---------- EventHandler ------------------------------------------------
 
-    /**
-     * Observation support class that listens for changes of <code>mix:language</code> nodes.
-     * In this case we check if the given language is already loaded and only then invalidate the cache.
-     */
-    private final EventListener languageChangeHandler = new EventListener() {
-        @Override
-        public void onEvent(EventIterator events) {
-            log.debug("onEvent: Resource changes. checking for cached bundle.");
-            while (events.hasNext()) {
-                Event e = events.nextEvent();
-                try {
-                    if (languageRootPaths.contains(e.getPath())) {
-                        log.debug("onEvent: Detected change of cached language root {}, removing cached ResourceBundles", e.getPath());
-                        clearCache();
-                        preloadBundles();
-                        return;
+    @Override
+    public void handleEvent(final org.osgi.service.event.Event event) {
+        final String path = (String)event.getProperty(SlingConstants.PROPERTY_PATH);
+        if ( path != null ) {
+            boolean invalidate = false;
+            if ( languageRootPaths.contains(path) ) {
+                log.debug("handleEvent: Detected change of cached language root {}, removing cached ResourceBundles", path);
+                invalidate = true;
+            } else {
+                for(final String root : languageRootPaths) {
+                    if ( path.startsWith(root) ) {
+                        log.debug("handleEvent: Resource changes, removing cached ResourceBundles");
+                        invalidate = true;
+                        break;
                     }
-                } catch (RepositoryException e1) {
-                    // ignore
                 }
             }
+
+            if ( invalidate ) {
+                clearCache();
+                preloadBundles();
+            }
         }
-    };
+    }
 
     // ---------- SCR Integration ----------------------------------------------
 
@@ -449,21 +428,8 @@ public class JcrResourceBundleProvider i
                         resolver = fac.getResourceResolver(repoCredentials);
                     }
 
-                    final Session s = resolver.adaptTo(Session.class);
-                    ObservationManager om = s.getWorkspace().getObservationManager();
-                    om.addEventListener(messageChangeHandler, 255, "/", true, null,
-                        new String[] { "sling:Message" }, true);
-                    om.addEventListener(languageChangeHandler, 255, "/", true, null,
-                        new String[] { "mix:language" }, true);
-
                     resourceResolver = resolver;
 
-                } catch (RepositoryException re) {
-
-                    log.error(
-                        "getResourceResolver: Problem setting up ResourceResolver with Session",
-                        re);
-
                 } catch (LoginException le) {
 
                     log.error(
@@ -495,7 +461,6 @@ public class JcrResourceBundleProvider i
 
     private void preloadBundles() {
         if (preloadBundles) {
-            @SuppressWarnings("deprecation")
             Iterator<Map<String, Object>> bundles = getResourceResolver().queryResources(
                     JcrResourceBundle.QUERY_LANGUAGE_ROOTS, "xpath");
             Set<Key> usedKeys = new HashSet<Key>();
@@ -527,21 +492,6 @@ public class JcrResourceBundleProvider i
 
         if (resolver != null) {
 
-            Session s = resolver.adaptTo(Session.class);
-            if (s != null) {
-
-                try {
-                    ObservationManager om = s.getWorkspace().getObservationManager();
-                    om.removeEventListener(messageChangeHandler);
-                    om.removeEventListener(languageChangeHandler);
-                } catch (Throwable t) {
-                    log.info(
-                        "releaseRepository: Problem unregistering as event listener",
-                        t);
-                }
-
-            }
-
             try {
                 resolver.close();
             } catch (Throwable t) {