incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1454255 - in /sling/trunk/bundles: api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java
Date Fri, 08 Mar 2013 06:56:12 GMT
Author: cziegeler
Date: Fri Mar  8 06:56:12 2013
New Revision: 1454255

URL: http://svn.apache.org/r1454255
Log:
SLING-2780 :  Make ResourceMetadata read-only when delivered to client code 

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java?rev=1454255&r1=1454254&r2=1454255&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
(original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
Fri Mar  8 06:56:12 2013
@@ -18,7 +18,11 @@
  */
 package org.apache.sling.api.resource;
 
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * The <code>ResourceMetadata</code> interface defines the API for the
@@ -33,6 +37,9 @@ import java.util.HashMap;
  * <p>
  * Note, that the prefix <em>sling.</em> to key names is reserved for the
  * Sling implementation.
+ *
+ * Once a resource is returned by the {@link ResourceResolver}, the resource
+ * metadata is made read-only and therefore can't be changed by client code!
  */
 public class ResourceMetadata extends HashMap<String, Object> {
 
@@ -119,6 +126,8 @@ public class ResourceMetadata extends Ha
      */
     public static final String INTERNAL_CONTINUE_RESOLVING = ":org.apache.sling.resource.internal.continue.resolving";
 
+    private boolean isReadOnly = false;
+
     /**
      * Sets the {@link #CHARACTER_ENCODING} property to <code>encoding</code>
      * if not <code>null</code>.
@@ -283,4 +292,71 @@ public class ResourceMetadata extends Ha
 
         return null;
     }
+
+    /**
+     * Make this object read-only. All method calls trying to modify this object
+     * result in an exception!
+     */
+    public void makeReadOnly() {
+        this.isReadOnly = true;
+    }
+
+    /**
+     * Check if this object is read only and if so throw an unsupported operation exception.
+     */
+    private void checkReadOnly() {
+        if ( this.isReadOnly ) {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    @Override
+    public void clear() {
+        this.checkReadOnly();
+        super.clear();
+    }
+
+    @Override
+    public Set<java.util.Map.Entry<String, Object>> entrySet() {
+        if ( this.isReadOnly ) {
+            // TODO - commons collections?
+            //return new UnmodifiableEntrySet(super.entrySet());
+        }
+        return super.entrySet();
+    }
+
+    @Override
+    public Object put(final String key, final Object value) {
+        this.checkReadOnly();
+        return super.put(key, value);
+    }
+
+    @Override
+    public void putAll(final Map<? extends String, ? extends Object> m) {
+        this.checkReadOnly();
+        super.putAll(m);
+    }
+
+    @Override
+    public Object remove(final Object key) {
+        this.checkReadOnly();
+        return super.remove(key);
+    }
+
+
+    @Override
+    public Set<String> keySet() {
+        if ( this.isReadOnly ) {
+            return Collections.unmodifiableSet(super.keySet());
+        }
+        return super.keySet();
+    }
+
+    @Override
+    public Collection<Object> values() {
+        if ( this.isReadOnly ) {
+            return Collections.unmodifiableCollection(super.values());
+        }
+        return super.values();
+    }
 }

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java?rev=1454255&r1=1454254&r2=1454255&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java
(original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/helper/ResourceDecoratorTracker.java
Fri Mar  8 06:56:12 2013
@@ -67,11 +67,14 @@ public class ResourceDecoratorTracker {
             }
         }
 
+        // make resource metadata read-only
+        result.getResourceMetadata().makeReadOnly();
+
         return result;
     }
 
     /**
-     * Bind a resouce decorator.
+     * Bind a resource decorator.
      */
     public void bindResourceDecorator(final ResourceDecorator decorator,
             final Map<String, Object> props) {



Mime
View raw message