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.toplink.support;
018:
019: import oracle.toplink.exceptions.TopLinkException;
020: import oracle.toplink.sessions.Session;
021:
022: import org.springframework.dao.DataAccessException;
023: import org.springframework.dao.DataAccessResourceFailureException;
024: import org.springframework.dao.support.DaoSupport;
025: import org.springframework.orm.toplink.SessionFactory;
026: import org.springframework.orm.toplink.SessionFactoryUtils;
027: import org.springframework.orm.toplink.TopLinkTemplate;
028:
029: /**
030: * Convenient super class for TopLink data access objects.
031: *
032: * <p>Requires a SessionFactory to be set, providing a TopLinkTemplate
033: * based on it to subclasses. Can alternatively be initialized directly with
034: * a TopLinkTemplate, to reuse the latter's settings such as the SessionFactory,
035: * exception translator, etc.
036: *
037: * <p>This base class is mainly intended for TopLinkTemplate usage
038: * but can also be used when working with SessionFactoryUtils directly,
039: * for example in combination with TopLinkInterceptor-managed Sessions.
040: * Convenience <code>getSession</code> and <code>releaseSession</code>
041: * methods are provided for that usage style.
042: *
043: * @author Juergen Hoeller
044: * @since 1.2
045: * @see #setSessionFactory
046: * @see #setTopLinkTemplate
047: * @see #getSession
048: * @see #releaseSession
049: * @see org.springframework.orm.toplink.TopLinkTemplate
050: * @see org.springframework.orm.toplink.TopLinkInterceptor
051: */
052: public abstract class TopLinkDaoSupport extends DaoSupport {
053:
054: private TopLinkTemplate topLinkTemplate;
055:
056: /**
057: * Set the TopLink SessionFactory to be used by this DAO.
058: * Will automatically create a TopLinkTemplate for the given SessionFactory.
059: * @see #createTopLinkTemplate
060: * @see #setTopLinkTemplate
061: */
062: public final void setSessionFactory(SessionFactory sessionFactory) {
063: this .topLinkTemplate = createTopLinkTemplate(sessionFactory);
064: }
065:
066: /**
067: * Create a TopLinkTemplate for the given SessionFactory.
068: * Only invoked if populating the DAO with a SessionFactory reference!
069: * <p>Can be overridden in subclasses to provide a TopLinkTemplate instance
070: * with different configuration, or a custom TopLinkTemplate subclass.
071: * @param sessionFactory the TopLink SessionFactory to create a TopLinkTemplate for
072: * @return the new TopLinkTemplate instance
073: * @see #setSessionFactory
074: */
075: protected TopLinkTemplate createTopLinkTemplate(
076: SessionFactory sessionFactory) {
077: return new TopLinkTemplate(sessionFactory);
078: }
079:
080: /**
081: * Return the TopLink SessionFactory used by this DAO.
082: */
083: public final SessionFactory getSessionFactory() {
084: return (this .topLinkTemplate != null ? this .topLinkTemplate
085: .getSessionFactory() : null);
086: }
087:
088: /**
089: * Set the TopLinkTemplate for this DAO explicitly,
090: * as an alternative to specifying a SessionFactory.
091: * @see #setSessionFactory
092: */
093: public final void setTopLinkTemplate(TopLinkTemplate topLinkTemplate) {
094: this .topLinkTemplate = topLinkTemplate;
095: }
096:
097: /**
098: * Return the TopLinkTemplate for this DAO,
099: * pre-initialized with the SessionFactory or set explicitly.
100: */
101: public final TopLinkTemplate getTopLinkTemplate() {
102: return topLinkTemplate;
103: }
104:
105: protected final void checkDaoConfig() {
106: if (this .topLinkTemplate == null) {
107: throw new IllegalArgumentException(
108: "sessionFactory or topLinkTemplate is required");
109: }
110: }
111:
112: /**
113: * Get a TopLink Session, either from the current transaction or a new one.
114: * The latter is only allowed if the "allowCreate" setting of this bean's
115: * TopLinkTemplate is true.
116: * <p><b>Note that this is not meant to be invoked from TopLinkTemplate code
117: * but rather just in plain TopLink code.</b> Either rely on a thread-bound
118: * Session (via TopLinkInterceptor), or use it in combination with
119: * <code>releaseSession</code>.
120: * <p>In general, it is recommended to use TopLinkTemplate, either with
121: * the provided convenience operations or with a custom TopLinkCallback
122: * that provides you with a Session to work on. TopLinkTemplate will care
123: * for all resource management and for proper exception conversion.
124: * @return the TopLink Session
125: * @throws DataAccessResourceFailureException if the Session couldn't be created
126: * @throws IllegalStateException if no thread-bound Session found and allowCreate false
127: * @see TopLinkTemplate
128: * @see org.springframework.orm.toplink.SessionFactoryUtils#getSession(SessionFactory, boolean)
129: * @see org.springframework.orm.toplink.TopLinkInterceptor
130: * @see org.springframework.orm.toplink.TopLinkTemplate
131: * @see org.springframework.orm.toplink.TopLinkCallback
132: */
133: protected final Session getSession()
134: throws DataAccessResourceFailureException,
135: IllegalStateException {
136:
137: return getSession(this .topLinkTemplate.isAllowCreate());
138: }
139:
140: /**
141: * Get a TopLink Session, either from the current transaction or a new one.
142: * The latter is only allowed if "allowCreate" is true.
143: * <p><b>Note that this is not meant to be invoked from TopLinkTemplate code
144: * but rather just in plain TopLink code.</b> Either rely on a thread-bound
145: * Session (via TopLinkInterceptor), or use it in combination with
146: * <code>releaseSession</code>.
147: * <p>In general, it is recommended to use TopLinkTemplate, either with
148: * the provided convenience operations or with a custom TopLinkCallback
149: * that provides you with a Session to work on. TopLinkTemplate will care
150: * for all resource management and for proper exception conversion.
151: * @param allowCreate if a new Session should be created if no thread-bound found
152: * @return the TopLink Session
153: * @throws DataAccessResourceFailureException if the Session couldn't be created
154: * @throws IllegalStateException if no thread-bound Session found and allowCreate false
155: * @see org.springframework.orm.toplink.SessionFactoryUtils#getSession(SessionFactory, boolean)
156: * @see org.springframework.orm.toplink.TopLinkInterceptor
157: * @see org.springframework.orm.toplink.TopLinkTemplate
158: * @see org.springframework.orm.toplink.TopLinkCallback
159: */
160: protected final Session getSession(boolean allowCreate)
161: throws DataAccessResourceFailureException,
162: IllegalStateException {
163:
164: return SessionFactoryUtils.getSession(this .getSessionFactory(),
165: allowCreate);
166: }
167:
168: /**
169: * Convert the given TopLinkException to an appropriate exception from the
170: * <code>org.springframework.dao</code> hierarchy. Will automatically detect
171: * wrapped SQLExceptions and convert them accordingly.
172: * <p>Delegates to the convertTopLinkAccessException method of this
173: * DAO's TopLinkTemplate.
174: * @param ex TopLinkException that occured
175: * @return the corresponding DataAccessException instance
176: * @see #setTopLinkTemplate
177: * @see org.springframework.orm.toplink.TopLinkTemplate#convertTopLinkAccessException
178: */
179: protected final DataAccessException convertTopLinkAccessException(
180: TopLinkException ex) {
181: return this .topLinkTemplate.convertTopLinkAccessException(ex);
182: }
183:
184: /**
185: * Close the given TopLink Session, created via this DAO's SessionFactory,
186: * if it isn't bound to the thread.
187: * @param session the TopLink Session to close
188: * @see org.springframework.orm.toplink.SessionFactoryUtils#releaseSession
189: */
190: protected final void releaseSession(Session session) {
191: SessionFactoryUtils
192: .releaseSession(session, getSessionFactory());
193: }
194:
195: }
|