All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.settings.AbstractSettings Maven / Gradle / Ivy

Go to download

This project contains some common utilities used in different projects from theMatchBox.

There is a newer version: 2.0.2
Show newest version
package com.thematchbox.settings;

/* Copyright 2015 theMatchBox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

public abstract class AbstractSettings {

    public static final Logger logger = LoggerFactory.getLogger(AbstractSettings.class);

    public static  S load(Class clazz, String configFile) {
        File file = new File(configFile);
        if (file.exists()) {
            try (InputStream inputStream = new FileInputStream(file)) {
                return load(clazz, inputStream);
            } catch (IOException e) {
                logger.error("Failed to load settings file " + file.getPath(), e);
            }
        }

        return createNewConfig(clazz, configFile);
    }

    public static  S createNewConfig(Class clazz, String configFile) {
        try {
            java.lang.reflect.Constructor constructor = clazz.getConstructor();
            S newInstance = constructor.newInstance();
            newInstance.write(configFile);
            return newInstance;
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    public static  S load(Class clazz, InputStream inputStream) {
        if (inputStream == null) {
            logger.error("Failed to load settings file as resource.");

            return null;
        }

        Yaml yaml = new Yaml(new Constructor(clazz));

        //noinspection unchecked
        return (S) yaml.load(inputStream);
    }

    public void write(String configFile) {
        Yaml yaml = new Yaml();
        File file = new File(configFile);
        String s = yaml.dumpAsMap(this);
        try {
            FileUtils.writeStringToFile(file, s);
        } catch (IOException e) {
            logger.error("Failed to write analyser settings to file " + file.getPath(), e);
        }
    }

}