KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > support > BlobSerializableType


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.hibernate.support;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Types JavaDoc;
30
31 import javax.transaction.TransactionManager JavaDoc;
32
33 import net.sf.hibernate.HibernateException;
34
35 import org.springframework.jdbc.support.lob.LobCreator;
36 import org.springframework.jdbc.support.lob.LobHandler;
37
38 /**
39  * Hibernate UserType implementation for arbitrary objects that get serialized to BLOBs.
40  * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
41  *
42  * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
43  * work with most JDBC-compliant database drivers. In this case, the field type
44  * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
45  * large enough binary type will work.
46  *
47  * @author Juergen Hoeller
48  * @since 1.1
49  * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setLobHandler
50  */

51 public class BlobSerializableType extends AbstractLobType {
52
53     /**
54      * Initial size for ByteArrayOutputStreams used for serialization output.
55      * <p>If a serialized object is larger than these 1024 bytes, the size of
56      * the byte array used by the output stream will be doubled each time the
57      * limit is reached.
58      */

59     private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 1024;
60
61     /**
62      * Constructor used by Hibernate: fetches config-time LobHandler and
63      * config-time JTA TransactionManager from LocalSessionFactoryBean.
64      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeLobHandler
65      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeTransactionManager
66      */

67     public BlobSerializableType() {
68         super();
69     }
70
71     /**
72      * Constructor used for testing: takes an explicit LobHandler
73      * and an explicit JTA TransactionManager (can be <code>null</code>).
74      */

75     protected BlobSerializableType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
76         super(lobHandler, jtaTransactionManager);
77     }
78
79     public int[] sqlTypes() {
80         return new int[] {Types.BLOB};
81     }
82
83     public Class JavaDoc returnedClass() {
84         return Serializable JavaDoc.class;
85     }
86
87     public boolean isMutable() {
88         return true;
89     }
90
91     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
92         try {
93             // Write to new byte array to clone.
94
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
95             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
96             try {
97                 oos.writeObject(value);
98             }
99             finally {
100                 oos.close();
101             }
102
103             // Read it back and return a true copy.
104
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
105             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
106             try {
107                 return ois.readObject();
108             }
109             finally {
110                 ois.close();
111             }
112         }
113         catch (ClassNotFoundException JavaDoc ex) {
114             throw new HibernateException("Couldn't clone BLOB contents", ex);
115         }
116         catch (IOException JavaDoc ex) {
117             throw new HibernateException("Couldn't clone BLOB contents", ex);
118         }
119     }
120
121     protected Object JavaDoc nullSafeGetInternal(ResultSet JavaDoc rs, int index, LobHandler lobHandler)
122             throws SQLException JavaDoc, IOException JavaDoc, HibernateException {
123
124         InputStream JavaDoc is = lobHandler.getBlobAsBinaryStream(rs, index);
125         if (is != null) {
126             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
127             try {
128                 return ois.readObject();
129             }
130             catch (ClassNotFoundException JavaDoc ex) {
131                 throw new HibernateException("Could not deserialize BLOB contents", ex);
132             }
133             finally {
134                 ois.close();
135             }
136         }
137         else {
138             return null;
139         }
140     }
141
142     protected void nullSafeSetInternal(PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
143             throws SQLException JavaDoc, IOException JavaDoc {
144
145         if (value != null) {
146             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
147             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
148             try {
149                 oos.writeObject(value);
150                 oos.flush();
151                 lobCreator.setBlobAsBytes(ps, index, baos.toByteArray());
152             }
153             finally {
154                 oos.close();
155             }
156         }
157         else {
158             lobCreator.setBlobAsBytes(ps, index, null);
159         }
160     }
161
162 }
163
Popular Tags