001: /*
002: * Copyright 2002-2005 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.ibatis.support;
018:
019: import javax.sql.DataSource;
020:
021: import com.ibatis.sqlmap.client.SqlMapClient;
022:
023: import org.springframework.dao.support.DaoSupport;
024: import org.springframework.orm.ibatis.SqlMapClientTemplate;
025:
026: /**
027: * Convenient super class for iBATIS SqlMapClient data access objects.
028: * Requires a SqlMapClient to be set, providing a SqlMapClientTemplate
029: * based on it to subclasses.
030: *
031: * <p>Instead of a plain SqlMapClient, you can also pass a preconfigured
032: * SqlMapClientTemplate instance in. This allows you to share your
033: * SqlMapClientTemplate configuration for all your DAOs, for example
034: * a custom SQLExceptionTranslator to use.
035: *
036: * @author Juergen Hoeller
037: * @since 24.02.2004
038: * @see #setSqlMapClient
039: * @see #setSqlMapClientTemplate
040: * @see org.springframework.orm.ibatis.SqlMapClientTemplate
041: * @see org.springframework.orm.ibatis.SqlMapClientTemplate#setExceptionTranslator
042: */
043: public abstract class SqlMapClientDaoSupport extends DaoSupport {
044:
045: private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
046:
047: private boolean externalTemplate = false;
048:
049: /**
050: * Set the JDBC DataSource to be used by this DAO.
051: * Not required: The SqlMapClient might carry a shared DataSource.
052: * @see #setSqlMapClient
053: */
054: public final void setDataSource(DataSource dataSource) {
055: this .sqlMapClientTemplate.setDataSource(dataSource);
056: }
057:
058: /**
059: * Return the JDBC DataSource used by this DAO.
060: */
061: public final DataSource getDataSource() {
062: return (this .sqlMapClientTemplate != null ? this .sqlMapClientTemplate
063: .getDataSource()
064: : null);
065: }
066:
067: /**
068: * Set the iBATIS Database Layer SqlMapClient to work with.
069: * Either this or a "sqlMapClientTemplate" is required.
070: * @see #setSqlMapClientTemplate
071: */
072: public final void setSqlMapClient(SqlMapClient sqlMapClient) {
073: this .sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
074: }
075:
076: /**
077: * Return the iBATIS Database Layer SqlMapClient that this template works with.
078: */
079: public final SqlMapClient getSqlMapClient() {
080: return this .sqlMapClientTemplate.getSqlMapClient();
081: }
082:
083: /**
084: * Set the SqlMapClientTemplate for this DAO explicitly,
085: * as an alternative to specifying a SqlMapClient.
086: * @see #setSqlMapClient
087: */
088: public final void setSqlMapClientTemplate(
089: SqlMapClientTemplate sqlMapClientTemplate) {
090: if (sqlMapClientTemplate == null) {
091: throw new IllegalArgumentException(
092: "Cannot set sqlMapClientTemplate to null");
093: }
094: this .sqlMapClientTemplate = sqlMapClientTemplate;
095: this .externalTemplate = true;
096: }
097:
098: /**
099: * Return the SqlMapClientTemplate for this DAO,
100: * pre-initialized with the SqlMapClient or set explicitly.
101: */
102: public final SqlMapClientTemplate getSqlMapClientTemplate() {
103: return sqlMapClientTemplate;
104: }
105:
106: protected final void checkDaoConfig() {
107: if (!this.externalTemplate) {
108: this.sqlMapClientTemplate.afterPropertiesSet();
109: }
110: }
111:
112: }
|