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