KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 import javax.transaction.Status JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.hibernate.HibernateException;
31 import org.hibernate.usertype.UserType;
32 import org.hibernate.util.EqualsHelper;
33
34 import org.springframework.dao.DataAccessResourceFailureException;
35 import org.springframework.jdbc.support.lob.JtaLobCreatorSynchronization;
36 import org.springframework.jdbc.support.lob.LobCreator;
37 import org.springframework.jdbc.support.lob.LobHandler;
38 import org.springframework.jdbc.support.lob.SpringLobCreatorSynchronization;
39 import org.springframework.jdbc.support.lob.LobCreatorUtils;
40 import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
41 import org.springframework.transaction.support.TransactionSynchronizationManager;
42
43 /**
44  * Abstract base class for Hibernate UserType implementations that map to LOBs.
45  * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
46  *
47  * <p>For writing LOBs, either an active Spring transaction synchronization
48  * or an active JTA transaction (with "jtaTransactionManager" specified on
49  * LocalSessionFactoryBean or a Hibernate TransactionManagerLookup configured
50  * through the corresponding Hibernate property) is required.
51  *
52  * <p>Offers template methods for setting parameters and getting result values,
53  * passing in the LobHandler or LobCreator to use.
54  *
55  * @author Juergen Hoeller
56  * @since 1.2
57  * @see org.springframework.jdbc.support.lob.LobHandler
58  * @see org.springframework.jdbc.support.lob.LobCreator
59  * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler
60  * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setJtaTransactionManager
61  */

62 public abstract class AbstractLobType implements UserType {
63
64     protected final Log logger = LogFactory.getLog(getClass());
65
66     private final LobHandler lobHandler;
67
68     private final TransactionManager JavaDoc jtaTransactionManager;
69
70
71     /**
72      * Constructor used by Hibernate: fetches config-time LobHandler and
73      * config-time JTA TransactionManager from LocalSessionFactoryBean.
74      * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
75      * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager
76      */

77     protected AbstractLobType() {
78         this(LocalSessionFactoryBean.getConfigTimeLobHandler(),
79             LocalSessionFactoryBean.getConfigTimeTransactionManager());
80     }
81
82     /**
83      * Constructor used for testing: takes an explicit LobHandler
84      * and an explicit JTA TransactionManager (can be <code>null</code>).
85      */

86     protected AbstractLobType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
87         this.lobHandler = lobHandler;
88         this.jtaTransactionManager = jtaTransactionManager;
89     }
90
91
92     /**
93      * This implementation returns false.
94      */

95     public boolean isMutable() {
96         return false;
97     }
98
99     /**
100      * This implementation delegates to the Hibernate EqualsHelper.
101      * @see org.hibernate.util.EqualsHelper#equals
102      */

103     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
104         return EqualsHelper.equals(x, y);
105     }
106
107     /**
108      * This implementation returns the hashCode of the given objectz.
109      */

110     public int hashCode(Object JavaDoc x) throws HibernateException {
111         return x.hashCode();
112     }
113
114     /**
115      * This implementation returns the passed-in value as-is.
116      */

117     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
118         return value;
119     }
120
121     /**
122      * This implementation returns the passed-in value as-is.
123      */

124     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
125         return (Serializable JavaDoc) value;
126     }
127
128     /**
129      * This implementation returns the passed-in value as-is.
130      */

131     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
132         return cached;
133     }
134
135     /**
136      * This implementation returns the passed-in original as-is.
137      */

138     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
139         return original;
140     }
141
142
143     /**
144      * This implementation delegates to nullSafeGetInternal,
145      * passing in the LobHandler of this type.
146      * @see #nullSafeGetInternal
147      */

148     public final Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner)
149             throws HibernateException, SQLException JavaDoc {
150
151         if (this.lobHandler == null) {
152             throw new IllegalStateException JavaDoc("No LobHandler found for configuration - " +
153                 "lobHandler property must be set on LocalSessionFactoryBean");
154         }
155
156         try {
157             return nullSafeGetInternal(rs, names, owner, this.lobHandler);
158         }
159         catch (IOException JavaDoc ex) {
160             throw new HibernateException("I/O errors during LOB access", ex);
161         }
162     }
163
164     /**
165      * This implementation delegates to nullSafeSetInternal,
166      * passing in a transaction-synchronized LobCreator for the
167      * LobHandler of this type.
168      * @see #nullSafeSetInternal
169      */

170     public final void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
171             throws HibernateException, SQLException JavaDoc {
172
173         if (this.lobHandler == null) {
174             throw new IllegalStateException JavaDoc("No LobHandler found for configuration - " +
175                 "lobHandler property must be set on LocalSessionFactoryBean");
176         }
177
178         LobCreator lobCreator = this.lobHandler.getLobCreator();
179         try {
180             nullSafeSetInternal(st, index, value, lobCreator);
181         }
182         catch (IOException JavaDoc ex) {
183             throw new HibernateException("I/O errors during LOB access", ex);
184         }
185         LobCreatorUtils.registerTransactionSynchronization(lobCreator, this.jtaTransactionManager);
186     }
187
188     /**
189      * Template method to extract a value from the given result set.
190      * @param rs the ResultSet to extract from
191      * @param names the column names
192      * @param owner the containing entity
193      * @param lobHandler the LobHandler to use
194      * @return the extracted value
195      * @throws SQLException if thrown by JDBC methods
196      * @throws IOException if thrown by streaming methods
197      * @throws HibernateException in case of any other exceptions
198      */

199     protected abstract Object JavaDoc nullSafeGetInternal(
200             ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner, LobHandler lobHandler)
201             throws SQLException JavaDoc, IOException JavaDoc, HibernateException;
202
203     /**
204      * Template method to set the given parameter value on the given statement.
205      * @param ps the PreparedStatement to set on
206      * @param index the statement parameter index
207      * @param value the value to set
208      * @param lobCreator the LobCreator to use
209      * @throws SQLException if thrown by JDBC methods
210      * @throws IOException if thrown by streaming methods
211      * @throws HibernateException in case of any other exceptions
212      */

213     protected abstract void nullSafeSetInternal(
214         PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
215             throws SQLException JavaDoc, IOException JavaDoc, HibernateException;
216
217 }
218
Popular Tags