KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > SqlMapFactoryBean


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;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import com.ibatis.db.sqlmap.SqlMap;
25 import com.ibatis.db.sqlmap.XmlSqlMapBuilder;
26
27 import org.springframework.beans.factory.FactoryBean;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.io.Resource;
30
31 /**
32  * FactoryBean that creates an iBATIS Database Layer SqlMap as singleton in the
33  * current bean factory, possibly for use with SqlMapTemplate.
34  *
35  * <p>NOTE: The SqlMap/MappedStatement API is the one to use with iBATIS SQL Maps 1.x.
36  * The SqlMapClient/SqlMapSession API is only available with SQL Maps 2.
37  *
38  * @author Juergen Hoeller
39  * @since 28.11.2003
40  * @see SqlMapTemplate#setSqlMap
41  */

42 public class SqlMapFactoryBean implements FactoryBean, InitializingBean {
43
44     private Resource configLocation;
45
46     private Properties JavaDoc sqlMapProperties;
47
48     private SqlMap sqlMap;
49
50
51     /**
52      * Set the location of the iBATIS SqlMap config file.
53      * A typical value is "WEB-INF/sql-map-config.xml".
54      */

55     public void setConfigLocation(Resource configLocation) {
56         this.configLocation = configLocation;
57     }
58
59     /**
60      * Set optional properties to be passed into the XmlSqlMapBuilder.
61      * @see com.ibatis.db.sqlmap.XmlSqlMapBuilder#buildSqlMap(java.io.Reader, java.util.Properties)
62      */

63     public void setSqlMapProperties(Properties JavaDoc sqlMapProperties) {
64         this.sqlMapProperties = sqlMapProperties;
65     }
66
67
68     public void afterPropertiesSet() throws IOException JavaDoc {
69         if (this.configLocation == null) {
70             throw new IllegalArgumentException JavaDoc("configLocation is required");
71         }
72
73         // build the SqlMap
74
InputStream JavaDoc is = this.configLocation.getInputStream();
75         this.sqlMap = (this.sqlMapProperties != null) ?
76                 XmlSqlMapBuilder.buildSqlMap(new InputStreamReader JavaDoc(is), this.sqlMapProperties) :
77                 XmlSqlMapBuilder.buildSqlMap(new InputStreamReader JavaDoc(is));
78     }
79
80
81     public Object JavaDoc getObject() {
82         return this.sqlMap;
83     }
84
85     public Class JavaDoc getObjectType() {
86         return (this.sqlMap != null ? this.sqlMap.getClass() : SqlMap.class);
87     }
88
89     public boolean isSingleton() {
90         return true;
91     }
92
93 }
94
Popular Tags