KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > 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.hibernate3.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 org.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.2
49  * @see org.springframework.orm.hibernate3.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.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
65      * @see org.springframework.orm.hibernate3.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(
122             ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner, LobHandler lobHandler)
123             throws SQLException JavaDoc, IOException JavaDoc, HibernateException {
124
125         InputStream JavaDoc is = lobHandler.getBlobAsBinaryStream(rs, names[0]);
126         if (is != null) {
127             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
128             try {
129                 return ois.readObject();
130             }
131             catch (ClassNotFoundException JavaDoc ex) {
132                 throw new HibernateException("Could not deserialize BLOB contents", ex);
133             }
134             finally {
135                 ois.close();
136             }
137         }
138         else {
139             return null;
140         }
141     }
142
143     protected void nullSafeSetInternal(
144             PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
145             throws SQLException JavaDoc, IOException JavaDoc {
146
147         if (value != null) {
148             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
149             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
150             try {
151                 oos.writeObject(value);
152                 oos.flush();
153                 lobCreator.setBlobAsBytes(ps, index, baos.toByteArray());
154             }
155             finally {
156                 oos.close();
157             }
158         }
159         else {
160             lobCreator.setBlobAsBytes(ps, index, null);
161         }
162     }
163
164 }
165
Popular Tags