com.xtesy.core.internals.impl.Settings.java Source code

Java tutorial

Introduction

Here is the source code for com.xtesy.core.internals.impl.Settings.java

Source

/**
 * Copyright 2015, Wasiq Amjad Bhamla.
 *
 * 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.
 */
package com.xtesy.core.internals.impl;

import java.io.FileNotFoundException;
import java.net.URL;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.xtesy.core.exception.FrameworkException;
import com.xtesy.core.utils.Constants;
import com.xtesy.core.utils.FileUtils;

/**
 * @author Wasiq B
 * @since 31-May-2015 9:07:23 pm
 */
public class Settings extends CompositeConfiguration {
    private static final Logger log = LogManager.getLogger(Settings.class);
    private PropertiesConfiguration prop;

    /**
     * @author Wasiq B
     * @since 31-May-2015 9:23:00 pm
     */
    private Settings() {
        super();
        log.entry();
        try {
            addConfiguration(new SystemConfiguration());
            parseConfig();
        } catch (final ConfigurationException | FileNotFoundException e) {
            try {
                throw new FrameworkException("Load Settings", e);
            } catch (final FrameworkException e1) {
                log.catching(e1);
                log.error(ExceptionUtils.getRootCauseMessage(e1));
                log.error(ExceptionUtils.getStackTrace(e1));
            }
        } finally {
            log.exit();
        }
    }

    /**
     * @author Wasiq B
     * @since 05-Jul-2015 12:09:18 pm
     * @return instance
     */
    public static Settings config() {
        return new Settings();
    }

    /**
     * @author Wasiq B
     * @throws ConfigurationException
     * @throws FileNotFoundException
     * @since 04-Jul-2015 6:17:05 pm
     */
    private void parseConfig() throws ConfigurationException, FileNotFoundException {
        String file = getString(Constants.TEST_CONFIG_FILE);
        if (file == null) {
            file = "test.properties";
        }
        final URL url = FileUtils.getResource(file);
        if (url == null)
            throw new FileNotFoundException(file);
        this.prop = new PropertiesConfiguration(url);
        this.prop.setAutoSave(true);
        this.prop.setReloadingStrategy(new FileChangedReloadingStrategy());
        addConfiguration(this.prop);
    }
}