/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package web.component.impl.aws;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import web.component.impl.Cloud;
import web.component.impl.CloudBlock;

/**
 *
 * @author Hiroshi
 */
public class AWS implements Cloud{
    
    private static final AWS INSTANCE = new AWS();

    private final Properties AWS_CONFIG = new Properties();
    
    private final AWSCredentials AWS_CREDENTIALS;
    
    private final Map<BlockName, CloudBlock> components = new HashMap<>();

    private AWS(){     

        try {
            AWS_CONFIG.load(AWS.class.getResourceAsStream("./aws_config.txt"));
        } catch (IOException ex) {
            System.out.println("configuration file not found.");
            throw new RuntimeException(ex);
        }

        String awsKey = AWS_CONFIG.getProperty("aws.key");
        String secretKey = AWS_CONFIG.getProperty("aws.secret");
        
        AWS_CREDENTIALS = new BasicAWSCredentials(awsKey,secretKey);
    }
    
    //interface to access cloud.
    public static AWS access(){
        return INSTANCE;
    }
    
    private final CloudBlock get(BlockName name){
        
        CloudBlock cb = components.get(name);
        
        if(cb == null){
        
            if(BlockName.EC2.equals(name))
                cb = AWSEC2.get();
            if(BlockName.ELB.equals(name))
                cb = AWSELB.get();
            if(BlockName.AutoScaling.equals(name))
                cb = AWSAutoScaling.get();
            
            components.put(name, cb);
        }
        return cb;
    }
    
   /*
    * returns a copy instancd of AWS client config.
    */
    Properties conf(){

        Properties copyConf = new Properties();
        
        List<Object> keys = new ArrayList<>(AWS_CONFIG.keySet());
        for(Object key : keys){
            String keyString = (String)key;
            copyConf.setProperty(keyString, AWS_CONFIG.getProperty(keyString));
        }
        
        return copyConf;
    }
    
   /*
    * returns a copy instancd of AWS client credentials.
    */
    AWSCredentials credentials(){
        
        String awsKey = AWS_CREDENTIALS.getAWSAccessKeyId();
        String secretKey = AWS_CREDENTIALS.getAWSSecretKey();
        
        //return a copy instance.
        return new BasicAWSCredentials(awsKey, secretKey);
    }
    
    public AWSAutoScaling as(){
        return (AWSAutoScaling)get(BlockName.AutoScaling);
    }
    public AWSEC2 ec2(){
        return (AWSEC2)get(BlockName.EC2);
    }
    public AWSELB elb(){
        return (AWSELB)get(BlockName.ELB);
    }
    
    private static enum BlockName{
        EC2,ELB,AutoScaling;
    }
}

@@ -297,24 +297,45 @@
 redentials;%0A
+import java.io.File;%0A
 import java.
@@ -884,34 +884,609 @@
 ate 
-AWS()%7B     %0A%0A        try %7B
+final String CONF_DIR_PROP_KEY = %22aws.config.file.dir%22;%0A    private final String CONF_FILE_NAME = %22aws_config.txt%22;%0A    %0A    private AWS()%7B     %0A%0A        try %7B%0A            %0A            StringBuilder confPath = %0A                    new StringBuilder(System.getProperty(CONF_DIR_PROP_KEY, System.getProperty(%22user.dir%22)));%0A            if(!confPath.toString().endsWith(File.separator))%0A                confPath.append(File.separator);%0A            String confFile = confPath.append(CONF_FILE_NAME).toString();%0A            System.out.println(%22Loading aws client configuration from %5B%22 + confFile + %22%5D ...%22);
 %0A   
@@ -1544,29 +1544,32 @@
 eam(
-%22./aws_config.txt%22));
+confFile));%0A            
 %0A   
@@ -1594,16 +1594,35 @@
 ception 
+%7C RuntimeException 
 ex) %7B%0A  
@@ -1655,82 +1655,83 @@
 ln(%22
-configuration file not found.%22);%0A            throw new RuntimeException(ex
+failed to load aws client configuration.%22);%0A            ex.printStackTrace(
 );%0A 

modify the way to specify aws client config file path to read from system property.