KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > SerializableToBlobType


1 //$Id: SerializableToBlobType.java,v 1.2 2005/07/25 06:43:41 epbernard Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.sql.Blob JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Types JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import org.dom4j.Node;
15 import org.hibernate.EntityMode;
16 import org.hibernate.HibernateException;
17 import org.hibernate.MappingException;
18 import org.hibernate.lob.BlobImpl;
19 import org.hibernate.engine.Mapping;
20 import org.hibernate.engine.SessionFactoryImplementor;
21 import org.hibernate.engine.SessionImplementor;
22 import org.hibernate.usertype.ParameterizedType;
23 import org.hibernate.util.ReflectHelper;
24 import org.hibernate.util.SerializationHelper;
25
26 /**
27  * @author Emmanuel Bernard
28  */

29 public class SerializableToBlobType extends AbstractLobType implements ParameterizedType {
30     /** class name of the serialisable class */
31     public static final String JavaDoc CLASS_NAME = "classname";
32     private Class JavaDoc serializableClass;
33     private SerializableType type;
34
35     public int[] sqlTypes(Mapping mapping) throws MappingException {
36         return new int[] { Types.BLOB };
37     }
38
39     public Class JavaDoc getReturnedClass() {
40         return serializableClass;
41     }
42
43     @Override JavaDoc public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode, SessionFactoryImplementor factory) {
44         return type.isEqual(x, y);
45     }
46
47
48     @Override JavaDoc public int getHashCode(Object JavaDoc x, EntityMode entityMode, SessionFactoryImplementor session) {
49         return type.getHashCode(x, null);
50     }
51
52     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
53         Blob JavaDoc blob = rs.getBlob( name );
54         if ( rs.wasNull() ) return null;
55         int length = (int) blob.length();
56         byte[] primaryResult = blob.getBytes( 1, length );
57         return fromBytes(primaryResult);
58     }
59
60     private static byte[] toBytes(Object JavaDoc object) throws SerializationException {
61         return SerializationHelper.serialize( (Serializable JavaDoc) object );
62     }
63
64     private static Object JavaDoc fromBytes( byte[] bytes ) throws SerializationException {
65         return SerializationHelper.deserialize(bytes);
66     }
67
68     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session) throws SQLException JavaDoc {
69         byte[] toSet;
70         toSet = toBytes(value);
71         if ( session.getFactory().getDialect().useInputStreamToInsertBlob() ) {
72             st.setBinaryStream( index, new ByteArrayInputStream JavaDoc( toSet ), toSet.length );
73         }
74         else {
75             st.setBlob( index, new BlobImpl(toSet) );
76         }
77     }
78
79     public void setToXMLNode(Node node, Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
80         type.setToXMLNode( node, value, factory );
81     }
82
83     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
84         return type.toLoggableString( value, factory );
85     }
86
87     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
88         return type.fromXMLNode( xml, factory );
89     }
90
91     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory)
92             throws HibernateException {
93         return type.deepCopy(value, null, null);
94     }
95
96     public boolean isMutable() {
97         return type.isMutable();
98     }
99
100     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, SessionImplementor session, Object JavaDoc owner, Map JavaDoc copyCache)
101             throws HibernateException {
102         return type.replace( original, target, session, owner, copyCache);
103     }
104
105     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
106         return type.toColumnNullness( value, mapping);
107     }
108
109     public void setParameterValues(Properties JavaDoc parameters) {
110         if( parameters != null ) {
111             String JavaDoc className = parameters.getProperty(CLASS_NAME);
112             if (className == null) throw new MappingException("No class name defined for type: " + SerializableToBlobType.class.getName() );
113             try {
114                 serializableClass = ReflectHelper.classForName(className);
115             }
116             catch (ClassNotFoundException JavaDoc e) {
117                 throw new MappingException("Unable to load class from " + CLASS_NAME + " parameter", e);
118             }
119         }
120         type = new SerializableType(serializableClass);
121     }
122 }
123
Popular Tags