/*
 * 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.File;
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 final String CONF_DIR_PROP_KEY = "aws.config.file.dir";
    private final String CONF_FILE_NAME = "aws_config.txt";
    
    private AWS(){     

        try {
            
            StringBuilder confPath = 
                    new StringBuilder(System.getProperty(CONF_DIR_PROP_KEY, System.getProperty("user.dir")));
            if(!confPath.toString().endsWith(File.separator))
                confPath.append(File.separator);
            String confFile = confPath.append(CONF_FILE_NAME).toString();
            System.out.println("Loading aws client configuration from [" + confFile + "] ...");
            AWS_CONFIG.load(AWS.class.getResourceAsStream(confFile));
            
        } catch (IOException | RuntimeException ex) {
            System.out.println("failed to load aws client configuration.");
            ex.printStackTrace();
        }

        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;
    }
}

@@ -322,16 +322,48 @@
 o.File;%0A
+import java.io.FileInputStream;%0A
 import j
@@ -647,24 +647,172 @@
  Cloud%7B%0A    
+%0A%0A    private static final String CONF_DIR_PROP_KEY = %22aws.config.file.dir%22;%0A    private static final String CONF_FILE_NAME = %22aws_config.txt%22;%0A    
 %0A    private
@@ -881,26 +881,20 @@
 perties 
-AWS_
 CONF
-IG
  = new P
@@ -901,25 +901,24 @@
 roperties();
-%0A
     %0A    pri
@@ -960,21 +960,16 @@
 NTIALS;%0A
-    %0A
     priv
@@ -1052,154 +1052,222 @@
 ate 
-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
+AWS()%7B%0A        %0A        loadClientConfig();%0A        AWS_CREDENTIALS = new BasicAWSCredentials(CONF.getProperty(%22aws.key%22),CONF.getProperty(%22aws.secret%22));  %0A    %7D%0A    %0A    private void loadClientConfig()%7B%0A   
      %0A
-%0A
     
@@ -1281,32 +1281,96 @@
  %7B%0A            %0A
+            System.out.println(System.getProperty(%22user.dir%22));%0A
             Stri
@@ -1798,47 +1798,31 @@
     
-AWS_
 CONF
-IG
 .load(
-AWS.class.getResourceAs
+new FileInput
 Stre
@@ -2023,16 +2023,21 @@
       %7D%0A
+    %7D
 %0A       
@@ -2041,301 +2041,342 @@
     
-String awsKey = AWS_CONFIG.getProperty(%22aws.key%22);%0A        String secretKey = AWS_CONFIG.getProperty(%22aws.secret%22);%0A        
+%0A    //interface to access cloud.%0A    public static AWS access()%7B%0A        return INSTANCE;
 %0A    
+%7D%0A
     
-AWS_CREDENTIALS = new BasicAWSCredentials(awsKey,secretKey);%0A    %7D%0A    %0A    //interface to access cloud.%0A    public static AWS access()%7B%0A        return INSTANCE
+public AWSAutoScaling as()%7B%0A        return (AWSAutoScaling)get(BlockName.AutoScaling);%0A    %7D%0A    public AWSEC2 ec2()%7B%0A        return (AWSEC2)get(BlockName.EC2);%0A    %7D%0A    public AWSELB elb()%7B%0A        return (AWSELB)get(BlockName.ELB)
 ;%0A  
@@ -2376,32 +2376,37 @@
 LB);%0A    %7D%0A    %0A
+    %0A
     private fina
@@ -3067,26 +3067,20 @@
 yList%3C%3E(
-AWS_
 CONF
-IG
 .keySet(
@@ -3207,18 +3207,12 @@
 ng, 
-AWS_
 CONF
-IG
 .get
@@ -3641,258 +3641,8 @@
    %0A
-    public AWSAutoScaling as()%7B%0A        return (AWSAutoScaling)get(BlockName.AutoScaling);%0A    %7D%0A    public AWSEC2 ec2()%7B%0A        return (AWSEC2)get(BlockName.EC2);%0A    %7D%0A    public AWSELB elb()%7B%0A        return (AWSELB)get(BlockName.ELB);%0A    %7D%0A    %0A
     

modify the way to read aws client config to use FileInputStream.