Skip to content

Commit be20e26

Browse files
committed
Adding support for environment variables
1 parent 7b06b50 commit be20e26

File tree

1 file changed

+44
-67
lines changed

1 file changed

+44
-67
lines changed

src/main/java/no/javazone/cake/redux/Configuration.java

Lines changed: 44 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import java.io.FileInputStream;
44
import java.io.IOException;
55
import java.time.LocalDate;
6-
import java.time.LocalDateTime;
76
import java.util.HashMap;
87
import java.util.Map;
98
import java.util.Optional;
109

1110
public class Configuration {
1211

1312
private Map<String,String> properties = null;
14-
private static Configuration instance = new Configuration();
13+
private static final Configuration instance = new Configuration();
1514

1615
private Configuration() {
1716

@@ -28,18 +27,24 @@ private static String readConfigFile(String filename) {
2827
}
2928
}
3029

31-
private static String getProperty(String key) {
30+
public static void setProps(Map<String,String> props) {
31+
instance.properties = props;
32+
}
33+
34+
private static String getProperty(String prop, String env) {
3235
if (instance.properties == null) {
3336
instance.loadProps();
3437
if (instance.properties == null) {
35-
throw new IllegalStateException("Properties not initalized getting " +key);
38+
throw new IllegalStateException("Properties not initalized getting " +prop);
3639
}
3740
}
38-
return instance.properties.get(key);
41+
return instance.properties.getOrDefault(prop, System.getenv(env));
3942

4043
}
4144

42-
45+
private static String readConf(String prop, String env, String defaultValue) {
46+
return Optional.ofNullable(getProperty(prop, env)).orElse(defaultValue);
47+
}
4348

4449
private synchronized void loadProps() {
4550
Map<String,String> readProps = new HashMap<>();
@@ -58,169 +63,141 @@ private synchronized void loadProps() {
5863
}
5964

6065
public static String getEmsUser() {
61-
return getProperty("emsUser");
66+
return getProperty("emsUser", "EMS_USER");
6267
}
6368

6469
public static String getEmsPassword() {
65-
return getProperty("emsPassword");
70+
return getProperty("emsPassword", "EMS_PASSWORD");
6671
}
6772

6873
public static String getGoogleClientId() {
69-
return getProperty("googleClientId");
74+
return getProperty("googleClientId", "GOOGLE_CLIENT_ID");
7075
}
7176

7277
public static String getGoogleClientSecret() {
73-
return getProperty("googleClientSecret");
78+
return getProperty("googleClientSecret", "GOOGLE_CLIENT_SECRET");
7479
}
7580

7681
public static String getGoogleRedirectUrl() {
77-
return getProperty("googleRedirectUrl");
82+
return getProperty("googleRedirectUrl", "GOOGLE_REDIRECT_URL");
7883
}
7984

8085
public static String getAutorizedUsers() {
81-
String authorizedUsers = getProperty("authorizedUsers");
86+
String authorizedUsers = getProperty("authorizedUsers", "AUTHORIZED_USERS");
8287
if (authorizedUsers == null) {
8388
return "";
8489
}
8590
return authorizedUsers;
8691
}
8792

8893
public static String autorizedUserFile() {
89-
return getProperty("autorizedUserFile");
94+
return getProperty("autorizedUserFile", "AUTORIZED_USER_FILE");
9095
}
9196

9297
public static String fullUsers() {
93-
return getProperty("fullUsers");
98+
return getProperty("fullUsers", "FULL_USERS");
9499
}
95100

96101
public static boolean noAuthMode() {
97-
return "true".equals(getProperty("noAuthMode"));
102+
return "true".equals(getProperty("noAuthMode", "NO_AUTH_MODE"));
98103
}
99104

100105
public static String emsEventLocation() {
101-
return getProperty("emsEventLocation");
106+
return getProperty("emsEventLocation", "EMS_EVENT_LOCATION");
102107
}
103108

104109
public static String submititLocation() {
105-
return getProperty("submititLocation");
110+
return getProperty("submititLocation", "SUBMITIT_LOCATION");
106111
}
107112

108113
public static Integer serverPort() {
109-
String serverPortStr = getProperty("serverPort");
114+
String serverPortStr = getProperty("serverPort", "SERVER_PORT");
110115
if (serverPortStr == null || serverPortStr.isEmpty()) {
111116
return null;
112117
}
113118
return Integer.parseInt(serverPortStr);
114119
}
115120

116121
public static String mailSenderImplementation() {
117-
return readConf("mailSenderImplementation","smtp");
118-
}
119-
120-
public static String smtpServer() {
121-
return getProperty("smthost");
122-
}
123-
124-
public static int smtpPort() {
125-
return Integer.parseInt(getProperty("smtpport"));
126-
}
127-
128-
public static boolean useMailSSL() {
129-
return "true".equals(getProperty("mailSsl"));
130-
}
131-
132-
public static String mailUser() {
133-
return getProperty("mailUser");
134-
}
135-
136-
public static String mailPassword() {
137-
return getProperty("mailPassword");
122+
return readConf("mailSenderImplementation", "MAIL_SENDER_IMPLEMENTATION","smtp");
138123
}
139124

140125
public static String cakeLocation() {
141-
return getProperty("cakeLocation");
142-
}
143-
144-
private static String readConf(String prop,String defaultValue) {
145-
return Optional.ofNullable(getProperty(prop)).orElse(defaultValue);
126+
return getProperty("cakeLocation", "CAKE_LOCATION");
146127
}
147128

148129
public static boolean whydaSupported() {
149-
return "true".equals(readConf("supportWhyda","false"));
130+
return "true".equals(readConf("supportWhyda", "SUPPORT_WHYDA","false"));
150131
}
151132

152133
public static String logonRedirectUrl() {
153-
return readConf("logonRedirectUrl", "http://localhost:9997/sso/login?redirectURI=http://localhost:8088/admin/");
134+
return readConf("logonRedirectUrl", "LOGON_REDIRECT_URL", "http://localhost:9997/sso/login?redirectURI=http://localhost:8088/admin/");
154135
}
155136

156137
public static String tokenServiceUrl() {
157-
return readConf("tokenServiceUrl", "http://localhost:9998/tokenservice");
138+
return readConf("tokenServiceUrl", "TOKEN_SERIVCE_URL","http://localhost:9998/tokenservice");
158139
}
159140

160141
public static String applicationId() {
161-
return readConf("applicationId", "99");
142+
return readConf("applicationId", "APPLICATION_ID", "99");
162143
}
163144

164145
public static String feedbackStoreFilename() {
165-
return readConf("feedbackStoreFilename",null);
146+
return readConf("feedbackStoreFilename", "FEEDBACK_STORE_FILENAME",null);
166147
}
167148

168-
public static String applicationSecret() { return readConf("applicationSecret", "33879936R6Jr47D4Hj5R6p9qT");}
169-
170-
public static void setProps(Map<String,String> props) {
171-
instance.properties = props;
172-
}
149+
public static String applicationSecret() { return readConf("applicationSecret", "APPLICATION_SECRET", "33879936R6Jr47D4Hj5R6p9qT");}
173150

174151
public static long emailSleepTime() {
175-
return Long.parseLong(readConf("emailSleepTime","5000"));
152+
return Long.parseLong(readConf("emailSleepTime", "EMAIL_SLEEP_TIME","5000"));
176153
}
177154

178155
public static String sleepingPillBaseLocation() {
179-
return readConf("sleepingPillBaseLocation","http://localhost:8082");
156+
return readConf("sleepingPillBaseLocation", "SLEEPINGPILL_BASE_LOCATION","http://localhost:8082");
180157
}
181158

182159
public static String sleepingpillUser() {
183-
return readConf("sleepingpillUser",null);
160+
return readConf("sleepingpillUser", "SLEEPINGPILL_USER",null);
184161
}
185162

186163
public static String sleepingpillPassword() {
187-
return readConf("sleepingpillPassword",null);
164+
return readConf("sleepingpillPassword", "SLEEPINGPILL_PASSWORD",null);
188165
}
189166

190167
public static String feedbackDaoImpl() {
191-
return readConf("feedbackDaoImpl","sleepingpill");
168+
return readConf("feedbackDaoImpl", "FEEDBACK_DAO_IMPL","sleepingpill");
192169
}
193170

194171
public static String videoAdminPassword() {
195-
return readConf("videoAdminPassword","dummy:bingo");
172+
return readConf("videoAdminPassword", "VIDEO_ADMIN_PASSWORD","dummy:bingo");
196173
}
197174

198175
public static String videoAdminConference() {
199-
return readConf("videoAdminConference","30d5c2f1cb214fc8b0649a44fdf3b4bf");
176+
return readConf("videoAdminConference", "VIDEO_ADMIN_CONFERENCE","30d5c2f1cb214fc8b0649a44fdf3b4bf");
200177
}
201178

202179
public static String sendGridKey() {
203-
return readConf("sendGridKey",null);
180+
return readConf("sendGridKey", "SENDGRID_KEY",null);
204181
}
205182

206183
public static String slackAppId() {
207-
return readConf("slackAppId",null);
184+
return readConf("slackAppId", "SLACK_APP_ID", null);
208185
}
209186

210187
public static String slackClientSecret() {
211-
return readConf("slackClientSecret",null);
188+
return readConf("slackClientSecret", "SLACK_CLIENT_SECRET",null);
212189
}
213190

214191
public static String slackAuthChannel() {
215-
return readConf("slackAuthChannel",null);
192+
return readConf("slackAuthChannel", "SLACK_AUTH_CHANNEL",null);
216193
}
217194

218195
public static String slackApiToken() {
219-
return readConf("slackApiToken",null);
196+
return readConf("slackApiToken", "SLACK_API_TOKEN",null);
220197
}
221198

222199
public static LocalDate conferenceWednesday() {
223-
return LocalDate.parse(readConf("conferenceWednesday","2019-09-11"));
200+
return LocalDate.parse(readConf("conferenceWednesday", "CONFERENCE_WEDNESDAY","2019-09-11"));
224201
}
225202

226203

0 commit comments

Comments
 (0)