~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/type/SerializableType.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
4
 
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
 
 * indicated by the @author tags or express copyright attribution
6
 
 * statements applied by the authors.  All third-party contributions are
7
 
 * distributed under license by Red Hat Middleware LLC.
8
 
 *
9
 
 * This copyrighted material is made available to anyone wishing to use, modify,
10
 
 * copy, or redistribute it subject to the terms and conditions of the GNU
11
 
 * Lesser General Public License, as published by the Free Software Foundation.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16
 
 * for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License
19
 
 * along with this distribution; if not, write to:
20
 
 * Free Software Foundation, Inc.
21
 
 * 51 Franklin Street, Fifth Floor
22
 
 * Boston, MA  02110-1301  USA
23
 
 *
24
 
 */
25
 
package org.hibernate.type;
26
 
 
27
 
import java.io.Serializable;
28
 
import java.sql.PreparedStatement;
29
 
import java.sql.ResultSet;
30
 
import java.sql.SQLException;
31
 
 
32
 
import org.hibernate.EntityMode;
33
 
import org.hibernate.Hibernate;
34
 
import org.hibernate.HibernateException;
35
 
import org.hibernate.engine.SessionImplementor;
36
 
import org.hibernate.util.SerializationHelper;
37
 
 
38
 
/**
39
 
 * <tt>serializable</tt>: A type that maps an SQL VARBINARY to a
40
 
 * serializable Java object.
41
 
 * @author Gavin King
42
 
 */
43
 
public class SerializableType extends MutableType {
44
 
 
45
 
        private final Class serializableClass;
46
 
 
47
 
        public SerializableType(Class serializableClass) {
48
 
                this.serializableClass = serializableClass;
49
 
        }
50
 
 
51
 
        public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
52
 
                Hibernate.BINARY.set(st, toBytes(value), index);
53
 
        }
54
 
 
55
 
        public Object get(ResultSet rs, String name) throws HibernateException, SQLException {
56
 
                byte[] bytes = (byte[]) Hibernate.BINARY.get(rs, name);
57
 
                // Some JDBC drivers erroneously return an empty array here for a null DB value :/
58
 
                if ( bytes == null || bytes.length == 0 ) {
59
 
                        return null;
60
 
                }
61
 
                else {
62
 
                        return fromBytes(bytes);
63
 
                }
64
 
        }
65
 
 
66
 
        public Class getReturnedClass() {
67
 
                return serializableClass;
68
 
        }
69
 
 
70
 
        public boolean isEqual(Object x, Object y) throws HibernateException {
71
 
                if ( x == y ) {
72
 
                        return true;
73
 
                }
74
 
                if ( x == null || y == null ) {
75
 
                        return false;
76
 
                }
77
 
                return x.equals( y ) || Hibernate.BINARY.isEqual( toBytes( x ), toBytes( y ) );
78
 
        }
79
 
 
80
 
        public int getHashCode(Object x, EntityMode entityMode) {
81
 
                return Hibernate.BINARY.getHashCode( toBytes(x), entityMode );
82
 
        }
83
 
 
84
 
        public String toString(Object value) throws HibernateException {
85
 
                return Hibernate.BINARY.toString( toBytes(value) );
86
 
        }
87
 
 
88
 
        public Object fromStringValue(String xml) throws HibernateException {
89
 
                return fromBytes( (byte[]) Hibernate.BINARY.fromStringValue(xml) );
90
 
        }
91
 
 
92
 
        public String getName() {
93
 
                return (serializableClass==Serializable.class) ? "serializable" : serializableClass.getName();
94
 
        }
95
 
 
96
 
        public Object deepCopyNotNull(Object value) throws HibernateException {
97
 
                return fromBytes( toBytes(value) );
98
 
        }
99
 
 
100
 
        private static byte[] toBytes(Object object) throws SerializationException {
101
 
                return SerializationHelper.serialize( (Serializable) object );
102
 
        }
103
 
 
104
 
        private static Object fromBytes( byte[] bytes ) throws SerializationException {
105
 
                return SerializationHelper.deserialize(bytes);
106
 
        }
107
 
 
108
 
        public int sqlType() {
109
 
                return Hibernate.BINARY.sqlType();
110
 
        }
111
 
 
112
 
        public Object assemble(Serializable cached, SessionImplementor session, Object owner)
113
 
        throws HibernateException {
114
 
                return (cached==null) ? null : fromBytes( (byte[]) cached );
115
 
        }
116
 
 
117
 
        public Serializable disassemble(Object value, SessionImplementor session, Object owner)
118
 
        throws HibernateException {
119
 
                return (value==null) ? null : toBytes(value);
120
 
        }
121
 
 
122
 
}
123
 
 
124
 
 
125
 
 
126
 
 
127
 
 
128
 
 
129