001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
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 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
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 */
016package com.mybatisflex.core.mybatis;
017
018import com.mybatisflex.core.FlexConsts;
019import com.mybatisflex.core.FlexGlobalConfig;
020import com.mybatisflex.core.exception.FlexExceptions;
021import org.apache.ibatis.builder.xml.XMLConfigBuilder;
022import org.apache.ibatis.exceptions.ExceptionFactory;
023import org.apache.ibatis.executor.ErrorContext;
024import org.apache.ibatis.session.Configuration;
025import org.apache.ibatis.session.SqlSessionFactory;
026import org.apache.ibatis.session.SqlSessionFactoryBuilder;
027
028import java.io.IOException;
029import java.io.InputStream;
030import java.io.Reader;
031import java.util.Properties;
032
033public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
034
035    @Override
036    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
037        try {
038            // 需要 mybatis v3.5.13+
039            // https://github.com/mybatis/mybatis-3/commit/d7826d71a7005a8b4d4e0e7a020db0f6c7e253a4
040            XMLConfigBuilder parser = new XMLConfigBuilder(FlexConfiguration.class, reader, environment, properties);
041            return build(parser.parse());
042        } catch (Exception e) {
043            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
044        } finally {
045            ErrorContext.instance().reset();
046            try {
047                if (reader != null) {
048                    reader.close();
049                }
050            } catch (IOException e) {
051                // Intentionally ignore. Prefer previous error.
052            }
053        }
054    }
055
056
057    @Override
058    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
059        try {
060            // 需要 mybatis v3.5.13+
061            // https://github.com/mybatis/mybatis-3/commit/d7826d71a7005a8b4d4e0e7a020db0f6c7e253a4
062            XMLConfigBuilder parser = new XMLConfigBuilder(FlexConfiguration.class, inputStream, environment, properties);
063            return build(parser.parse());
064        } catch (Exception e) {
065            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
066        } finally {
067            ErrorContext.instance().reset();
068            try {
069                if (inputStream != null) {
070                    inputStream.close();
071                }
072            } catch (IOException e) {
073                // Intentionally ignore. Prefer previous error.
074            }
075        }
076    }
077
078
079    @Override
080    public SqlSessionFactory build(Configuration configuration) {
081        if (!FlexConfiguration.class.isAssignableFrom(configuration.getClass())) {
082            throw FlexExceptions.wrap("only support FlexMybatisConfiguration.");
083        }
084
085        SqlSessionFactory sessionFactory = super.build(configuration);
086
087        // 设置全局配置的 sessionFactory
088        initGlobalConfig(configuration, sessionFactory);
089
090        printBanner();
091
092        return sessionFactory;
093    }
094
095
096    private void printBanner() {
097        if (!FlexGlobalConfig.getDefaultConfig().isPrintBanner()) {
098            return;
099        }
100        String banner = "  __  __       _           _   _       _____ _           \n" +
101            " |  \\/  |_   _| |__   __ _| |_(_)___  |  ___| | _____  __\n" +
102            " | |\\/| | | | | '_ \\ / _` | __| / __| | |_  | |/ _ \\ \\/ /\n" +
103            " | |  | | |_| | |_) | (_| | |_| \\__ \\ |  _| | |  __/>  < \n" +
104            " |_|  |_|\\__, |_.__/ \\__,_|\\__|_|___/ |_|   |_|\\___/_/\\_\\\n" +
105            "         |___/ v" + FlexConsts.VERSION + " https://mybatis-flex.com";
106        System.out.println(banner);
107    }
108
109    /**
110     * 设置全局配置
111     *
112     * @param configuration
113     * @param sessionFactory
114     */
115    private void initGlobalConfig(Configuration configuration, SqlSessionFactory sessionFactory) {
116        String environmentId = configuration.getEnvironment().getId();
117
118        FlexGlobalConfig globalConfig = FlexGlobalConfig.getConfig(environmentId);
119        if (globalConfig == null){
120            globalConfig = new FlexGlobalConfig();
121        }
122
123        globalConfig.setSqlSessionFactory(sessionFactory);
124        globalConfig.setConfiguration(configuration);
125
126        FlexGlobalConfig.setConfig(environmentId, globalConfig,true);
127    }
128
129
130}