KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > TopLinkAccessor


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;
18
19 import java.sql.SQLException JavaDoc;
20
21 import oracle.toplink.exceptions.DatabaseException;
22 import oracle.toplink.exceptions.TopLinkException;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.dao.DataAccessException;
28 import org.springframework.jdbc.support.SQLExceptionTranslator;
29
30 /**
31  * Base class for TopLinkTemplate and TopLinkInterceptor, defining common properties
32  * such as SessionFactory and JDBC exception translator.
33  *
34  * <p>Not intended to be used directly. See TopLinkTemplate and TopLinkInterceptor.
35  *
36  * <p>Thanks to Slavik Markovich for implementing the initial TopLink support prototype!
37  *
38  * @author Juergen Hoeller
39  * @since 1.2
40  * @see TopLinkTemplate
41  * @see TopLinkInterceptor
42  */

43 public abstract class TopLinkAccessor implements InitializingBean {
44
45     /** Logger available to subclasses */
46     protected final Log logger = LogFactory.getLog(getClass());
47
48     private SessionFactory sessionFactory;
49
50     private SQLExceptionTranslator jdbcExceptionTranslator;
51
52
53     /**
54      * Set the the TopLink SessionFactory that should be used to create TopLink
55      * Sessions. This will usually be a ServerSessionFactory in a multi-threaded
56      * environment, but can also be a SingleSessionFactory for testing purposes
57      * or for standalone execution.
58      * <p>The passed-in SessionFactory will usually be asked for a plain Session
59      * to perform data access on, unless an active transaction with a thread-bound
60      * Session is found.
61      * @see ServerSessionFactory
62      * @see SingleSessionFactory
63      * @see SessionFactory#createSession()
64      * @see SessionFactoryUtils#getSession(SessionFactory, boolean)
65      */

66     public void setSessionFactory(SessionFactory sessionFactory) {
67         this.sessionFactory = sessionFactory;
68     }
69
70     /**
71      * Return the TopLink SessionFactory that should be used to create
72      * TopLink Sessions.
73      */

74     public SessionFactory getSessionFactory() {
75         return sessionFactory;
76     }
77
78     /**
79      * Set the JDBC exception translator for this instance.
80      * <p>Applied to any SQLException root cause of a TopLink DatabaseException.
81      * The default is to rely on TopLink's native exception translation.
82      * @param jdbcExceptionTranslator the exception translator
83      * @see oracle.toplink.exceptions.DatabaseException
84      * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
85      * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
86      */

87     public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
88         this.jdbcExceptionTranslator = jdbcExceptionTranslator;
89     }
90
91     /**
92      * Return the JDBC exception translator for this instance, if any.
93      */

94     public SQLExceptionTranslator getJdbcExceptionTranslator() {
95         return this.jdbcExceptionTranslator;
96     }
97
98
99     /**
100      * Check that we were provided with a session to use
101      */

102     public void afterPropertiesSet() {
103         if (this.sessionFactory == null) {
104             throw new IllegalArgumentException JavaDoc("sessionFactory is required");
105         }
106     }
107
108
109     /**
110      * Convert the given TopLinkException to an appropriate exception from the
111      * <code>org.springframework.dao</code> hierarchy.
112      * <p>Will automatically apply a specified SQLExceptionTranslator to a
113      * TopLink DatabaseException, else rely on TopLink's default translation.
114      * @param ex TopLinkException that occured
115      * @return a corresponding DataAccessException
116      * @see SessionFactoryUtils#convertTopLinkAccessException
117      * @see #setJdbcExceptionTranslator
118      */

119     public DataAccessException convertTopLinkAccessException(TopLinkException ex) {
120         if (getJdbcExceptionTranslator() != null && ex instanceof DatabaseException) {
121             Throwable JavaDoc internalEx = ex.getInternalException();
122             // Should always be a SQLException inside a DatabaseException.
123
if (internalEx instanceof SQLException JavaDoc) {
124                 return getJdbcExceptionTranslator().translate(
125                         "TopLink operation: " + ex.getMessage(), null, (SQLException JavaDoc) internalEx);
126             }
127         }
128         return SessionFactoryUtils.convertTopLinkAccessException(ex);
129     }
130
131 }
132
Popular Tags