View Javadoc

1   /**
2    * Copyright (c) 2015 Renato Del Gaudio
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in all 
12   * copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  package com.renatodelgaudio.awsupdate;
23  import static org.apache.commons.lang.StringUtils.isBlank;
24  import static org.apache.commons.lang.StringUtils.trim;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.util.Properties;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import com.amazonaws.auth.PropertiesCredentials;
36  import com.amazonaws.services.route53.AmazonRoute53;
37  import com.amazonaws.services.route53.AmazonRoute53Client;
38  
39  public class EnvUtil {
40  	
41  	/**
42  	 * This is the expected property passed to Java on boot
43  	 * <br> e.g. java -DINSTALL_DIR= 
44  	 */
45  	public static final String PROP_INSTALL_DIR = "INSTALL_DIR";
46  	public static final String PROP_AWS_FILE_NAME = "aws.properties";
47  	
48  	
49  	private final static Logger log = LoggerFactory.getLogger(EnvUtil.class);
50  
51  	private EnvUtil(){}
52  	/*
53  	 * It returns the installation path ending with either / or \
54  	 */
55  	public static String INSTALL_DIR(){
56  		String root = System.getProperty(PROP_INSTALL_DIR);
57  		
58  		if (isBlank(root)){
59  			log.error("Environment not initiazed properly. Please make sure Java is started with a not empty value for -D"+PROP_INSTALL_DIR);
60  			throw new ConfigurationException("-D"+PROP_INSTALL_DIR+" is missing or empty. Program aborted");
61  		}
62  		String trim = trim(root);
63  		if (!StringUtils.endsWith(trim, "/") && !StringUtils.endsWith(trim, "\\"))
64  			trim += File.separator;
65  		
66  		return trim;
67  	}
68  	
69  	public static String getAwsFilePath(){
70  		return INSTALL_DIR() + PROP_AWS_FILE_NAME;
71  	}
72  	
73  	public static AmazonRoute53 buildRoute53(File aws){
74  		AmazonRoute53 r53 = null;
75  		try {
76  			r53 = new AmazonRoute53Client(new PropertiesCredentials(aws));
77  		} catch (Exception e) {
78  			log.error("Cannot build AmazonRouter53Client",e);
79  			throw new ConfigurationException(e.getMessage());
80  		} 
81  		return r53;
82  	}
83  	
84  	public static Properties getConfigAsProperty(){
85  	    String awsPath = getAwsFilePath();
86  	    File file = new File(awsPath);
87  
88  	    FileInputStream stream =null;
89  
90  	    try {
91  		stream = new FileInputStream(file);
92  		Properties ret = new Properties();
93  		ret.load(stream);
94  		return ret;
95  	    } 
96  	    catch (Exception e) {
97  		throw new ConfigurationException(e.getMessage());
98  	    }
99  	    finally {
100 		try {
101 		    stream.close();
102 		} catch (IOException ignored){}
103 	    }
104 	}
105 }