1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati;
47
48 import java.io.FileNotFoundException;
49 import java.io.IOException;
50 import java.util.List;
51 import java.util.Properties;
52 import java.util.Vector;
53
54 import org.melati.login.AccessHandler;
55 import org.melati.poem.PoemLocale;
56 import org.melati.poem.util.EnumUtils;
57 import org.melati.servlet.FormDataAdaptorFactory;
58 import org.melati.template.ClassNameTempletLoader;
59 import org.melati.template.ServletTemplateEngine;
60 import org.melati.template.SimpleDateAdaptor;
61 import org.melati.template.TemplateEngine;
62 import org.melati.template.TempletLoader;
63 import org.melati.template.YMDDateAdaptor;
64 import org.melati.template.YMDHMSTimestampAdaptor;
65 import org.melati.util.ConfigException;
66 import org.melati.util.HttpHeader;
67 import org.melati.util.PropertiesUtils;
68
69
70
71
72
73 public class MelatiConfig {
74
75 private Properties configuration = null;
76
77 private String propertiesName = "org.melati.MelatiConfig";
78
79 private AccessHandler accessHandler = null;
80 private FormDataAdaptorFactory fdaFactory = null;
81 private TempletLoader templetLoader = null;
82 private TemplateEngine templateEngine = null;
83 private static PoemLocale poemLocale = null;
84 private Vector preferredCharsets = null;
85 private String javascriptLibraryURL = null;
86 private String staticURL = null;
87 private String templatePath = null;
88 private static String loginPageServletClassName = "org.melati.login.Login";
89 private static String logoutPageServletClassName = "org.melati.login.Logout";
90
91 private static String realPath = null;
92
93
94
95
96
97 public MelatiConfig() {
98 try {
99 configuration =
100 PropertiesUtils.fromResource(getClass(), propertiesName + ".properties");
101 }
102 catch (FileNotFoundException e) {
103 configuration = new Properties();
104
105
106 }
107 catch (IOException e) {
108 throw new ConfigException("The file " + propertiesName + ".properties" +
109 " could not be read." +
110 " Full Error: " + e.toString());
111 }
112 init(propertiesName);
113 }
114
115
116
117
118
119
120
121
122 public MelatiConfig(String propertiesName) {
123 this.propertiesName = propertiesName;
124 try {
125 configuration =
126 PropertiesUtils.fromResource(getClass(), propertiesName + ".properties");
127 }
128 catch (FileNotFoundException e) {
129 throw new ConfigException("The file " + propertiesName + "properties" +
130 " could not be found." +
131 " Is it in your CLASSPATH? Full Error: " +
132 e.toString());
133 }
134 catch (IOException e) {
135 throw new ConfigException("The file " + propertiesName + ".properties" +
136 " could not be read." +
137 " Full Error: " + e.toString());
138 }
139 init(propertiesName);
140 }
141
142
143
144
145
146 public MelatiConfig(Properties properties) {
147 configuration = properties;
148 init(propertiesName);
149 }
150
151 void init(String propertiesNameIn) {
152 this.propertiesName = propertiesNameIn;
153 String pref = propertiesName + ".";
154
155 String accessHandlerProp = pref + "accessHandler";
156 String fdaFactoryProp = pref + "formDataAdaptorFactory";
157 String templetLoaderProp = pref + "templetLoader";
158 String templateEngineProp = pref + "templateEngine";
159 String templatePathProp = pref + "templatePath";
160 String javascriptLibraryURLProp = pref + "javascriptLibraryURL";
161 String staticURLProp = pref + "staticURL";
162 String melatiLocaleProp = pref + "locale";
163 String preferredCharsetsProp = pref + "preferredCharsets";
164 String loginPageServletClassNameProp = pref + "loginPageServletClassName";
165 String logoutPageServletClassNameProp = pref + "logoutPageServletClassName";
166
167 try {
168 setAccessHandler((AccessHandler)PropertiesUtils.
169 instanceOfNamedClass(
170 configuration,
171 accessHandlerProp,
172 "org.melati.login.AccessHandler",
173 "org.melati.login.OpenAccessHandler"));
174
175 setFdaFactory((FormDataAdaptorFactory)PropertiesUtils.
176 instanceOfNamedClass(
177 configuration,
178 fdaFactoryProp,
179 "org.melati.servlet.FormDataAdaptorFactory",
180 "org.melati.servlet.MemoryFormDataAdaptorFactory"));
181
182 String templetLoaderClassName = (String)configuration.get(templetLoaderProp);
183 if(templetLoaderClassName == null ||
184 templetLoaderClassName.equals("org.melati.template.ClassNameTempletLoader")) {
185 setTempletLoader(ClassNameTempletLoader.getInstance());
186 } else
187 setTempletLoader((TempletLoader)PropertiesUtils.
188 instanceOfNamedClass(
189 configuration,
190 templetLoaderProp,
191 "org.melati.template.TempletLoader",
192 "org.melati.template.ClassNameTempletLoader"));
193
194 setTemplateEngine((TemplateEngine)PropertiesUtils.
195 instanceOfNamedClass(
196 configuration,
197 templateEngineProp,
198 "org.melati.template.TemplateEngine",
199 "org.melati.template.NoTemplateEngine"));
200
201 String languageTag = PropertiesUtils.getOrDefault(configuration,
202 melatiLocaleProp,
203 "en-gb");
204
205 setPoemiLocale(PoemLocale.fromLanguageTag(languageTag));
206 if (poemLocale == null)
207 throw new ConfigException(languageTag +
208 " is not a valid language tag for " +
209 melatiLocaleProp);
210
211
212
213
214
215 setPreferredCharsets(
216 EnumUtils.vectorOf(
217 new HttpHeader(PropertiesUtils.getOrDefault(
218 configuration,
219 preferredCharsetsProp,
220 "ISO-8859-1, UTF-8, UTF-16")).wordIterator()));
221
222 setJavascriptLibraryURL(PropertiesUtils.getOrDefault(
223 configuration,
224 javascriptLibraryURLProp,
225 "/melati-static/admin/"));
226
227 setStaticURL(PropertiesUtils.getOrDefault(
228 configuration,
229 staticURLProp,
230 "/melati-static/"
231 ));
232
233 setTemplatePath(PropertiesUtils.getOrDefault(configuration,
234 templatePathProp, "."));
235
236 setLoginPageServletClassName(PropertiesUtils.getOrDefault(configuration,
237 loginPageServletClassNameProp, loginPageServletClassName));
238
239 setLogoutPageServletClassName(PropertiesUtils.getOrDefault(configuration,
240 logoutPageServletClassNameProp, logoutPageServletClassName));
241 }
242 catch (Exception e) {
243 throw new ConfigException("Melati could not be configured because: " +
244 e.toString(), e);
245 }
246
247 }
248
249
250
251
252 public ServletTemplateEngine getServletTemplateEngine() {
253 return (ServletTemplateEngine)templateEngine;
254 }
255
256
257
258
259 public TemplateEngine getTemplateEngine() {
260 return templateEngine;
261 }
262
263
264
265
266
267
268
269 public void setTemplateEngine(TemplateEngine templateEngine) {
270 this.templateEngine = templateEngine;
271 }
272
273
274
275
276 public AccessHandler getAccessHandler() {
277 return accessHandler;
278 }
279
280
281
282
283
284
285
286 public void setAccessHandler(AccessHandler accessHandler) {
287 this.accessHandler = accessHandler;
288 }
289
290
291
292
293 public TempletLoader getTempletLoader() {
294 return templetLoader;
295 }
296
297
298
299
300
301
302
303 public void setTempletLoader(TempletLoader templetLoader) {
304 this.templetLoader = templetLoader;
305 }
306
307
308
309
310 public FormDataAdaptorFactory getFormDataAdaptorFactory() {
311 return fdaFactory;
312 }
313
314
315
316
317
318
319
320 public void setFormDataAdaptorFactory(FormDataAdaptorFactory fdaf) {
321 fdaFactory = fdaf;
322 }
323
324
325
326
327 public String getJavascriptLibraryURL() {
328 return javascriptLibraryURL;
329 }
330
331
332
333
334
335
336
337 public void setJavascriptLibraryURL(String url) {
338 this.javascriptLibraryURL = url;
339 }
340
341
342
343
344
345
346 public String getStaticURL() {
347 return staticURL;
348 }
349
350
351
352
353
354
355
356 public void setStaticURL(String url) {
357 this.staticURL = url;
358 }
359
360
361
362
363 public String getTemplatePath() {
364 return templatePath;
365 }
366
367
368
369
370
371 public void setTemplatePath(String templatePath) {
372 this.templatePath = templatePath;
373 }
374
375
376
377
378 public static String getLogoutPageServletClassName() {
379 return logoutPageServletClassName;
380 }
381
382
383
384
385
386 public static void setLogoutPageServletClassName(
387 String logoutPageServletClassName) {
388 MelatiConfig.logoutPageServletClassName = logoutPageServletClassName;
389 }
390
391
392
393
394 public static String getLoginPageServletClassName() {
395 return loginPageServletClassName;
396 }
397
398
399
400
401
402 public static void setLoginPageServletClassName(
403 String loginPageServletClassName) {
404 MelatiConfig.loginPageServletClassName = loginPageServletClassName;
405 }
406
407
408
409
410 public static PoemLocale getPoemLocale() {
411 return poemLocale;
412 }
413
414
415
416
417
418 public void setPoemiLocale(PoemLocale poemLocale) {
419 MelatiConfig.poemLocale = poemLocale;
420 }
421
422
423
424
425
426
427
428 public List getPreferredCharsets() {
429 return preferredCharsets;
430 }
431
432
433
434
435
436 public void setPreferredCharsets(Vector preferredCharsets) {
437 this.preferredCharsets = preferredCharsets;
438 }
439
440
441
442
443 public FormDataAdaptorFactory getFdaFactory() {
444 return fdaFactory;
445 }
446
447
448
449
450
451 public void setFdaFactory(FormDataAdaptorFactory fdaFactory) {
452 this.fdaFactory = fdaFactory;
453 }
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468 public static YMDDateAdaptor getYMDDateAdaptor() {
469 return YMDDateAdaptor.it;
470 }
471
472
473
474
475
476
477
478
479
480
481 public static YMDHMSTimestampAdaptor getYMDHMSTimestampAdaptor() {
482 return YMDHMSTimestampAdaptor.getIt();
483 }
484
485
486
487
488
489
490 public static SimpleDateAdaptor getSimpleDateAdaptor() {
491 return SimpleDateAdaptor.it;
492 }
493
494
495
496
497 public void setRealPath(String realPathP) {
498 realPath = realPathP;
499 }
500
501
502
503 public String getRealPath() {
504 if (realPath == null) throw new NullPointerException();
505 return realPath;
506 }
507
508 }