sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1633593 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/ main/java/org/apache/sling/jcr/resource/internal/ test/java/org/apache/sling/jcr/resource/internal/
Date Wed, 22 Oct 2014 10:31:06 GMT
Author: cziegeler
Date: Wed Oct 22 10:31:06 2014
New Revision: 1633593

URL: http://svn.apache.org/r1633593
Log:
SLING-4088 : Regression introduced by SLING-3846 : JcrModifiableValueMap: Date object not
supported for writing data

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=1633593&r1=1633592&r2=1633593&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Wed Oct 22 10:31:06 2014
@@ -22,7 +22,6 @@ import java.io.InputStream;
 import java.lang.reflect.Array;
 import java.math.BigDecimal;
 import java.util.Calendar;
-import java.util.Date;
 import java.util.StringTokenizer;
 
 import javax.jcr.Node;
@@ -137,10 +136,6 @@ public class JcrResourceUtil {
         ValueFactory fac = session.getValueFactory();
         if(value instanceof Calendar) {
             val = fac.createValue((Calendar)value);
-        } else if(value instanceof Date) {
-            Calendar calendarValue = Calendar.getInstance();
-            calendarValue.setTime((Date)value);
-            val = fac.createValue(calendarValue);
         } else if (value instanceof InputStream) {
             val = fac.createValue(fac.createBinary((InputStream)value));
         } else if (value instanceof Node) {

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java?rev=1633593&r1=1633592&r2=1633593&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
Wed Oct 22 10:31:06 2014
@@ -523,10 +523,26 @@ public final class JcrModifiableValueMap
             return (T) Boolean.valueOf(jcrValue.getBoolean());
 
         } else if (Date.class == type) {
-            return (T) jcrValue.getDate().getTime();
+            if ( jcrValue.getType() == PropertyType.BINARY ) {
+                final Object obj = deserializeObject(jcrValue);
+                if ( obj instanceof Date) {
+                    return (T) obj;
+                }
+            } else {
+                return (T) jcrValue.getDate().getTime();
+            }
 
         } else if (Calendar.class == type) {
-            return (T) jcrValue.getDate();
+            if ( jcrValue.getType() == PropertyType.BINARY ) {
+                final Object obj = deserializeObject(jcrValue);
+                if ( obj instanceof Date) {
+                    final Calendar c = Calendar.getInstance();
+                    c.setTime((Date)obj);
+                    return (T) c;
+                }
+            } else {
+                return (T) jcrValue.getDate();
+            }
 
         } else if (Value.class == type) {
             return (T) jcrValue;
@@ -544,25 +560,9 @@ public final class JcrModifiableValueMap
             }
         } else if (Serializable.class.isAssignableFrom(type)
                 && jcrValue.getType() == PropertyType.BINARY) {
-            ObjectInputStream ois = null;
-            try {
-                ois = new ObjectInputStream(jcrValue.getBinary().getStream(), this.dynamicClassLoader);
-                final Object obj = ois.readObject();
-                if ( type.isInstance(obj) ) {
-                    return (T)obj;
-                }
-            } catch (ClassNotFoundException cnfe) {
-                 // ignore and use fallback
-            } catch (IOException ioe) {
-                // ignore and use fallback
-            } finally {
-                if ( ois != null ) {
-                    try {
-                        ois.close();
-                    } catch (IOException ignore) {
-                        // ignore
-                    }
-                }
+            final Object obj = deserializeObject(jcrValue);
+            if ( type.isInstance(obj) ) {
+                return (T)obj;
             }
         }
 
@@ -570,6 +570,34 @@ public final class JcrModifiableValueMap
         return null;
     }
 
+    /**
+     * Deserialize a binary object
+     * @param jcrValue The jcr property value
+     * @return The object or {@code null}
+     * @throws RepositoryException
+     */
+    private Object deserializeObject(final Value jcrValue) throws RepositoryException {
+        ObjectInputStream ois = null;
+        try {
+            ois = new ObjectInputStream(jcrValue.getBinary().getStream(), this.dynamicClassLoader);
+            final Object obj = ois.readObject();
+            return obj;
+        } catch (ClassNotFoundException cnfe) {
+             // ignore and use fallback
+        } catch (IOException ioe) {
+            // ignore and use fallback
+        } finally {
+            if ( ois != null ) {
+                try {
+                    ois.close();
+                } catch (IOException ignore) {
+                    // ignore
+                }
+            }
+        }
+        return null;
+    }
+
     private Class<?> normalizeClass(Class<?> type) {
         if (Calendar.class.isAssignableFrom(type)) {
             type = Calendar.class;

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java?rev=1633593&r1=1633592&r2=1633593&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
Wed Oct 22 10:31:06 2014
@@ -254,18 +254,18 @@ public class JcrModifiableValueMapTest e
             assertEquals(values.get(entry.getKey()), stored);
         }
     }
-    
+
     /**
      * Test date conversions from Date to Calendar and vice versa when reading or writing
from value maps.
      */
     public void testDateConversion() throws Exception {
         this.rootNode.getSession().refresh(false);
-        
+
         // prepare some date values
         Date dateValue1 = new Date(10000);
         Calendar calendarValue1 = Calendar.getInstance();
         calendarValue1.setTime(dateValue1);
-        
+
         Date dateValue2 = new Date(20000);
         Calendar calendarValue2 = Calendar.getInstance();
         calendarValue2.setTime(dateValue2);
@@ -296,9 +296,8 @@ public class JcrModifiableValueMapTest e
 
         // read properties
         assertEquals(calendarValue1, testNode.getProperty(PROP1).getDate());
-        assertEquals(calendarValue2, testNode.getProperty(PROP2).getDate());
         assertEquals(calendarValue3, testNode.getProperty(PROP3).getDate());
-        
+
     }
-    
+
 }



Mime
View raw message