Package eu.bitfish.jcf.common.config

Source Code of eu.bitfish.jcf.common.config.ConfigManager

/*
* This file is part of JCF.

* JCF is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JCF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with JCF.  If not, see <http://www.gnu.org/licenses/>.
*/
package eu.bitfish.jcf.common.config;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Manager class for loading and providing the configuration file.
*
* @author Michael Sieber
*
*/
public abstract class ConfigManager {
  private final static Logger LOGGER = LoggerFactory
      .getLogger(ConfigManager.class);
  private final static String CONFIG_FILE = "jcf.xml";
  private final static Configuration DEFAULT_CONFIG;
  private static Configuration _config;

  // property names

  // allowed are all classes which extend AbstractParser
  public final static String PARSER_CLASS = "parser.class";

  // the maximum size of cached queries. this has to be an integer
  public final static String PARSER_CACHE_MAX_SIZE = "parser.cache.maximumSize";

  // the time after which a cache entry expires after its last access in
  // minutes. this has to be an integer
  public final static String PARSER_CACHE_EXPIRE_AFTER_ACCESS = "parser.cache.expireAfterAccess";

  // allowed are all classes which extend AbstractProcessor
  public final static String PROCESSOR_CLASS = "processor.class";

  // the date pattern which is used to parse input dates. possible values:
  // dd/MM/yyyy
  // dd-MM-yyyy
  // dd.MM.yyyy
  public final static String PROCESSOR_DATE_PATTERN = "processor.date.pattern";

  static {
    // create default configuration
    DEFAULT_CONFIG = new XMLConfiguration();
    DEFAULT_CONFIG.addProperty(PARSER_CLASS,
        "eu.bitfish.jcf.parser.antlr4.ANTLR4Parser");
    DEFAULT_CONFIG.addProperty(PARSER_CACHE_MAX_SIZE, 1000);
    DEFAULT_CONFIG.addProperty(PARSER_CACHE_EXPIRE_AFTER_ACCESS, 1440); // minutes
    DEFAULT_CONFIG.addProperty(PROCESSOR_CLASS,
        "eu.bitfish.jcf.processor.DefaultProcessor");

    // important for date parsing
    DEFAULT_CONFIG.addProperty(PROCESSOR_DATE_PATTERN, "dd.MM.yyyy");
  }

  /**
   * Get the configuration. If no configuration file is available the default
   * settings will be applied.
   *
   * @return The configuration
   */
  public static Configuration getConfig() {
    if (_config == null) {
      _config = loadConfig();
    }

    return _config;
  }

  /**
   * Load the configuration file.
   *
   * @return The configuration object or the default config if the file cannot
   *         be loaded.
   */
  private static Configuration loadConfig() {
    CompositeConfiguration config = new CompositeConfiguration();

    try {
      LOGGER.debug("Trying to load configuration from file "
          + CONFIG_FILE);
      DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder(
          CONFIG_FILE);
      Configuration xmlConf = factory.getConfiguration();
      LOGGER.debug("Configuration successfully loaded.");
      config.addConfiguration(xmlConf);
    } catch (ConfigurationException e) {
      LOGGER.info("Unable to locate " + CONFIG_FILE
          + ". Using default configuration.");
    }

    // ensure that properties which are not set in the config files get
    // their default values
    config.addConfiguration(DEFAULT_CONFIG);
    return config;
  }

}
TOP

Related Classes of eu.bitfish.jcf.common.config.ConfigManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.