001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.hibernate3.support;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024:
025: import javax.transaction.Status;
026: import javax.transaction.TransactionManager;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.hibernate.HibernateException;
031: import org.hibernate.usertype.UserType;
032: import org.hibernate.util.EqualsHelper;
033:
034: import org.springframework.dao.DataAccessResourceFailureException;
035: import org.springframework.jdbc.support.lob.JtaLobCreatorSynchronization;
036: import org.springframework.jdbc.support.lob.LobCreator;
037: import org.springframework.jdbc.support.lob.LobHandler;
038: import org.springframework.jdbc.support.lob.SpringLobCreatorSynchronization;
039: import org.springframework.jdbc.support.lob.LobCreatorUtils;
040: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
041: import org.springframework.transaction.support.TransactionSynchronizationManager;
042:
043: /**
044: * Abstract base class for Hibernate UserType implementations that map to LOBs.
045: * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
046: *
047: * <p>For writing LOBs, either an active Spring transaction synchronization
048: * or an active JTA transaction (with "jtaTransactionManager" specified on
049: * LocalSessionFactoryBean or a Hibernate TransactionManagerLookup configured
050: * through the corresponding Hibernate property) is required.
051: *
052: * <p>Offers template methods for setting parameters and getting result values,
053: * passing in the LobHandler or LobCreator to use.
054: *
055: * @author Juergen Hoeller
056: * @since 1.2
057: * @see org.springframework.jdbc.support.lob.LobHandler
058: * @see org.springframework.jdbc.support.lob.LobCreator
059: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler
060: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setJtaTransactionManager
061: */
062: public abstract class AbstractLobType implements UserType {
063:
064: protected final Log logger = LogFactory.getLog(getClass());
065:
066: private final LobHandler lobHandler;
067:
068: private final TransactionManager jtaTransactionManager;
069:
070: /**
071: * Constructor used by Hibernate: fetches config-time LobHandler and
072: * config-time JTA TransactionManager from LocalSessionFactoryBean.
073: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
074: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager
075: */
076: protected AbstractLobType() {
077: this (LocalSessionFactoryBean.getConfigTimeLobHandler(),
078: LocalSessionFactoryBean
079: .getConfigTimeTransactionManager());
080: }
081:
082: /**
083: * Constructor used for testing: takes an explicit LobHandler
084: * and an explicit JTA TransactionManager (can be <code>null</code>).
085: */
086: protected AbstractLobType(LobHandler lobHandler,
087: TransactionManager jtaTransactionManager) {
088: this .lobHandler = lobHandler;
089: this .jtaTransactionManager = jtaTransactionManager;
090: }
091:
092: /**
093: * This implementation returns false.
094: */
095: public boolean isMutable() {
096: return false;
097: }
098:
099: /**
100: * This implementation delegates to the Hibernate EqualsHelper.
101: * @see org.hibernate.util.EqualsHelper#equals
102: */
103: public boolean equals(Object x, Object 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 x) throws HibernateException {
111: return x.hashCode();
112: }
113:
114: /**
115: * This implementation returns the passed-in value as-is.
116: */
117: public Object deepCopy(Object value) throws HibernateException {
118: return value;
119: }
120:
121: /**
122: * This implementation returns the passed-in value as-is.
123: */
124: public Serializable disassemble(Object value)
125: throws HibernateException {
126: return (Serializable) value;
127: }
128:
129: /**
130: * This implementation returns the passed-in value as-is.
131: */
132: public Object assemble(Serializable cached, Object owner)
133: throws HibernateException {
134: return cached;
135: }
136:
137: /**
138: * This implementation returns the passed-in original as-is.
139: */
140: public Object replace(Object original, Object target, Object owner)
141: throws HibernateException {
142: return original;
143: }
144:
145: /**
146: * This implementation delegates to nullSafeGetInternal,
147: * passing in the LobHandler of this type.
148: * @see #nullSafeGetInternal
149: */
150: public final Object nullSafeGet(ResultSet rs, String[] names,
151: Object owner) throws HibernateException, SQLException {
152:
153: if (this .lobHandler == null) {
154: throw new IllegalStateException(
155: "No LobHandler found for configuration - "
156: + "lobHandler property must be set on LocalSessionFactoryBean");
157: }
158:
159: try {
160: return nullSafeGetInternal(rs, names, owner,
161: this .lobHandler);
162: } catch (IOException ex) {
163: throw new HibernateException(
164: "I/O errors during LOB access", ex);
165: }
166: }
167:
168: /**
169: * This implementation delegates to nullSafeSetInternal,
170: * passing in a transaction-synchronized LobCreator for the
171: * LobHandler of this type.
172: * @see #nullSafeSetInternal
173: */
174: public final void nullSafeSet(PreparedStatement st, Object value,
175: int index) throws HibernateException, SQLException {
176:
177: if (this .lobHandler == null) {
178: throw new IllegalStateException(
179: "No LobHandler found for configuration - "
180: + "lobHandler property must be set on LocalSessionFactoryBean");
181: }
182:
183: LobCreator lobCreator = this .lobHandler.getLobCreator();
184: try {
185: nullSafeSetInternal(st, index, value, lobCreator);
186: } catch (IOException ex) {
187: throw new HibernateException(
188: "I/O errors during LOB access", ex);
189: }
190: LobCreatorUtils.registerTransactionSynchronization(lobCreator,
191: this .jtaTransactionManager);
192: }
193:
194: /**
195: * Template method to extract a value from the given result set.
196: * @param rs the ResultSet to extract from
197: * @param names the column names
198: * @param owner the containing entity
199: * @param lobHandler the LobHandler to use
200: * @return the extracted value
201: * @throws SQLException if thrown by JDBC methods
202: * @throws IOException if thrown by streaming methods
203: * @throws HibernateException in case of any other exceptions
204: */
205: protected abstract Object nullSafeGetInternal(ResultSet rs,
206: String[] names, Object owner, LobHandler lobHandler)
207: throws SQLException, IOException, HibernateException;
208:
209: /**
210: * Template method to set the given parameter value on the given statement.
211: * @param ps the PreparedStatement to set on
212: * @param index the statement parameter index
213: * @param value the value to set
214: * @param lobCreator the LobCreator to use
215: * @throws SQLException if thrown by JDBC methods
216: * @throws IOException if thrown by streaming methods
217: * @throws HibernateException in case of any other exceptions
218: */
219: protected abstract void nullSafeSetInternal(PreparedStatement ps,
220: int index, Object value, LobCreator lobCreator)
221: throws SQLException, IOException, HibernateException;
222:
223: }
|