KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > support > TopLinkDaoSupport


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.toplink.support;
18
19 import oracle.toplink.exceptions.TopLinkException;
20 import oracle.toplink.sessions.Session;
21
22 import org.springframework.dao.DataAccessException;
23 import org.springframework.dao.DataAccessResourceFailureException;
24 import org.springframework.dao.support.DaoSupport;
25 import org.springframework.orm.toplink.SessionFactory;
26 import org.springframework.orm.toplink.SessionFactoryUtils;
27 import org.springframework.orm.toplink.TopLinkTemplate;
28
29 /**
30  * Convenient super class for TopLink data access objects.
31  *
32  * <p>Requires a SessionFactory to be set, providing a TopLinkTemplate
33  * based on it to subclasses. Can alternatively be initialized directly with
34  * a TopLinkTemplate, to reuse the latter's settings such as the SessionFactory,
35  * exception translator, etc.
36  *
37  * <p>This base class is mainly intended for TopLinkTemplate usage
38  * but can also be used when working with SessionFactoryUtils directly,
39  * for example in combination with TopLinkInterceptor-managed Sessions.
40  * Convenience <code>getSession</code> and <code>releaseSession</code>
41  * methods are provided for that usage style.
42  *
43  * @author Juergen Hoeller
44  * @since 1.2
45  * @see #setSessionFactory
46  * @see #setTopLinkTemplate
47  * @see #getSession
48  * @see #releaseSession
49  * @see org.springframework.orm.toplink.TopLinkTemplate
50  * @see org.springframework.orm.toplink.TopLinkInterceptor
51  */

52 public abstract class TopLinkDaoSupport extends DaoSupport {
53
54     private TopLinkTemplate topLinkTemplate;
55
56
57     /**
58      * Set the TopLink SessionFactory to be used by this DAO.
59      * Will automatically create a TopLinkTemplate for the given SessionFactory.
60      * @see #createTopLinkTemplate
61      * @see #setTopLinkTemplate
62      */

63     public final void setSessionFactory(SessionFactory sessionFactory) {
64       this.topLinkTemplate = createTopLinkTemplate(sessionFactory);
65     }
66
67     /**
68      * Create a TopLinkTemplate for the given SessionFactory.
69      * Only invoked if populating the DAO with a SessionFactory reference!
70      * <p>Can be overridden in subclasses to provide a TopLinkTemplate instance
71      * with different configuration, or a custom TopLinkTemplate subclass.
72      * @param sessionFactory the TopLink SessionFactory to create a TopLinkTemplate for
73      * @return the new TopLinkTemplate instance
74      * @see #setSessionFactory
75      */

76     protected TopLinkTemplate createTopLinkTemplate(SessionFactory sessionFactory) {
77         return new TopLinkTemplate(sessionFactory);
78     }
79
80     /**
81      * Return the TopLink SessionFactory used by this DAO.
82      */

83     public final SessionFactory getSessionFactory() {
84         return (this.topLinkTemplate != null ? this.topLinkTemplate.getSessionFactory() : null);
85     }
86
87     /**
88      * Set the TopLinkTemplate for this DAO explicitly,
89      * as an alternative to specifying a SessionFactory.
90      * @see #setSessionFactory
91      */

92     public final void setTopLinkTemplate(TopLinkTemplate topLinkTemplate) {
93         this.topLinkTemplate = topLinkTemplate;
94     }
95
96     /**
97      * Return the TopLinkTemplate for this DAO,
98      * pre-initialized with the SessionFactory or set explicitly.
99      */

100     public final TopLinkTemplate getTopLinkTemplate() {
101         return topLinkTemplate;
102     }
103
104     protected final void checkDaoConfig() {
105         if (this.topLinkTemplate == null) {
106             throw new IllegalArgumentException JavaDoc("sessionFactory or topLinkTemplate is required");
107         }
108     }
109
110
111     /**
112      * Get a TopLink Session, either from the current transaction or a new one.
113      * The latter is only allowed if the "allowCreate" setting of this bean's
114      * TopLinkTemplate is true.
115      * <p><b>Note that this is not meant to be invoked from TopLinkTemplate code
116      * but rather just in plain TopLink code.</b> Either rely on a thread-bound
117      * Session (via TopLinkInterceptor), or use it in combination with
118      * <code>releaseSession</code>.
119      * <p>In general, it is recommended to use TopLinkTemplate, either with
120      * the provided convenience operations or with a custom TopLinkCallback
121      * that provides you with a Session to work on. TopLinkTemplate will care
122      * for all resource management and for proper exception conversion.
123      * @return the TopLink Session
124      * @throws DataAccessResourceFailureException if the Session couldn't be created
125      * @throws IllegalStateException if no thread-bound Session found and allowCreate false
126      * @see TopLinkTemplate
127      * @see org.springframework.orm.toplink.SessionFactoryUtils#getSession(SessionFactory, boolean)
128      * @see org.springframework.orm.toplink.TopLinkInterceptor
129      * @see org.springframework.orm.toplink.TopLinkTemplate
130      * @see org.springframework.orm.toplink.TopLinkCallback
131      */

132     protected final Session getSession()
133             throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
134
135         return getSession(this.topLinkTemplate.isAllowCreate());
136     }
137
138     /**
139      * Get a TopLink Session, either from the current transaction or a new one.
140      * The latter is only allowed if "allowCreate" is true.
141      * <p><b>Note that this is not meant to be invoked from TopLinkTemplate code
142      * but rather just in plain TopLink code.</b> Either rely on a thread-bound
143      * Session (via TopLinkInterceptor), or use it in combination with
144      * <code>releaseSession</code>.
145      * <p>In general, it is recommended to use TopLinkTemplate, either with
146      * the provided convenience operations or with a custom TopLinkCallback
147      * that provides you with a Session to work on. TopLinkTemplate will care
148      * for all resource management and for proper exception conversion.
149      * @param allowCreate if a new Session should be created if no thread-bound found
150      * @return the TopLink Session
151      * @throws DataAccessResourceFailureException if the Session couldn't be created
152      * @throws IllegalStateException if no thread-bound Session found and allowCreate false
153      * @see org.springframework.orm.toplink.SessionFactoryUtils#getSession(SessionFactory, boolean)
154      * @see org.springframework.orm.toplink.TopLinkInterceptor
155      * @see org.springframework.orm.toplink.TopLinkTemplate
156      * @see org.springframework.orm.toplink.TopLinkCallback
157      */

158     protected final Session getSession(boolean allowCreate)
159             throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
160
161         return SessionFactoryUtils.getSession(this.getSessionFactory(), allowCreate);
162     }
163
164     /**
165      * Convert the given TopLinkException to an appropriate exception from the
166      * <code>org.springframework.dao</code> hierarchy. Will automatically detect
167      * wrapped SQLExceptions and convert them accordingly.
168      * <p>Delegates to the convertTopLinkAccessException method of this
169      * DAO's TopLinkTemplate.
170      * @param ex TopLinkException that occured
171      * @return the corresponding DataAccessException instance
172      * @see #setTopLinkTemplate
173      * @see org.springframework.orm.toplink.TopLinkTemplate#convertTopLinkAccessException
174      */

175     protected final DataAccessException convertTopLinkAccessException(TopLinkException ex) {
176         return this.topLinkTemplate.convertTopLinkAccessException(ex);
177     }
178
179     /**
180      * Close the given TopLink Session, created via this DAO's SessionFactory,
181      * if it isn't bound to the thread.
182      * @param session the TopLink Session to close
183      * @see org.springframework.orm.toplink.SessionFactoryUtils#releaseSession
184      */

185     protected final void releaseSession(Session session) {
186         SessionFactoryUtils.releaseSession(session, getSessionFactory());
187     }
188
189 }
190
Popular Tags