/*
 * @Author: Kane
 * @Date: 2022-05-10 16:06:14
 * @LastEditors: Kane
 * @LastEditTime: 2022-05-10 16:09:07
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\AppConfigManager.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

package com.cpic.xim.config;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class AppConfigManager
{
    private static final int BUFFER_SIZE = 1024;
    private static final String CONFIG_FILE_CHARSET = "UTF-8";
    private static final String CONFIG_FILE_PATH = "./config.json";
    private static WeatherDisasterNotifyConfig appConfig = null;

    public static WeatherDisasterNotifyConfig getConfig() throws IOException

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

        ObjectMapper mapper = new ObjectMapper();
        FileInputStream configFile = null;
        InputStreamReader in = null;
        StringBuffer json = null;
        char[] buffer = new char[BUFFER_SIZE];

        // 设置json属性
        mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );

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

            int length = in.read( buffer );

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

                length = in.read( buffer );
            }

            appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
        }
        finally
        {
            if ( configFile != null)
            {
                try
                {
                    configFile.close();
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
        }

        return appConfig;
    }
}
