<commit_before>package org.jimmutable.cloud;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.jimmutable.cloud.elasticsearch.ElasticSearch;
import org.jimmutable.cloud.elasticsearch.ElasticSearchEndpoint;
import org.jimmutable.cloud.elasticsearch.ISearch;
import org.jimmutable.cloud.elasticsearch.StubSearch;
import org.jimmutable.cloud.logging.Log4jUtil;
import org.jimmutable.cloud.messaging.queue.IQueue;
import org.jimmutable.cloud.messaging.queue.QueueRedis;
import org.jimmutable.cloud.messaging.queue.QueueStub;
import org.jimmutable.cloud.messaging.signal.ISignal;
import org.jimmutable.cloud.messaging.signal.SignalRedis;
import org.jimmutable.cloud.messaging.signal.SignalStub;
import org.jimmutable.cloud.storage.IStorage;
import org.jimmutable.cloud.storage.StandardImmutableObjectCache;
import org.jimmutable.cloud.storage.StorageDevLocalFileSystem;
import org.jimmutable.cloud.storage.StubStorage;
import org.jimmutable.cloud.storage.s3.RegionSpecificAmazonS3ClientFactory;
import org.jimmutable.cloud.storage.s3.StorageS3;
import org.jimmutable.core.serialization.JimmutableTypeNameRegister;

/**
 * Configures environment and application specific setting, to be used by other
 * classes. This includes EnvironmantType, ApplicationId, and logging settings.
 *
 * @author trevorbox
 *
 */
public class CloudExecutionEnvironment
{
	private static Logger logger;
	private static CloudExecutionEnvironment CURRENT;

	private ISearch search;
	private IStorage storage;
	private IQueue queue_service;
	private ISignal signal_service;
	
	// System properties
	private static final String ENV_TYPE_VARIABLE_NAME = "JIMMUTABLE_ENV_TYPE";
	private static final String ENV_LOGGING_LEVEL = "JIMMUTABLE_LOGGING_LEVEL";

	private static final Level DEFAULT_LEVEL = Level.INFO;

	private static EnvironmentType ENV_TYPE;
	private static ApplicationId APPLICATION_ID;
	private static StandardImmutableObjectCache STANDARD_IMMUTABLE_OBJECT_CACHE;

	// setup the logging level programmatically
	static
	{
		Level level = Level.toLevel(System.getProperty(ENV_LOGGING_LEVEL), DEFAULT_LEVEL);
		Log4jUtil.setAllLoggerLevels(level);
		logger = LogManager.getLogger(CloudExecutionEnvironment.class);
		logger.trace(String.format("Logging level: %s", level));
	}

	private CloudExecutionEnvironment(ISearch search, IStorage storage, IQueue queue_service, ISignal signal_service)
	{
		this.search = search;
		this.storage = storage;
		this.queue_service = queue_service;
		this.signal_service = signal_service;
	}

	public EnvironmentType getSimpleEnvironmentType()
	{
		return ENV_TYPE;
	}

	public ApplicationId getSimpleApplicationId()
	{
		return APPLICATION_ID;
	}

	/**
	 * Search instance used for document upsert and searching of indices
	 *
	 * @return The Search instance
	 */
	public ISearch getSimpleSearch()
	{
		return search;
	}

	/**
	 * Storage system the application uses to store objects
	 *
	 * @return Storage The storage system
	 */
	public IStorage getSimpleStorage()
	{
		return storage;
	}

	public IQueue getSimpleQueueService()
	{
		return queue_service;
	}
	
	public ISignal getSimpleSignalService()
	{
		return signal_service;
	}

	/**
	 *
	 * ONLY CALL THIS METHOD ONCE
	 *
	 * Startup must be called within the main method of the application and will
	 * attempt to set the environment type and application id based on the
	 * environment variables JIMMUTABLE_ENV_TYPE and JIMMUTABLE_APP_ID.
	 *
	 * The DEV environment variable is the default if the variable is not provided
	 *
	 * @param default_id
	 *            A default application id in case the environment variable is not
	 *            set
	 */
	@SuppressWarnings("resource")
	public static void startup(ApplicationId application_id, EnvironmentType env_type)
	{

		if (CURRENT != null)
		{
			throw new RuntimeException("Startup has already been called!");
		}

		// register objects

		ENV_TYPE = env_type;
		APPLICATION_ID = application_id;

		logger.info(String.format("ApplicationID=%s Environment=%s", APPLICATION_ID, ENV_TYPE));

		switch (env_type)
		{
		// For now, staging is the same as dev
		case STAGING:
		case DEV:

			checkOs();

			TransportClient client = null;
			try
			{
				client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new TransportAddress(InetAddress.getByName(ElasticSearchEndpoint.CURRENT.getSimpleHost()), ElasticSearchEndpoint.CURRENT.getSimplePort()));
			}
			catch (UnknownHostException e)
			{
				logger.log(Level.FATAL, "Failed to instantiate the elasticsearch client!", e);
			}

			if (client == null)
			{
				throw new RuntimeException("Failed to instantiate the elasticsearch client!");
			}

			CURRENT = new CloudExecutionEnvironment(new ElasticSearch(client), new StorageDevLocalFileSystem(false, APPLICATION_ID), new QueueRedis(APPLICATION_ID), new SignalRedis(APPLICATION_ID));

			break;
		case PRODUCTION:
			checkOs();
			
			TransportClient prod_client = null;<commit_msg>added debug statement (help troubleshoot issues starting in production)<commit_after>package org.jimmutable.cloud;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.jimmutable.cloud.elasticsearch.ElasticSearch;
import org.jimmutable.cloud.elasticsearch.ElasticSearchEndpoint;
import org.jimmutable.cloud.elasticsearch.ISearch;
import org.jimmutable.cloud.elasticsearch.StubSearch;
import org.jimmutable.cloud.logging.Log4jUtil;
import org.jimmutable.cloud.messaging.queue.IQueue;
import org.jimmutable.cloud.messaging.queue.QueueRedis;
import org.jimmutable.cloud.messaging.queue.QueueStub;
import org.jimmutable.cloud.messaging.signal.ISignal;
import org.jimmutable.cloud.messaging.signal.SignalRedis;
import org.jimmutable.cloud.messaging.signal.SignalStub;
import org.jimmutable.cloud.storage.IStorage;
import org.jimmutable.cloud.storage.StandardImmutableObjectCache;
import org.jimmutable.cloud.storage.StorageDevLocalFileSystem;
import org.jimmutable.cloud.storage.StubStorage;
import org.jimmutable.cloud.storage.s3.RegionSpecificAmazonS3ClientFactory;
import org.jimmutable.cloud.storage.s3.StorageS3;
import org.jimmutable.core.serialization.JimmutableTypeNameRegister;

/**
 * Configures environment and application specific setting, to be used by other
 * classes. This includes EnvironmantType, ApplicationId, and logging settings.
 *
 * @author trevorbox
 *
 */
public class CloudExecutionEnvironment
{
	private static Logger logger;
	private static CloudExecutionEnvironment CURRENT;

	private ISearch search;
	private IStorage storage;
	private IQueue queue_service;
	private ISignal signal_service;
	
	// System properties
	private static final String ENV_TYPE_VARIABLE_NAME = "JIMMUTABLE_ENV_TYPE";
	private static final String ENV_LOGGING_LEVEL = "JIMMUTABLE_LOGGING_LEVEL";

	private static final Level DEFAULT_LEVEL = Level.INFO;

	private static EnvironmentType ENV_TYPE;
	private static ApplicationId APPLICATION_ID;
	private static StandardImmutableObjectCache STANDARD_IMMUTABLE_OBJECT_CACHE;

	// setup the logging level programmatically
	static
	{
		Level level = Level.toLevel(System.getProperty(ENV_LOGGING_LEVEL), DEFAULT_LEVEL);
		Log4jUtil.setAllLoggerLevels(level);
		logger = LogManager.getLogger(CloudExecutionEnvironment.class);
		logger.trace(String.format("Logging level: %s", level));
	}

	private CloudExecutionEnvironment(ISearch search, IStorage storage, IQueue queue_service, ISignal signal_service)
	{
		this.search = search;
		this.storage = storage;
		this.queue_service = queue_service;
		this.signal_service = signal_service;
	}

	public EnvironmentType getSimpleEnvironmentType()
	{
		return ENV_TYPE;
	}

	public ApplicationId getSimpleApplicationId()
	{
		return APPLICATION_ID;
	}

	/**
	 * Search instance used for document upsert and searching of indices
	 *
	 * @return The Search instance
	 */
	public ISearch getSimpleSearch()
	{
		return search;
	}

	/**
	 * Storage system the application uses to store objects
	 *
	 * @return Storage The storage system
	 */
	public IStorage getSimpleStorage()
	{
		return storage;
	}

	public IQueue getSimpleQueueService()
	{
		return queue_service;
	}
	
	public ISignal getSimpleSignalService()
	{
		return signal_service;
	}

	/**
	 *
	 * ONLY CALL THIS METHOD ONCE
	 *
	 * Startup must be called within the main method of the application and will
	 * attempt to set the environment type and application id based on the
	 * environment variables JIMMUTABLE_ENV_TYPE and JIMMUTABLE_APP_ID.
	 *
	 * The DEV environment variable is the default if the variable is not provided
	 *
	 * @param default_id
	 *            A default application id in case the environment variable is not
	 *            set
	 */
	@SuppressWarnings("resource")
	public static void startup(ApplicationId application_id, EnvironmentType env_type)
	{

		if (CURRENT != null)
		{
			throw new RuntimeException("Startup has already been called!");
		}

		// register objects

		ENV_TYPE = env_type;
		APPLICATION_ID = application_id;

		logger.info(String.format("ApplicationID=%s Environment=%s", APPLICATION_ID, ENV_TYPE));

		switch (env_type)
		{
		// For now, staging is the same as dev
		case STAGING:
		case DEV:

			checkOs();

			TransportClient client = null;
			try
			{
				client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new TransportAddress(InetAddress.getByName(ElasticSearchEndpoint.CURRENT.getSimpleHost()), ElasticSearchEndpoint.CURRENT.getSimplePort()));
			}
			catch (UnknownHostException e)
			{
				logger.log(Level.FATAL, "Failed to instantiate the elasticsearch client!", e);
			}

			if (client == null)
			{
				throw new RuntimeException("Failed to instantiate the elasticsearch client!");
			}

			CURRENT = new CloudExecutionEnvironment(new ElasticSearch(client), new StorageDevLocalFileSystem(false, APPLICATION_ID), new QueueRedis(APPLICATION_ID), new SignalRedis(APPLICATION_ID));

			break;
		case PRODUCTION:
			checkOs();
			
			logger.log(Level.INFO, "Starting production environment");
			
			TransportClient prod_client = null;
{'fraction_non_alphanumeric': 0.08136726732305406, 'fraction_numerical': 0.0016719301504737136, 'mean_word_length': 5.10374149659864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3957858', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3132, 'n_words': 765}