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 2013/03/08 08:30:52 UTC

svn commit: r1454259 - /sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java

Author: cziegeler
Date: Fri Mar  8 07:30:52 2013
New Revision: 1454259

URL: http://svn.apache.org/r1454259
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

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=1454259&r1=1454258&r2=1454259&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 07:30:52 2013
@@ -126,18 +126,13 @@ public class ResourceMetadata extends Ha
      */
     public static final String INTERNAL_CONTINUE_RESOLVING = ":org.apache.sling.resource.internal.continue.resolving";
 
-
-    /**
-     * This map contains the real metadata.
-     * A call to {@link #makeReadOnly()} makes it read-only
-     */
-    private Map<String, Object> dataMap = new HashMap<String, Object>();
+    private boolean isReadOnly = false;
 
     /**
      * Sets the {@link #CHARACTER_ENCODING} property to <code>encoding</code>
      * if not <code>null</code>.
      */
-    public void setCharacterEncoding(final String encoding) {
+    public void setCharacterEncoding(String encoding) {
         if (encoding != null) {
             put(CHARACTER_ENCODING, encoding);
         }
@@ -161,7 +156,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #CONTENT_TYPE} property to <code>contentType</code> if
      * not <code>null</code>.
      */
-    public void setContentType(final String contentType) {
+    public void setContentType(String contentType) {
         if (contentType != null) {
             put(CONTENT_TYPE, contentType);
         }
@@ -185,7 +180,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #CONTENT_LENGTH} property to <code>contentType</code>
      * if not <code>null</code>.
      */
-    public void setContentLength(final long contentLength) {
+    public void setContentLength(long contentLength) {
         if (contentLength > 0) {
             put(CONTENT_LENGTH, contentLength);
         }
@@ -208,7 +203,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #CREATION_TIME} property to <code>creationTime</code>
      * if not negative.
      */
-    public void setCreationTime(final long creationTime) {
+    public void setCreationTime(long creationTime) {
         if (creationTime >= 0) {
             put(CREATION_TIME, creationTime);
         }
@@ -231,7 +226,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #MODIFICATION_TIME} property to
      * <code>modificationTime</code> if not negative.
      */
-    public void setModificationTime(final long modificationTime) {
+    public void setModificationTime(long modificationTime) {
         if (modificationTime >= 0) {
             put(MODIFICATION_TIME, modificationTime);
         }
@@ -254,7 +249,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #RESOLUTION_PATH} property to <code>resolutionPath</code>
      * if not <code>null</code>.
      */
-    public void setResolutionPath(final String resolutionPath) {
+    public void setResolutionPath(String resolutionPath) {
         if (resolutionPath != null) {
             put(RESOLUTION_PATH, resolutionPath);
         }
@@ -278,7 +273,7 @@ public class ResourceMetadata extends Ha
      * Sets the {@link #RESOLUTION_PATH_INFO} property to
      * <code>resolutionPathInfo</code> if not <code>null</code>.
      */
-    public void setResolutionPathInfo(final String resolutionPathInfo) {
+    public void setResolutionPathInfo(String resolutionPathInfo) {
         if (resolutionPathInfo != null) {
             put(RESOLUTION_PATH_INFO, resolutionPathInfo);
         }
@@ -303,71 +298,72 @@ public class ResourceMetadata extends Ha
      * result in an exception!
      */
     public void makeReadOnly() {
-        this.dataMap = Collections.unmodifiableMap(this.dataMap);
-    }
-
-    @Override
-    public void clear() {
-        this.dataMap.clear();
-    }
-
-    @Override
-    public boolean containsKey(final Object key) {
-        return this.dataMap.containsKey(key);
+        this.isReadOnly = true;
     }
 
-    @Override
-    public boolean containsValue(final Object value) {
-        return this.dataMap.containsValue(value);
+    /**
+     * 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 Set<java.util.Map.Entry<String, Object>> entrySet() {
-        return this.dataMap.entrySet();
+    public void clear() {
+        this.checkReadOnly();
+        super.clear();
     }
 
     @Override
-    public Object get(final Object key) {
-        return this.dataMap.get(key);
+    public Object put(final String key, final Object value) {
+        this.checkReadOnly();
+        return super.put(key, value);
     }
 
     @Override
-    public boolean isEmpty() {
-        return this.dataMap.isEmpty();
+    public void putAll(final Map<? extends String, ? extends Object> m) {
+        this.checkReadOnly();
+        super.putAll(m);
     }
 
     @Override
-    public Set<String> keySet() {
-        return this.dataMap.keySet();
+    public Object remove(final Object key) {
+        this.checkReadOnly();
+        return super.remove(key);
     }
 
-    @Override
-    public Object put(final String key, final Object value) {
-        return this.dataMap.put(key, value);
-    }
+    private Map<String, Object> unmodifiableMap;
 
-    @Override
-    public void putAll(final Map<? extends String, ? extends Object> m) {
-        this.dataMap.putAll(m);
+    private Map<String, Object> getUnmodifiableMap() {
+        if ( this.unmodifiableMap == null ) {
+            this.unmodifiableMap = Collections.unmodifiableMap(this);
+        }
+        return this.unmodifiableMap;
     }
 
     @Override
-    public Object remove(final Object key) {
-        return this.dataMap.remove(key);
+    public Set<java.util.Map.Entry<String, Object>> entrySet() {
+        if ( this.isReadOnly ) {
+            return this.getUnmodifiableMap().entrySet();
+        }
+        return super.entrySet();
     }
 
     @Override
-    public int size() {
-        return this.dataMap.size();
+    public Set<String> keySet() {
+        if ( this.isReadOnly ) {
+            return this.getUnmodifiableMap().keySet();
+        }
+        return super.keySet();
     }
 
     @Override
     public Collection<Object> values() {
-        return this.dataMap.values();
-    }
-
-    @Override
-    public String toString() {
-        return "Resource Metadata: " + this.dataMap.toString();
+        if ( this.isReadOnly ) {
+            return this.getUnmodifiableMap().values();
+        }
+        return super.values();
     }
 }