/*
 * @Author: Kane
 * @Date: 2022-04-25 21:45:12
 * @LastEditors: Kane
 * @LastEditTime: 2022-04-27 17:10:57
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfigLoader.java
 * @Description: oracle数据库配置文件加载类
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.config.db;

import java.io.*;

import com.fasterxml.jackson.databind.*;

public class OracleConfigLoader
{
    private static final String CONFIG_FILE_PATH = "./db.json";
    private static final String CONFIG_FILE_CHARSET = "UTF-8";
    private static final int BUFFER_SIZE = 1024;
    private static OracleConfig dbConfig = null;

    private OracleConfigLoader()
    {}

    public static OracleConfig getOracleConfig() throws IOException
    {
        FileInputStream file = null;
        InputStreamReader in = null;
        StringBuffer json = null;
        char[] buffer = new char[BUFFER_SIZE];

        if ( dbConfig != null)
        {
            return dbConfig;
        }

        try
        {
            file = new FileInputStream( CONFIG_FILE_PATH );
            in = new InputStreamReader( file, CONFIG_FILE_CHARSET );
            json = new StringBuffer();

            int count = in.read( buffer );

            while (count != -1)
            {
                json.append( buffer );

                count = in.read( buffer );
            }

            ObjectMapper mapper = new ObjectMapper();

            dbConfig = mapper.readValue( json.toString(), OracleConfig.class );
        }
        catch ( IOException error )
        {
            throw error;
        }
        finally
        {
            try
            {
                file.close();
            }
            catch ( Exception error )
            {
                error.printStackTrace();
            }

        }

        return dbConfig;
    }
}
