KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > support > BlobStringType


1 /*
2  * Copyright 2002-2006 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.UnsupportedEncodingException JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import org.springframework.jdbc.support.lob.LobCreator;
28 import org.springframework.jdbc.support.lob.LobHandler;
29
30 /**
31  * Hibernate UserType implementation for Strings that get mapped to BLOBs.
32  * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
33  *
34  * <p>This is intended for the (arguably unnatural, but still common) case
35  * where character data is stored in a binary LOB. This requires encoding
36  * and decoding the characters within this UserType; see the javadoc of the
37  * <code>getCharacterEncoding()</code> method.
38  *
39  * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
40  * work with most JDBC-compliant database drivers. In this case, the field type
41  * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
42  * large enough binary type will work.
43  *
44  * @author Juergen Hoeller
45  * @since 1.2.7
46  * @see #getCharacterEncoding()
47  * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler
48  */

49 public class BlobStringType extends AbstractLobType {
50
51     /**
52      * Constructor used by Hibernate: fetches config-time LobHandler and
53      * config-time JTA TransactionManager from LocalSessionFactoryBean.
54      * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
55      * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager
56      */

57     public BlobStringType() {
58         super();
59     }
60
61     /**
62      * Constructor used for testing: takes an explicit LobHandler
63      * and an explicit JTA TransactionManager (can be <code>null</code>).
64      */

65     protected BlobStringType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
66         super(lobHandler, jtaTransactionManager);
67     }
68
69     public int[] sqlTypes() {
70         return new int[] {Types.BLOB};
71     }
72
73     public Class JavaDoc returnedClass() {
74         return String JavaDoc.class;
75     }
76
77     protected Object JavaDoc nullSafeGetInternal(
78             ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner, LobHandler lobHandler)
79             throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc {
80
81         byte[] bytes = lobHandler.getBlobAsBytes(rs, names[0]);
82         if (bytes != null) {
83             String JavaDoc encoding = getCharacterEncoding();
84             return (encoding != null ? new String JavaDoc(bytes, encoding) : new String JavaDoc(bytes));
85         }
86         else {
87             return null;
88         }
89     }
90
91     protected void nullSafeSetInternal(
92             PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
93             throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc {
94
95         if (value != null) {
96             String JavaDoc str = (String JavaDoc) value;
97             String JavaDoc encoding = getCharacterEncoding();
98             byte[] bytes = (encoding != null ? str.getBytes(encoding) : str.getBytes());
99             lobCreator.setBlobAsBytes(ps, index, bytes);
100         }
101         else {
102             lobCreator.setBlobAsBytes(ps, index, null);
103         }
104     }
105
106     /**
107      * Determine the character encoding to apply to the BLOB's bytes
108      * to turn them into a String.
109      * <p>Default is <code>null</code>, indicating to use the platform
110      * default encoding. To be overridden in subclasses for a specific
111      * encoding such as "ISO-8859-1" or "UTF-8".
112      * @return the character encoding to use, or <code>null</code>
113      * to use the platform default encoding
114      * @see java.lang.String#String(byte[], String)
115      * @see java.lang.String#getBytes(String)
116      */

117     protected String JavaDoc getCharacterEncoding() {
118         return null;
119     }
120
121 }
122
Popular Tags