1
2
3
4
5
6 package pt.digitalis.dif.startup;
7
8 import java.io.InputStream;
9 import java.util.Properties;
10
11 import pt.digitalis.dif.dem.objects.messages.MessagesLocation;
12 import pt.digitalis.dif.utils.logging.DIFLogger;
13 import pt.digitalis.log.LogLevel;
14 import pt.digitalis.utils.ioc.IoCImplementations;
15 import pt.digitalis.utils.ioc.ModuleParser;
16
17
18
19
20
21
22
23
24
25
26
27
28 public class DIFStartupConfiguration {
29
30
31
32
33
34 static final public String CONFIGURATION_FILE = "dif2.properties";
35
36
37 static private Boolean demoMode;
38
39
40 static private Boolean developerMode;
41
42
43 static private IoCImplementations iocImplementation;
44
45
46 static private LogLevel logLevel;
47
48
49 static private MessagesLocation messagesLocation;
50
51
52 static private ModuleParser moduleParser;
53
54
55 static private Boolean printStackTrace = true;
56
57
58 static private Boolean testingMode;
59
60
61 static
62 {
63 Properties parameters = new Properties();
64 try
65 {
66
67
68
69
70
71
72 InputStream configsFile = Thread.currentThread().getContextClassLoader()
73 .getResourceAsStream(CONFIGURATION_FILE);
74
75 if (configsFile != null)
76 {
77
78 parameters.load(configsFile);
79
80 iocImplementation = IoCImplementations.getImplementationByName(loadProperty(parameters,
81 "iocimplementation", IoCImplementations.GUICE.toString()));
82
83 logLevel = LogLevel.getLevelByName(loadProperty(parameters, "loglevel", LogLevel.WARN.toString()));
84 developerMode = loadProperty(parameters, "developerMode", false);
85 demoMode = loadProperty(parameters, "demoMode", false);
86 testingMode = loadProperty(parameters, "testingMode", false);
87
88 moduleParser = ModuleParser.getParsingMethodByName(loadProperty(parameters, "moduleparser",
89 ModuleParser.FAST.toString()));
90 messagesLocation = MessagesLocation.getMessageLocationByName(loadProperty(parameters,
91 "messagelocation", MessagesLocation.MESSAGES_FOLDER.toString()));
92 printStackTrace = loadProperty(parameters, "printStackTrace", true);
93 }
94 else
95 setDefaultConfigurations();
96
97 }
98 catch (Exception e)
99 {
100 setDefaultConfigurations();
101 DIFLogger.getLogger().warn("dif2.properties unavailable! Reverting to defaults...");
102 }
103 }
104
105
106
107
108
109
110 public static Boolean getDemoMode()
111 {
112 return demoMode;
113 }
114
115
116
117
118
119
120 public static Boolean getDeveloperMode()
121 {
122 return developerMode;
123 }
124
125
126
127
128 static public IoCImplementations getIoCImplementation()
129 {
130 return iocImplementation;
131 }
132
133
134
135
136 static public LogLevel getLogLevel()
137 {
138 return logLevel;
139 }
140
141
142
143
144 static public MessagesLocation getMessagesLocation()
145 {
146 return messagesLocation;
147 }
148
149
150
151
152 static public ModuleParser getModuleParser()
153 {
154 return moduleParser;
155 }
156
157
158
159
160
161
162 public static Boolean getTestingMode()
163 {
164 return testingMode;
165 }
166
167
168
169
170
171
172 static public Boolean isPrintStackTrace()
173 {
174 return printStackTrace;
175 }
176
177
178
179
180
181 public static boolean isProductionMode()
182 {
183 return !getDemoMode() && !getDeveloperMode() && !getTestingMode();
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 static Boolean loadProperty(Properties prop, String key, boolean defaultValue)
198 {
199 String value = prop.getProperty(key);
200
201 if (value == null)
202 return defaultValue;
203 else
204 {
205 if (value.toLowerCase().equals("false") || value.toLowerCase().equals("f"))
206 return false;
207 else if (value.toLowerCase().equals("true") || value.toLowerCase().equals("t"))
208 return true;
209 else
210 throw new RuntimeException("Invalid configuration value for property '" + key
211 + "'! Unable to proceed...\nValid values are: 'true', 'T', 'false' and 'F'.");
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 static String loadProperty(Properties prop, String key, String defaultValue)
227 {
228 String value = prop.getProperty(key);
229
230 if (value == null)
231 return defaultValue;
232 else
233 return value;
234 }
235
236
237 static private void setDefaultConfigurations()
238 {
239 iocImplementation = IoCImplementations.GUICE;
240 logLevel = LogLevel.WARN;
241 developerMode = false;
242 demoMode = false;
243 testingMode = false;
244 moduleParser = ModuleParser.FAST;
245 messagesLocation = MessagesLocation.MESSAGES_FOLDER;
246 printStackTrace = true;
247 }
248
249
250
251
252
253
254
255 public static void setDemoMode(Boolean demoMode)
256 {
257 DIFStartupConfiguration.demoMode = demoMode;
258 }
259
260
261
262
263
264
265
266 public static void setDeveloperMode(Boolean developerMode)
267 {
268 DIFStartupConfiguration.developerMode = developerMode;
269 }
270
271
272
273
274
275
276
277 public static void setLogLevel(LogLevel logLevel)
278 {
279 DIFStartupConfiguration.logLevel = logLevel;
280 }
281
282
283
284
285
286
287
288 public static void setTestingMode(Boolean testingMode)
289 {
290 DIFStartupConfiguration.testingMode = testingMode;
291 }
292 }