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;
018:
019: import java.sql.SQLException;
020:
021: import oracle.toplink.exceptions.DatabaseException;
022: import oracle.toplink.exceptions.TopLinkException;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import org.springframework.beans.factory.InitializingBean;
027: import org.springframework.dao.DataAccessException;
028: import org.springframework.jdbc.support.SQLExceptionTranslator;
029:
030: /**
031: * Base class for TopLinkTemplate and TopLinkInterceptor, defining common properties
032: * such as SessionFactory and JDBC exception translator.
033: *
034: * <p>Not intended to be used directly. See TopLinkTemplate and TopLinkInterceptor.
035: *
036: * <p>Thanks to Slavik Markovich for implementing the initial TopLink support prototype!
037: *
038: * @author Juergen Hoeller
039: * @since 1.2
040: * @see TopLinkTemplate
041: * @see TopLinkInterceptor
042: */
043: public abstract class TopLinkAccessor implements InitializingBean {
044:
045: /** Logger available to subclasses */
046: protected final Log logger = LogFactory.getLog(getClass());
047:
048: private SessionFactory sessionFactory;
049:
050: private SQLExceptionTranslator jdbcExceptionTranslator;
051:
052: /**
053: * Set the the TopLink SessionFactory that should be used to create TopLink
054: * Sessions. This will usually be a ServerSessionFactory in a multi-threaded
055: * environment, but can also be a SingleSessionFactory for testing purposes
056: * or for standalone execution.
057: * <p>The passed-in SessionFactory will usually be asked for a plain Session
058: * to perform data access on, unless an active transaction with a thread-bound
059: * Session is found.
060: * @see ServerSessionFactory
061: * @see SingleSessionFactory
062: * @see SessionFactory#createSession()
063: * @see SessionFactoryUtils#getSession(SessionFactory, boolean)
064: */
065: public void setSessionFactory(SessionFactory sessionFactory) {
066: this .sessionFactory = sessionFactory;
067: }
068:
069: /**
070: * Return the TopLink SessionFactory that should be used to create
071: * TopLink Sessions.
072: */
073: public SessionFactory getSessionFactory() {
074: return sessionFactory;
075: }
076:
077: /**
078: * Set the JDBC exception translator for this instance.
079: * <p>Applied to any SQLException root cause of a TopLink DatabaseException.
080: * The default is to rely on TopLink's native exception translation.
081: * @param jdbcExceptionTranslator the exception translator
082: * @see oracle.toplink.exceptions.DatabaseException
083: * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
084: * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
085: */
086: public void setJdbcExceptionTranslator(
087: SQLExceptionTranslator jdbcExceptionTranslator) {
088: this .jdbcExceptionTranslator = jdbcExceptionTranslator;
089: }
090:
091: /**
092: * Return the JDBC exception translator for this instance, if any.
093: */
094: public SQLExceptionTranslator getJdbcExceptionTranslator() {
095: return this .jdbcExceptionTranslator;
096: }
097:
098: /**
099: * Check that we were provided with a session to use
100: */
101: public void afterPropertiesSet() {
102: if (this .sessionFactory == null) {
103: throw new IllegalArgumentException(
104: "sessionFactory is required");
105: }
106: }
107:
108: /**
109: * Convert the given TopLinkException to an appropriate exception from the
110: * <code>org.springframework.dao</code> hierarchy.
111: * <p>Will automatically apply a specified SQLExceptionTranslator to a
112: * TopLink DatabaseException, else rely on TopLink's default translation.
113: * @param ex TopLinkException that occured
114: * @return a corresponding DataAccessException
115: * @see SessionFactoryUtils#convertTopLinkAccessException
116: * @see #setJdbcExceptionTranslator
117: */
118: public DataAccessException convertTopLinkAccessException(
119: TopLinkException ex) {
120: if (getJdbcExceptionTranslator() != null
121: && ex instanceof DatabaseException) {
122: Throwable internalEx = ex.getInternalException();
123: // Should always be a SQLException inside a DatabaseException.
124: if (internalEx instanceof SQLException) {
125: return getJdbcExceptionTranslator().translate(
126: "TopLink operation: " + ex.getMessage(), null,
127: (SQLException) internalEx);
128: }
129: }
130: return SessionFactoryUtils.convertTopLinkAccessException(ex);
131: }
132:
133: }
|