View Javadoc
1   /**
2    *    Copyright 2009-2015 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.apache.ibatis.session;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.Reader;
21  import java.util.Properties;
22  
23  import org.apache.ibatis.builder.xml.XMLConfigBuilder;
24  import org.apache.ibatis.exceptions.ExceptionFactory;
25  import org.apache.ibatis.executor.ErrorContext;
26  import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
27  
28  /*
29   * Builds {@link SqlSession} instances.
30   *
31   */
32  /**
33   * @author Clinton Begin
34   */
35  public class SqlSessionFactoryBuilder {
36  
37    public SqlSessionFactory build(Reader reader) {
38      return build(reader, null, null);
39    }
40  
41    public SqlSessionFactory build(Reader reader, String environment) {
42      return build(reader, environment, null);
43    }
44  
45    public SqlSessionFactory build(Reader reader, Properties properties) {
46      return build(reader, null, properties);
47    }
48  
49    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
50      try {
51        XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
52        return build(parser.parse());
53      } catch (Exception e) {
54        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
55      } finally {
56        ErrorContext.instance().reset();
57        try {
58          reader.close();
59        } catch (IOException e) {
60          // Intentionally ignore. Prefer previous error.
61        }
62      }
63    }
64  
65    public SqlSessionFactory build(InputStream inputStream) {
66      return build(inputStream, null, null);
67    }
68  
69    public SqlSessionFactory build(InputStream inputStream, String environment) {
70      return build(inputStream, environment, null);
71    }
72  
73    public SqlSessionFactory build(InputStream inputStream, Properties properties) {
74      return build(inputStream, null, properties);
75    }
76  
77    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
78      try {
79        XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
80        return build(parser.parse());
81      } catch (Exception e) {
82        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
83      } finally {
84        ErrorContext.instance().reset();
85        try {
86          inputStream.close();
87        } catch (IOException e) {
88          // Intentionally ignore. Prefer previous error.
89        }
90      }
91    }
92      
93    public SqlSessionFactory build(Configuration config) {
94      return new DefaultSqlSessionFactory(config);
95    }
96  
97  }