/*
 * Copyright 2014 Stackify
 *
 * 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.stackify.api.common;

import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ApiConfigurations
 * @author Eric Martin
 */
public class ApiConfigurations {

	/**
	 * Logger
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(ApiConfigurations.class);

	/**
	 * Explicitly configure the API
	 * @param apiUrl API URL
	 * @param apiKey API Key
	 * @param application Configured application name
	 * @param environment Configured environment name
	 * @return ApiConfiguration
	 */
	public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl, final String apiKey, final String application, final String environment) {
		ApiConfiguration props = ApiConfigurations.fromProperties();
		
		String mergedApiUrl = ((apiUrl != null) && (0 < apiUrl.length())) ? apiUrl : props.getApiUrl();
		String mergedApiKey = ((apiKey != null) && (0 < apiKey.length())) ? apiKey : props.getApiKey();
		String mergedApplication = ((application != null) && (0 < application.length())) ? application : props.getApplication();
		String mergedEnvironment = ((environment != null) && (0 < environment.length())) ? environment : props.getEnvironment();
		
		ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();
		builder.apiUrl(mergedApiUrl);
		builder.apiKey(mergedApiKey);
		builder.application(mergedApplication);
		builder.environment(mergedEnvironment);
		builder.envDetail(EnvironmentDetails.getEnvironmentDetail(mergedApplication, mergedEnvironment));
		
		return builder.build();
	}
	
	/**
	 * @return ApiConfiguration read from the stackify-api.properties file
	 */
	public static ApiConfiguration fromProperties() {
		
		ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();

		FileReader confFileReader = null;
		
		try {
			URL confFileUrl = ApiConfigurations.class.getResource("/stackify-api.properties");
			File confFile = new File(confFileUrl.toURI());
						
			if (confFile.exists()) {
	
				confFileReader = new FileReader(confFile);
			
				Properties confProps = new Properties();
				confProps.load(confFileReader);
				
				String apiUrl = null;
				
				if (confProps.containsKey("stackify.apiUrl")) {
					apiUrl = confProps.getProperty("stackify.apiUrl");
				}
				
				String apiKey = confProps.getProperty("stackify.apiKey");
				String application = confProps.getProperty("stackify.application");
				String environment = confProps.getProperty("stackify.environment");
				
				builder.apiUrl(apiUrl);
				builder.apiKey(apiKey);
				builder.application(application);
				builder.environment(environment);
				builder.envDetail(EnvironmentDetails.getEnvironmentDetail(application, environment));
			}
			
		} catch (Throwable t) {
			LOGGER.error("Exception reading stackify-api.properties configuration file", t);
		} finally {
			if (confFileReader != null) {
				try {
					confFileReader.close();
				} catch (Throwable t) {
					LOGGER.info("Exception closing stackify-api.properties configuration file", t);
				}
			}
		}
		
		return builder.build();
	}
	
	/**
	 * Hidden to prevent construction
	 */
	private ApiConfigurations() {
	}
}

@@ -2552,16 +2552,51 @@
 s%22);%0A%09%09%09
+%0A%09%09%09if (confFileUrl != null) %7B%0A%09%09%09%09
 File con
@@ -2640,17 +2640,19 @@
 ;%0A%09%09%09%09%09%09
-%0A
+%09%0A%09
 %09%09%09if (c
@@ -2672,21 +2672,23 @@
 s()) %7B%0A%09
+%09
 %0A%09%09%09%09
+%09
 confFile
@@ -2725,21 +2725,23 @@
 le);%0A%09%09%09
+%09
 %0A%09%09%09%09
+%09
 Properti
@@ -2769,24 +2769,25 @@
 operties();%0A
+%09
 %09%09%09%09confProp
@@ -2810,25 +2810,27 @@
 eader);%0A%09%09%09%09
-%0A
+%09%0A%09
 %09%09%09%09String a
@@ -2843,29 +2843,31 @@
 = null;%0A%09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 if (confProp
@@ -2898,24 +2898,25 @@
 apiUrl%22)) %7B%0A
+%09
 %09%09%09%09%09apiUrl 
@@ -2967,19 +2967,22 @@
 %09%09%09%09
+%09
 %7D%0A%09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 Stri
@@ -3031,24 +3031,25 @@
 y.apiKey%22);%0A
+%09
 %09%09%09%09String a
@@ -3108,24 +3108,25 @@
 tion%22);%0A%09%09%09%09
+%09
 String envir
@@ -3185,21 +3185,23 @@
 %22);%0A%09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 builder.
@@ -3216,16 +3216,17 @@
 piUrl);%0A
+%09
 %09%09%09%09buil
@@ -3245,24 +3245,25 @@
 piKey);%0A%09%09%09%09
+%09
 builder.appl
@@ -3284,16 +3284,17 @@
 ation);%0A
+%09
 %09%09%09%09buil
@@ -3323,24 +3323,25 @@
 nment);%0A%09%09%09%09
+%09
 builder.envD
@@ -3417,21 +3417,23 @@
 t));%0A%09%09%09
+%09
 %7D%0A%09%09%09
+%7D
 %0A%09%09%7D cat

Check existence of properties before constructing File object