KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > support > BlobSerializableTypeHandler


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.ibatis.support;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import org.springframework.jdbc.support.lob.LobCreator;
29 import org.springframework.jdbc.support.lob.LobHandler;
30
31 /**
32  * iBATIS TypeHandler implementation for arbitrary objects that get serialized to BLOBs.
33  * Retrieves the LobHandler to use from SqlMapClientFactoryBean at config time.
34  *
35  * <p>Can also be defined in generic iBATIS mappings, as DefaultLobCreator will
36  * work with most JDBC-compliant database drivers. In this case, the field type
37  * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
38  * large enough binary type will work.
39  *
40  * @author Juergen Hoeller
41  * @since 1.1.5
42  * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#setLobHandler
43  */

44 public class BlobSerializableTypeHandler extends AbstractLobTypeHandler {
45
46     /**
47      * Constructor used by iBATIS: fetches config-time LobHandler from
48      * SqlMapClientFactoryBean.
49      * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#getConfigTimeLobHandler
50      */

51     public BlobSerializableTypeHandler() {
52         super();
53     }
54
55     /**
56      * Constructor used for testing: takes an explicit LobHandler.
57      */

58     protected BlobSerializableTypeHandler(LobHandler lobHandler) {
59         super(lobHandler);
60     }
61
62     protected void setParameterInternal(
63             PreparedStatement JavaDoc ps, int index, Object JavaDoc value, String JavaDoc jdbcType, LobCreator lobCreator)
64             throws SQLException JavaDoc, IOException JavaDoc {
65
66         if (value != null) {
67             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
68             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
69             try {
70                 oos.writeObject(value);
71                 oos.flush();
72                 lobCreator.setBlobAsBytes(ps, index, baos.toByteArray());
73             }
74             finally {
75                 oos.close();
76             }
77         }
78         else {
79             lobCreator.setBlobAsBytes(ps, index, null);
80         }
81     }
82
83     protected Object JavaDoc getResultInternal(ResultSet JavaDoc rs, int index, LobHandler lobHandler)
84             throws SQLException JavaDoc, IOException JavaDoc {
85
86         InputStream JavaDoc is = lobHandler.getBlobAsBinaryStream(rs, index);
87         if (is != null) {
88             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
89             try {
90                 return ois.readObject();
91             }
92             catch (ClassNotFoundException JavaDoc ex) {
93                 throw new SQLException JavaDoc("Could not deserialize BLOB contents: " + ex.getMessage());
94             }
95             finally {
96                 ois.close();
97             }
98         }
99         else {
100             return null;
101         }
102     }
103
104     public Object JavaDoc valueOf(String JavaDoc s) {
105         return s;
106     }
107
108 }
109
Popular Tags