incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1404209 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
Date Wed, 31 Oct 2012 15:40:04 GMT
Author: bdelacretaz
Date: Wed Oct 31 15:40:03 2012
New Revision: 1404209

URL: http://svn.apache.org/viewvc?rev=1404209&view=rev
Log:
SLING-2640 - ResourceProviderEntry did not correctly remove a root provider

Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java?rev=1404209&r1=1404208&r2=1404209&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java
(original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntry.java
Wed Oct 31 15:40:03 2012
@@ -215,15 +215,25 @@ public class ResourceProviderEntry imple
      * Remove the given resource provider from the tree
      */
     protected synchronized boolean removeResourceProvider(final String prefix, final ProviderHandler
resourceProvider) {
+        boolean result = false;
         final String[] elements = split(prefix);
         final List<ResourceProviderEntry> entries = new ArrayList<ResourceProviderEntry>();
         this.populateProviderPath(entries, elements);
 
-        if (entries.size() > 0 && entries.size() == elements.length) {
+        if(entries.size() == 0) {
+            // might be a root provider, try to remove it on this entry
+            result = this.removeInternalProvider(resourceProvider);
+        } else if (entries.size() > 0 && entries.size() == elements.length) {
             // the last element is a perfect match;
-            return entries.get(entries.size() - 1).removeInternalProvider(resourceProvider);
+            result = entries.get(entries.size() - 1).removeInternalProvider(resourceProvider);
         }
-        return false;
+        
+        if(!result) {
+            // bad news - the provider might be an OSGi service being deactivated,
+            // so this should be taken care of.
+            LOGGER.warn("Unable to remove {} for prefix {}, no matching entry found", resourceProvider,
prefix);
+        }
+        return result;
     }
 
     /**

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java?rev=1404209&r1=1404208&r2=1404209&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
(original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/tree/ResourceProviderEntryTest.java
Wed Oct 31 15:40:03 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.resourceresolver.impl.tree;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
@@ -34,6 +35,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.Constants;
@@ -179,6 +181,37 @@ public class ResourceProviderEntryTest {
         assertEquals(first, root.getResource(null, null, "/rootel/html.js"));
         assertEquals(second, root.getResource(null, null, "/rootel/child/html.js"));
     }
+    
+    @Test public void testRemoveTheOnlyProvider() {
+        final ResourceProviderEntry e = new ResourceProviderEntry("/", null);
+        long counter = 1;
+        
+        for(String path : new String[] { "/foo", "/", "/foo/bar" }) {
+            final ResourceProvider p = new TestResourceProvider(path);
+            final Map<String, Object> props = new HashMap<String, Object>();
+            props.put(Constants.SERVICE_ID, ++counter);
+            
+            e.addResourceProvider(path, new ResourceProviderHandler(p, props));
+            {
+                final Resource r = e.getResource(null, null, path); 
+                assertEquals(p, r);
+                assertFalse(r instanceof SyntheticResource);
+            }
+            
+            e.removeResourceProvider(path, new ResourceProviderHandler(p, props));
+            {
+                final Resource r = e.getResource(null, null, path); 
+                // If our provider is indeed gone, we should get one of the following conditions
+                if(r == null) {
+                    //fine
+                } else if(!p.equals(r.getResourceResolver())) {
+                    //fine
+                } else {
+                    fail("Expecting inactive provider after removing it for " + path); 
+                }
+            }
+        }
+    }
 
     protected void assertEquals(ResourceProvider resProvider, Resource res) {
         org.junit.Assert.assertEquals(resProvider, res.getResourceResolver());



Mime
View raw message