| 1 17 18 23 package org.pentaho.repository.usertypes; 24 25 import java.io.InputStream ; 26 import java.io.Serializable ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.hibernate.HibernateException; 34 import org.hibernate.type.SerializationException; 35 import org.hibernate.usertype.UserType; 36 import org.hibernate.util.EqualsHelper; 37 import org.hibernate.util.SerializationHelper; 38 import org.pentaho.core.system.PentahoSystem; 39 import org.pentaho.messages.Messages; 40 41 public class BlobUserType implements UserType { 42 private static Log log = LogFactory.getLog(BlobUserType.class); 43 44 private final static boolean debug = PentahoSystem.debug; 45 46 private static final int[] SQLTYPE = { Types.BLOB }; 47 48 53 public int[] sqlTypes() { 54 return SQLTYPE; 55 } 56 57 62 public Class returnedClass() { 63 return Serializable .class; 64 } 65 66 72 public boolean equals(Object arg0, Object arg1) throws HibernateException { 73 return EqualsHelper.equals(arg0, arg1); 74 } 75 76 81 public int hashCode(Object arg0) throws HibernateException { 82 return arg0.hashCode(); 83 } 84 85 91 public Object nullSafeGet(ResultSet arg0, String [] arg1, Object arg2) throws HibernateException, SQLException { 92 if (debug) 93 log.debug(Messages.getString("BLOBUTYPE.DEBUG_NULL_SAFE_GET")); InputStream is = arg0.getBinaryStream(arg1[0]); 95 if (is != null) { 96 return SerializationHelper.deserialize(is); 97 } 98 return null; 99 } 100 101 107 public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2) throws HibernateException, SQLException { 108 if (debug) 109 log.debug(Messages.getString("BLOBUTYPE.DEBUG_NULL_SAFE_SET")); if (arg1 != null) { 111 try { 112 arg0.setBytes(arg2, SerializationHelper.serialize((Serializable ) arg1)); 113 } catch (SerializationException ex) { 114 log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ex); throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ex); } 117 } else { 118 arg0.setNull(arg2, Types.BLOB); 119 } 120 } 121 122 127 public Object deepCopy(Object arg0) throws HibernateException { 128 return SerializationHelper.clone((Serializable ) arg0); 129 } 130 131 136 public boolean isMutable() { 137 return true; 138 } 139 140 145 public Serializable disassemble(Object arg0) throws HibernateException { 146 return (Serializable ) SerializationHelper.clone((Serializable ) arg0); 147 } 148 149 155 public Object assemble(Serializable arg0, Object arg1) throws HibernateException { 156 return SerializationHelper.clone(arg0); 157 } 158 159 165 public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException { 166 return SerializationHelper.clone((Serializable ) arg0); 167 } 168 169 } 170 | Popular Tags |