1. /*
  2. * Copyright 2002-2004 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. package org.springframework.orm.ibatis;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.util.Properties;
  21. import com.ibatis.db.sqlmap.SqlMap;
  22. import com.ibatis.db.sqlmap.XmlSqlMapBuilder;
  23. import org.springframework.beans.factory.FactoryBean;
  24. import org.springframework.beans.factory.InitializingBean;
  25. import org.springframework.core.io.Resource;
  26. /**
  27. * FactoryBean that creates an iBATIS Database Layer SqlMap as singleton in the
  28. * current bean factory, possibly for use with SqlMapTemplate.
  29. *
  30. * <p>NOTE: The SqlMap/MappedStatement API is the one to use with iBATIS SQL Maps 1.x.
  31. * The SqlMapClient/SqlMapSession API is only available with SQL Maps 2.
  32. *
  33. * @author Juergen Hoeller
  34. * @since 28.11.2003
  35. * @see SqlMapTemplate#setSqlMap
  36. */
  37. public class SqlMapFactoryBean implements FactoryBean, InitializingBean {
  38. private Resource configLocation;
  39. private Properties sqlMapProperties;
  40. private SqlMap sqlMap;
  41. /**
  42. * Set the location of the iBATIS SqlMap config file.
  43. * A typical value is "WEB-INF/sql-map-config.xml".
  44. */
  45. public void setConfigLocation(Resource configLocation) {
  46. this.configLocation = configLocation;
  47. }
  48. /**
  49. * Set optional properties to be passed into the XmlSqlMapBuilder.
  50. * @see com.ibatis.db.sqlmap.XmlSqlMapBuilder#buildSqlMap(java.io.Reader, java.util.Properties)
  51. */
  52. public void setSqlMapProperties(Properties sqlMapProperties) {
  53. this.sqlMapProperties = sqlMapProperties;
  54. }
  55. public void afterPropertiesSet() throws IOException {
  56. if (this.configLocation == null) {
  57. throw new IllegalArgumentException("configLocation is required");
  58. }
  59. // build the SqlMap
  60. InputStream is = this.configLocation.getInputStream();
  61. this.sqlMap = (this.sqlMapProperties != null) ?
  62. XmlSqlMapBuilder.buildSqlMap(new InputStreamReader(is), this.sqlMapProperties) :
  63. XmlSqlMapBuilder.buildSqlMap(new InputStreamReader(is));
  64. }
  65. public Object getObject() {
  66. return this.sqlMap;
  67. }
  68. public Class getObjectType() {
  69. return (this.sqlMap != null ? this.sqlMap.getClass() : SqlMap.class);
  70. }
  71. public boolean isSingleton() {
  72. return true;
  73. }
  74. }