001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.hibernate;
023:
024: import org.hibernate.HibernateException;
025: import org.hibernate.usertype.UserType;
026:
027: import java.io.Serializable;
028: import java.sql.Blob;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Types;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8784 $
037: */
038: public class CacheableBlobUserType implements UserType {
039:
040: /** . */
041: private static final int[] SQL_TYPES = { Types.BLOB };
042:
043: public int[] sqlTypes() {
044: return SQL_TYPES;
045: }
046:
047: public Class returnedClass() {
048: return Blob.class;
049: }
050:
051: public boolean equals(Object x, Object y) throws HibernateException {
052: if (x == y) {
053: return true;
054: }
055: if (x != null && y != null) {
056: try {
057: ByteArrayBlob _x = ByteArrayBlob.get((Blob) x);
058: ByteArrayBlob _y = ByteArrayBlob.get((Blob) y);
059: return java.util.Arrays.equals(_x._bytes, _y._bytes);
060: } catch (Exception e) {
061: throw new HibernateException(e);
062: }
063: }
064: return false;
065: }
066:
067: public int hashCode(Object object) throws HibernateException {
068: return object.hashCode();
069: }
070:
071: public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
072: throws HibernateException, SQLException {
073: Blob blob = rs.getBlob(names[0]);
074: return rs.wasNull() ? null : new ByteArrayBlob(blob);
075: }
076:
077: public void nullSafeSet(PreparedStatement st, Object value,
078: int index) throws HibernateException, SQLException {
079: if (value == null) {
080: st.setNull(index, Types.BLOB);
081: } else {
082: st.setBlob(index, (Blob) value);
083: }
084: // if (value==null) {
085: // st.setNull(index, Types.BLOB);
086: // }
087: // else {
088: //
089: // if (value instanceof SerializableBlob) {
090: // value = ( (SerializableBlob) value ).getWrappedBlob();
091: // }
092: //
093: // final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob() &&
094: // (value instanceof BlobImpl);
095: //
096: // if ( useInputStream ) {
097: // BlobImpl blob = (BlobImpl) value;
098: // st.setBinaryStream( index, blob.getBinaryStream(), (int) blob.length() );
099: // }
100: // else {
101: // st.setBlob(index, (Blob) value);
102: // }
103: //
104: // }
105: }
106:
107: public Object deepCopy(Object value) throws HibernateException {
108: if (value == null) {
109: return null;
110: }
111: try {
112: return ByteArrayBlob.create((Blob) value);
113: } catch (Exception e) {
114: throw new HibernateException(e);
115: }
116: }
117:
118: public boolean isMutable() {
119: return true;
120: }
121:
122: public Serializable disassemble(Object value)
123: throws HibernateException {
124: if (value == null) {
125: return null;
126: }
127: return ((ByteArrayBlob) value)._bytes;
128: }
129:
130: public Object assemble(Serializable cached, Object owner)
131: throws HibernateException {
132: if (cached == null) {
133: return null;
134: }
135: return new ByteArrayBlob((byte[]) cached);
136: }
137:
138: public Object replace(Object original, Object target, Object owner)
139: throws HibernateException {
140: return original;
141: }
142:
143: }
|