1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
43
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
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 }