<commit_before>package com.sixsq.slipstream.configuration;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * 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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.persistence.NoResultException;

import org.restlet.data.Protocol;
import org.restlet.data.Reference;

import slipstream.ui.views.Representation;

import com.sixsq.slipstream.connector.ConnectorFactory;
import com.sixsq.slipstream.exceptions.ConfigurationException;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.factory.ParametersFactory;
import com.sixsq.slipstream.persistence.ServiceConfiguration;
import com.sixsq.slipstream.persistence.ServiceConfiguration.RequiredParameters;
import com.sixsq.slipstream.persistence.ServiceConfigurationParameter;

/**
 * This singleton class is the interface to the service configuration. It
 * handles reading the services configuration files and makes the parameters
 * available via the getProperty() method. The configuration model is a simple:
 * all of the configuration is done at start up and changes to the configuration
 * require the service to be restarted.
 * 
 * The system first reads a set of default values. These may be overridden by a
 * user specified configuration file.
 * 
 * After setting the defaults, this class check if the "slipstream.config.file"
 * system property is set. If it is, it will be used as a filename to find the
 * configuration file. Users should probably use an absolute path when setting
 * the system property. A relative name will be resolved via the JVM and may not
 * have the desired effect.
 * 
 * If the system property is not set, then the file "slipstream.conf" is
 * searched for in the current working directory and then in the user's home
 * area.
 * 
 * If no configuration file is found, then just the default configuration is
 * used. The default configuration is likely to be incomplete (some critical
 * properties will not have reasonable default values) and will likely to cause
 * the service to fail during initialization elsewhere.
 * 
 * The singleton instance of this class is immutable, so clients are encouraged
 * to cache a copy of instance returned by getInstance() rather than reinvoking
 * the method.
 * 
 */
public class Configuration {

	/**
	 * Singleton instance. Since this will not be used heavily after startup use
	 * lazy, synchronized instantiation.
	 */
	private static Configuration instance = null;

	/**
	 * Name of the resource containing the default configuration parameters.
	 * This file must be in the same directory as this class.
	 */
	private static final String defaultPropertiesResource = "default.config.properties";

	/**
	 * Name of the system property used to set the location/name of the
	 * configuration file. It is suggested that users specify an absolute path
	 * name when using this system property.
	 */
	private static final String configSystemProperty = "slipstream.config.file";

	/**
	 * Name of the configuration file for the service.
	 */
	private static final String configFilename = "slipstream.conf";

	private ServiceConfiguration serviceConfiguration = new ServiceConfiguration();

	private Reference baseRef;

	private int defaultPort = 80;
	private int defaultSecurePort = 443;

	/**
	 * SlipStream version number derived from the slipstream.version property in
	 * the property default file.
	 */
	public String version;

	/**
	 * The base URL of the SlipStream service (without a trailing slash) as a
	 * String. This value will never be null and will never have a trailing
	 * slash.
	 */
	public String baseUrl;

	/**
	 * Return the singleton instance of a Configuration object. This method must
	 * be synchronized to ensure that only one instance of this class is
	 * constructed.
	 * 
	 * @return singleton Configuration instance
	 * 
	 * @throws ConfigurationException
	 *             if there is an error when reading the configuration
	 * @throws ValidationException 
	 */
	public static synchronized Configuration getInstance()
			throws ConfigurationException, ValidationException {
		if (instance == null) {
			instance = new Configuration();
		}
		return instance;
	}

	/**
	 * Private constructor, called only from getInstance(), ensures that this is
	 * a singleton class. The constructor will first load the default properties
	 * and then search for the user-specified configuration file.
	 * 
	 * @throws ConfigurationException
	 *             if an error occurs during the default initialization or when<commit_msg>Add helper method to assess boolean configuration parameter value<commit_after>package com.sixsq.slipstream.configuration;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * 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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.persistence.NoResultException;

import org.restlet.data.Protocol;
import org.restlet.data.Reference;

import slipstream.ui.views.Representation;

import com.sixsq.slipstream.connector.ConnectorFactory;
import com.sixsq.slipstream.exceptions.ConfigurationException;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.factory.ParametersFactory;
import com.sixsq.slipstream.persistence.ServiceConfiguration;
import com.sixsq.slipstream.persistence.ServiceConfiguration.RequiredParameters;
import com.sixsq.slipstream.persistence.ServiceConfigurationParameter;

/**
 * This singleton class is the interface to the service configuration. It
 * handles reading the services configuration files and makes the parameters
 * available via the getProperty() method. The configuration model is a simple:
 * all of the configuration is done at start up and changes to the configuration
 * require the service to be restarted.
 * 
 * The system first reads a set of default values. These may be overridden by a
 * user specified configuration file.
 * 
 * After setting the defaults, this class check if the "slipstream.config.file"
 * system property is set. If it is, it will be used as a filename to find the
 * configuration file. Users should probably use an absolute path when setting
 * the system property. A relative name will be resolved via the JVM and may not
 * have the desired effect.
 * 
 * If the system property is not set, then the file "slipstream.conf" is
 * searched for in the current working directory and then in the user's home
 * area.
 * 
 * If no configuration file is found, then just the default configuration is
 * used. The default configuration is likely to be incomplete (some critical
 * properties will not have reasonable default values) and will likely to cause
 * the service to fail during initialization elsewhere.
 * 
 * The singleton instance of this class is immutable, so clients are encouraged
 * to cache a copy of instance returned by getInstance() rather than reinvoking
 * the method.
 * 
 */
public class Configuration {

	/**
	 * Singleton instance. Since this will not be used heavily after startup use
	 * lazy, synchronized instantiation.
	 */
	private static Configuration instance = null;

	/**
	 * Name of the resource containing the default configuration parameters.
	 * This file must be in the same directory as this class.
	 */
	private static final String defaultPropertiesResource = "default.config.properties";

	/**
	 * Name of the system property used to set the location/name of the
	 * configuration file. It is suggested that users specify an absolute path
	 * name when using this system property.
	 */
	private static final String configSystemProperty = "slipstream.config.file";

	/**
	 * Name of the configuration file for the service.
	 */
	private static final String configFilename = "slipstream.conf";

	private ServiceConfiguration serviceConfiguration = new ServiceConfiguration();

	private Reference baseRef;

	private int defaultPort = 80;
	private int defaultSecurePort = 443;

	/**
	 * SlipStream version number derived from the slipstream.version property in
	 * the property default file.
	 */
	public String version;

	/**
	 * The base URL of the SlipStream service (without a trailing slash) as a
	 * String. This value will never be null and will never have a trailing
	 * slash.
	 */
	public String baseUrl;

	public static boolean isEnabled(String key) throws ValidationException {
		return Boolean.parseBoolean(Configuration.getInstance()
				.getProperty(key));
	}

	/**
	 * Return the singleton instance of a Configuration object. This method must
	 * be synchronized to ensure that only one instance of this class is
	 * constructed.
	 * 
	 * @return singleton Configuration instance
	 * 
	 * @throws ConfigurationException
	 *             if there is an error when reading the configuration
	 * @throws ValidationException
	 */
	public static synchronized Configuration getInstance()
			throws ConfigurationException, ValidationException {
		if (instance == null) {
			instance = new Configuration();
		}
		return instance;
	}

	/**
	 * Private constructor, called only from getInstance(), ensures that this is
	 * a singleton class. The constructor will first load the default properties
	 * and then search for the user-specified configuration file.
	 * 
	 * @throws ConfigurationException
	 *             if an error occurs during the default initialization or when
{'fraction_non_alphanumeric': 0.08931868131868131, 'fraction_numerical': 0.002285714285714286, 'mean_word_length': 4.208791208791209, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5110362', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2670, 'n_words': 1251}