component.Configuration.java Source code

Java tutorial

Introduction

Here is the source code for component.Configuration.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package component;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONObject;
import java.io.File;
import java.io.FileReader;
import model.Config;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author roman
 */
public class Configuration {

    public static void createConfigFile() throws IOException {
        JSONObject config = new JSONObject();
        List<String> queryList = new ArrayList<>();
        Map<String, List<String>> queries = new HashMap<>();
        try (Writer writer = new FileWriter("config.json", false)) {
            queryList.add("full_message:(Exception)");
            queryList.add("full_message:(Error)");
            config.put("queries", queryList);
            config.put("range", 3600);

            config.writeJSONString(writer);
            writer.flush();
        }
    }

    public static boolean loadConfigFile() {
        Config loadConfig;
        try {
            File file = new File("config.json");
            if (!file.exists()) {
                return false;
            }
            JSONParser parser = new JSONParser();
            JSONObject object;
            object = (JSONObject) parser.parse(new FileReader(file));
            loadConfig = new Config();
            loadConfig.load(object);
            if (loadConfig.isConfigFill()) {
                Configuration.config = loadConfig;
                return true;
            }
            return false;
        } catch (IOException | ParseException ex) {
            return false;
        }
    }

    public static Config getInstance() {
        return Configuration.config;
    }

    private static Config config;

}