package com.dm.awstasks.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;

import com.dm.awstasks.ec2.InstanceGroup;
import com.dm.awstasks.ec2.InstanceGroupImpl;
import com.xerox.amazonws.ec2.Jec2;
import com.xerox.amazonws.ec2.LaunchConfiguration;

/**
 * Configuration class that can be used to read all ec2 access information from an properties file.
 */
public class Ec2Configuration {

    public static final String EC2_PROPERTIES_FILE = "/ec2.properties";
    private static final String ACCESS_KEY = "ec2.accessKey";
    private static final String ACCESS_KEY_SECRET = "ec2.accessSecret";
    private static final String PRIVATE_KEY_NAME = "ec2.privateKeyName";
    private static final String PRIVATE_KEY_FILE = "ec2.privateKeyFile";

    protected String _accessKeyId;
    protected String _accessKeySecret;
    protected String _privateKeyName;
    protected String _privateKeyFile;

    /**
     * Reads a ec2.properties from classpath. The path is {@link #EC2_PROPERTIES_FILE}.
     */
    public Ec2Configuration() throws IOException {
        InputStream inputStream = Ec2Configuration.class.getResourceAsStream(EC2_PROPERTIES_FILE);
        if (inputStream == null) {
            throw new IOException(EC2_PROPERTIES_FILE + " not found in classpath");
        }
        Properties properties = new Properties();
        properties.load(inputStream);
        _accessKeyId = properties.getProperty(ACCESS_KEY);
        _accessKeySecret = properties.getProperty(ACCESS_KEY_SECRET);
        _privateKeyName = properties.getProperty(PRIVATE_KEY_NAME);
        _privateKeyFile = properties.getProperty(PRIVATE_KEY_FILE);
    }

    public boolean isEc2Configured() {
        return !nullOrEmpty(_accessKeyId, _accessKeySecret, _privateKeyName, _privateKeyFile);
    }

    public String getAccessKey() {
        return _accessKeyId;
    }

    public String getAccessSecret() {
        return _accessKeySecret;
    }

    public String getPrivateKeyName() {
        return _privateKeyName;
    }

    public String getPrivateKeyFile() {
        return _privateKeyFile;
    }

    public LaunchConfiguration createLaunchConfiguration(String ami, String group, int instanceCount) {
        LaunchConfiguration launchConfiguration = new LaunchConfiguration(ami, instanceCount, instanceCount);
        launchConfiguration.setKeyName(getPrivateKeyName());
        launchConfiguration.setSecurityGroup(Arrays.asList("default", group));
        return launchConfiguration;
    }

    public Jec2 createJEc2() {
        return new Jec2(_accessKeyId, _accessKeySecret);
    }

    public InstanceGroup createInstanceGroup() {
        return new InstanceGroupImpl(createJEc2());
    }

    private static boolean nullOrEmpty(String... strings) {
        boolean nullOrEmpty = false;
        for (String string : strings) {
            if (nullOrEmpty(string)) {
                nullOrEmpty = true;
            }
        }
        return nullOrEmpty;

    }

    private static boolean nullOrEmpty(String string) {
        return string == null || string.trim().length() == 0;
    }

}

@@ -805,16 +805,52 @@
 File%22;%0A%0A
+    private Properties _properties;%0A
     prot
@@ -872,24 +872,24 @@
 ccessKeyId;%0A
-
     protecte
@@ -1383,27 +1383,17 @@
         
-Properties 
+_
 properti
@@ -1423,16 +1423,17 @@
         
+_
 properti
@@ -1477,16 +1477,17 @@
 KeyId = 
+_
 properti
@@ -1541,16 +1541,17 @@
 ecret = 
+_
 properti
@@ -1607,24 +1607,25 @@
 teKeyName = 
+_
 properties.g
@@ -1680,16 +1680,17 @@
 yFile = 
+_
 properti
@@ -2135,24 +2135,24 @@
 KeyFile() %7B%0A
-
         retu
@@ -2170,32 +2170,307 @@
 KeyFile;%0A    %7D%0A%0A
+    public String getProperty(String name) %7B%0A        String property = _properties.getProperty(name);%0A        if (property == null) %7B%0A            throw new IllegalArgumentException(%22no property with name '%22 + name + %22' configured%22);%0A        %7D%0A        return property;%0A    %7D%0A%0A
     public Launc

allow custom property retrieval from Ec2Configuration