svn commit: r1553890 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: jdbc/JdbcValueHandler.java test/EntityTestSuite.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1553890 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: jdbc/JdbcValueHandler.java test/EntityTestSuite.java

adrianc
Author: adrianc
Date: Sat Dec 28 19:15:58 2013
New Revision: 1553890

URL: http://svn.apache.org/r1553890
Log:
A small code cleanup in the entity engine - remove deprecated functionality. Originally, the "blob" field type was used as a catch-all for java.sql.Blob, byte[], and java.lang.Object. Now each Java type has its own field type - so you must specify the correct one.

IMPORTANT: This commit might break some applications. If you get ClassCastExceptions, then you need to change the field type in your entity from blob to either byte-array or object. The change will have no effect on your database - because they are all persisted as BLOB SQL types.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java?rev=1553890&r1=1553889&r2=1553890&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java Sat Dec 28 19:15:58 2013
@@ -18,14 +18,12 @@
  *******************************************************************************/
 package org.ofbiz.entity.jdbc;
 
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Reader;
-import java.nio.ByteBuffer;
 import java.sql.Blob;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -186,31 +184,6 @@ public abstract class JdbcValueHandler<T
         return os.toByteArray();
     }
 
-    protected static byte[] toByteArray(java.sql.Blob blob) throws SQLException {
-        InputStream inStream = null;
-        try {
-            inStream = blob.getBinaryStream();
-            int blobLength = (int) blob.length();
-            byte[] byteBuffer = new byte[blobLength];
-            int offset = 0;
-            int bytesRead = inStream.read(byteBuffer, offset, blobLength);
-            while (bytesRead > 0) {
-                offset += bytesRead;
-                bytesRead = inStream.read(byteBuffer, offset, blobLength);
-            }
-            return byteBuffer;
-        } catch (Exception e) {
-            throw new SQLException(e);
-        }
-        finally {
-            if (inStream != null) {
-                try {
-                    inStream.close();
-                } catch (IOException e) {}
-            }
-        }
-    }
-
     // The target database SQL type to be used for the
     // PreparedStatement.setNull method.
     private final int sqlType;
@@ -324,67 +297,13 @@ public abstract class JdbcValueHandler<T
         }
         @Override
         protected void castAndSetValue(PreparedStatement ps, int parameterIndex, Object obj) throws SQLException {
-            // FIXME: This is here for backwards compatibility. Client code
-            // that uses a Blob java-type for a byte array should use a
-            // byte[] java-type instead.
-            if (obj instanceof Blob) {
-                ps.setBlob(parameterIndex, (Blob)obj);
-            } else if (obj instanceof byte[]) {
-                ps.setBytes(parameterIndex, (byte[]) obj);
-            } else if (obj instanceof ByteBuffer) {
-                ps.setBytes(parameterIndex, ((ByteBuffer)obj).array());
-            } else {
-                Debug.logError("JdbcValueHandler.castAndSetValue(): Unexpected type found. type=" + obj.getClass().getName(), module);
-                throw new IllegalArgumentException(obj.getClass().getName());
-            }
-            return;
+            ps.setBlob(parameterIndex, (Blob) obj);
         }
         @Override
         public Object getValue(ResultSet rs, int columnIndex) throws SQLException {
-            // FIXME: This code is here for backwards compatibility. Client code
-            // that uses a Blob java-type for non-Blob types should be updated
-            // to use the correct type.
-            Object originalObject;
-            byte[] fieldBytes;
-            try {
-                Blob theBlob = rs.getBlob(columnIndex);
-                fieldBytes = toByteArray(theBlob);
-                originalObject = theBlob;
-            } catch (SQLException e) {
-                // for backward compatibility if getBlob didn't work try getBytes
-                fieldBytes = rs.getBytes(columnIndex);
-                originalObject = fieldBytes;
-            }
-            if (originalObject != null) {
-                // for backward compatibility, check to see if there is a serialized object and if so return that
-                Object blobObject = null;
-                ObjectInputStream in = null;
-                try {
-                    in = new ObjectInputStream(new ByteArrayInputStream(fieldBytes));
-                    blobObject = in.readObject();
-                } catch (IOException e) {
-                    if (Debug.verboseOn()) Debug.logVerbose(e, "Unable to read BLOB data from input stream", module);
-                } catch (ClassNotFoundException e) {
-                    if (Debug.verboseOn()) Debug.logVerbose(e, "Class not found: Unable to cast BLOB data to an Java object while getting value, most likely because it is a straight byte[], so just using the raw bytes", module);
-                } finally {
-                    if (in != null) {
-                        try {
-                            in.close();
-                        } catch (IOException e) {}
-                    }
-                }
-                if (blobObject != null) {
-                    Debug.logWarning("Blob java-type used for java.lang.Object. Use java.lang.Object java-type instead.", module);
-                    return blobObject;
-                } else {
-                    if (originalObject instanceof Blob) {
-                        // NOTE using SerialBlob here instead of the Blob from the database to make sure we can pass it around, serialize it, etc
-                        return new SerialBlob((Blob) originalObject);
-                    } else {
-                        Debug.logWarning("Blob java-type used for byte array. Use byte[] java-type instead.", module);
-                        return originalObject;
-                    }
-                }
+            Blob fieldBlob = rs.getBlob(columnIndex);
+            if (fieldBlob != null) {
+                return new SerialBlob(fieldBlob);
             }
             return null;
         }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java?rev=1553890&r1=1553889&r2=1553890&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Sat Dec 28 19:15:58 2013
@@ -28,6 +28,8 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
+import javax.sql.rowset.serial.SerialBlob;
+
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.Observable;
 import org.ofbiz.base.util.Observer;
@@ -44,8 +46,8 @@ import org.ofbiz.entity.condition.Entity
 import org.ofbiz.entity.condition.EntityConditionList;
 import org.ofbiz.entity.condition.EntityExpr;
 import org.ofbiz.entity.condition.EntityOperator;
-import org.ofbiz.entity.config.model.Datasource;
 import org.ofbiz.entity.config.EntityConfigUtil;
+import org.ofbiz.entity.config.model.Datasource;
 import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.model.ModelField;
 import org.ofbiz.entity.testtools.EntityTestCase;
@@ -627,6 +629,7 @@ public class EntityTestSuite extends Ent
         for (int i = 0; i < b.length; i++) {
             b[i] = (byte) i;
         }
+        Blob testBlob = new SerialBlob(b);
         String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         StringBuilder sb = new StringBuilder(alpha.length() * 1000);
         for (int i = 0; i < 1000; i++) {
@@ -647,7 +650,7 @@ public class EntityTestSuite extends Ent
         try {
             GenericValue testValue = delegator.makeValue("TestFieldType", "testFieldTypeId", id);
             testValue.create();
-            testValue.set("blobField", b);
+            testValue.set("blobField", testBlob);
             testValue.set("byteArrayField", b);
             testValue.set("objectField", currentTimestamp);
             testValue.set("dateField", currentDate);
@@ -660,13 +663,8 @@ public class EntityTestSuite extends Ent
             testValue.store();
             testValue = delegator.findOne("TestFieldType", UtilMisc.toMap("testFieldTypeId", id), false);
             assertEquals("testFieldTypeId", id, testValue.get("testFieldTypeId"));
-            byte[] c = null;
-            try {
-                Blob blob = (Blob) testValue.get("blobField");
-                c = blob.getBytes(1, (int) blob.length());
-            } catch (ClassCastException e) {
-                c = (byte[]) testValue.get("blobField");
-            }
+            Blob blob = (Blob) testValue.get("blobField");
+            byte[] c = blob.getBytes(1, (int) blob.length());
             assertEquals("Byte array read from entity is the same length", b.length, c.length);
             for (int i = 0; i < b.length; i++) {
                 assertEquals("Byte array data[" + i + "]", b[i], c[i]);