KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > 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.hibernate.support;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import javax.transaction.Status JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import net.sf.hibernate.HibernateException;
28 import net.sf.hibernate.UserType;
29 import net.sf.hibernate.util.EqualsHelper;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import org.springframework.dao.DataAccessResourceFailureException;
34 import org.springframework.jdbc.support.lob.JtaLobCreatorSynchronization;
35 import org.springframework.jdbc.support.lob.LobCreator;
36 import org.springframework.jdbc.support.lob.LobHandler;
37 import org.springframework.jdbc.support.lob.SpringLobCreatorSynchronization;
38 import org.springframework.jdbc.support.lob.LobCreatorUtils;
39 import org.springframework.orm.hibernate.LocalSessionFactoryBean;
40 import org.springframework.orm.hibernate.SessionFactoryUtils;
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) is required.
50  *
51  * <p>Offers template methods for setting parameters and getting result values,
52  * passing in the LobHandler or LobCreator to use.
53  *
54  * @author Juergen Hoeller
55  * @since 1.1
56  * @see org.springframework.jdbc.support.lob.LobHandler
57  * @see org.springframework.jdbc.support.lob.LobCreator
58  * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setLobHandler
59  * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setJtaTransactionManager
60  */

61 public abstract class AbstractLobType implements UserType {
62
63     /**
64      * Order value for TransactionSynchronization objects that clean up LobCreators.
65      * Return SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER - 100 to execute
66      * LobCreator cleanup before Hibernate Session and JDBC Connection cleanup, if any.
67      * @see org.springframework.orm.hibernate.SessionFactoryUtils#SESSION_SYNCHRONIZATION_ORDER
68      */

69     public static final int LOB_CREATOR_SYNCHRONIZATION_ORDER =
70             SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER - 100;
71
72     protected final Log logger = LogFactory.getLog(getClass());
73
74     private final LobHandler lobHandler;
75
76     private final TransactionManager JavaDoc jtaTransactionManager;
77
78
79     /**
80      * Constructor used by Hibernate: fetches config-time LobHandler and
81      * config-time JTA TransactionManager from LocalSessionFactoryBean.
82      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeLobHandler
83      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeTransactionManager
84      */

85     protected AbstractLobType() {
86         this(LocalSessionFactoryBean.getConfigTimeLobHandler(),
87             LocalSessionFactoryBean.getConfigTimeTransactionManager());
88     }
89
90     /**
91      * Constructor used for testing: takes an explicit LobHandler
92      * and an explicit JTA TransactionManager (can be <code>null</code>).
93      */

94     protected AbstractLobType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
95         this.lobHandler = lobHandler;
96         this.jtaTransactionManager = jtaTransactionManager;
97     }
98
99
100     /**
101      * This implementation returns false.
102      */

103     public boolean isMutable() {
104         return false;
105     }
106
107     /**
108      * This implementation delegates to the Hibernate EqualsHelper.
109      * @see net.sf.hibernate.util.EqualsHelper#equals
110      */

111     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
112         return EqualsHelper.equals(x, y);
113     }
114
115     /**
116      * This implementation returns the passed-in value as-is.
117      */

118     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
119         return value;
120     }
121
122
123     /**
124      * This implementation delegates to nullSafeGetInternal,
125      * passing in the LobHandler of this type.
126      * @see #nullSafeGetInternal
127      */

128     public final Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner)
129             throws HibernateException, SQLException JavaDoc {
130
131         if (this.lobHandler == null) {
132             throw new IllegalStateException JavaDoc("No LobHandler found for configuration - " +
133                 "lobHandler property must be set on LocalSessionFactoryBean");
134         }
135
136         try {
137             return nullSafeGetInternal(rs, rs.findColumn(names[0]), this.lobHandler);
138         }
139         catch (IOException JavaDoc ex) {
140             throw new HibernateException("I/O errors during LOB access", ex);
141         }
142     }
143
144     /**
145      * This implementation delegates to nullSafeSetInternal,
146      * passing in a transaction-synchronized LobCreator for the
147      * LobHandler of this type.
148      * @see #nullSafeSetInternal
149      */

150     public final void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
151             throws HibernateException, SQLException JavaDoc {
152
153         if (this.lobHandler == null) {
154             throw new IllegalStateException JavaDoc("No LobHandler found for configuration - " +
155                 "lobHandler property must be set on LocalSessionFactoryBean");
156         }
157
158         LobCreator lobCreator = this.lobHandler.getLobCreator();
159         try {
160             nullSafeSetInternal(st, index, value, lobCreator);
161         }
162         catch (IOException JavaDoc ex) {
163             throw new HibernateException("I/O errors during LOB access", ex);
164         }
165         LobCreatorUtils.registerTransactionSynchronization(lobCreator, this.jtaTransactionManager);
166     }
167
168     /**
169      * Template method to extract a value from the given result set.
170      * @param rs the ResultSet to extract from
171      * @param index the index in the ResultSet
172      * @param lobHandler the LobHandler to use
173      * @return the extracted value
174      * @throws SQLException if thrown by JDBC methods
175      * @throws IOException if thrown by streaming methods
176      * @throws HibernateException in case of any other exceptions
177      */

178     protected abstract Object JavaDoc nullSafeGetInternal(ResultSet JavaDoc rs, int index, LobHandler lobHandler)
179             throws SQLException JavaDoc, IOException JavaDoc, HibernateException;
180
181     /**
182      * Template method to set the given parameter value on the given statement.
183      * @param ps the PreparedStatement to set on
184      * @param index the statement parameter index
185      * @param value the value to set
186      * @param lobCreator the LobCreator to use
187      * @throws SQLException if thrown by JDBC methods
188      * @throws IOException if thrown by streaming methods
189      * @throws HibernateException in case of any other exceptions
190      */

191     protected abstract void nullSafeSetInternal(
192         PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
193             throws SQLException JavaDoc, IOException JavaDoc, HibernateException;
194
195 }
196
Popular Tags