KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > support > SqlMapClientDaoSupport


1 /*
2  * Copyright 2002-2005 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.ibatis.support;
18
19 import javax.sql.DataSource JavaDoc;
20
21 import com.ibatis.sqlmap.client.SqlMapClient;
22
23 import org.springframework.dao.support.DaoSupport;
24 import org.springframework.orm.ibatis.SqlMapClientTemplate;
25
26 /**
27  * Convenient super class for iBATIS SqlMapClient data access objects.
28  * Requires a SqlMapClient to be set, providing a SqlMapClientTemplate
29  * based on it to subclasses.
30  *
31  * <p>Instead of a plain SqlMapClient, you can also pass a preconfigured
32  * SqlMapClientTemplate instance in. This allows you to share your
33  * SqlMapClientTemplate configuration for all your DAOs, for example
34  * a custom SQLExceptionTranslator to use.
35  *
36  * @author Juergen Hoeller
37  * @since 24.02.2004
38  * @see #setSqlMapClient
39  * @see #setSqlMapClientTemplate
40  * @see org.springframework.orm.ibatis.SqlMapClientTemplate
41  * @see org.springframework.orm.ibatis.SqlMapClientTemplate#setExceptionTranslator
42  */

43 public abstract class SqlMapClientDaoSupport extends DaoSupport {
44
45     private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
46
47     private boolean externalTemplate = false;
48
49     /**
50      * Set the JDBC DataSource to be used by this DAO.
51      * Not required: The SqlMapClient might carry a shared DataSource.
52      * @see #setSqlMapClient
53      */

54     public final void setDataSource(DataSource JavaDoc dataSource) {
55       this.sqlMapClientTemplate.setDataSource(dataSource);
56     }
57
58     /**
59      * Return the JDBC DataSource used by this DAO.
60      */

61     public final DataSource JavaDoc getDataSource() {
62         return (this.sqlMapClientTemplate != null ? this.sqlMapClientTemplate.getDataSource() : null);
63     }
64
65     /**
66      * Set the iBATIS Database Layer SqlMapClient to work with.
67      * Either this or a "sqlMapClientTemplate" is required.
68      * @see #setSqlMapClientTemplate
69      */

70     public final void setSqlMapClient(SqlMapClient sqlMapClient) {
71         this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
72     }
73
74     /**
75      * Return the iBATIS Database Layer SqlMapClient that this template works with.
76      */

77     public final SqlMapClient getSqlMapClient() {
78         return this.sqlMapClientTemplate.getSqlMapClient();
79     }
80
81     /**
82      * Set the SqlMapClientTemplate for this DAO explicitly,
83      * as an alternative to specifying a SqlMapClient.
84      * @see #setSqlMapClient
85      */

86     public final void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
87         if (sqlMapClientTemplate == null) {
88             throw new IllegalArgumentException JavaDoc("Cannot set sqlMapClientTemplate to null");
89         }
90         this.sqlMapClientTemplate = sqlMapClientTemplate;
91         this.externalTemplate = true;
92     }
93
94     /**
95      * Return the SqlMapClientTemplate for this DAO,
96      * pre-initialized with the SqlMapClient or set explicitly.
97      */

98     public final SqlMapClientTemplate getSqlMapClientTemplate() {
99       return sqlMapClientTemplate;
100     }
101
102     protected final void checkDaoConfig() {
103         if (!this.externalTemplate) {
104             this.sqlMapClientTemplate.afterPropertiesSet();
105         }
106     }
107
108 }
109
Popular Tags