/**
 * 
 */
package net.brutex.xservices.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.exception.CacheException;


import net.brutex.mgmt.openair.OpenAirRestConnection;

/**
 * @author brosenberger
 *
 */
@Slf4j
public final class OpenAirConnection {

	
	public static OpenAirRestConnection getOpenAirConnection() {
		final PropertiesConfiguration props;
		try {
			final String config = "../openair.properties";
			log.info("Loading Open Air connection details from {}", config.toString());
			final URL configloc = OpenAirConnection.class.getClassLoader().getResource(config);
			log.debug("Loading Open Air connection details from {}", configloc.toString());

			props = new PropertiesConfiguration();
			props.read( new InputStreamReader( new BufferedInputStream(configloc.openStream())) );

			final String user = props.getString("user");
			final String password = props.getString("password");
			final String company = props.getString("company");
			final String apikey = props.getString("apikey", "_PUT_HERE_");
			final String namespace = props.getString("namespace", "");

			final OpenAirRestConnection con;

			con = new OpenAirRestConnection(JCS.getInstance("OACache"), company, user, password);
			return con;
		} catch (CacheException | ConfigurationException e) {
			log.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
		return null;
	}
	

}
