You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package com.sothr.imagetools.engine;
  2. import akka.actor.ActorSystem;
  3. import ch.qos.logback.classic.Logger;
  4. import ch.qos.logback.classic.LoggerContext;
  5. import ch.qos.logback.classic.joran.JoranConfigurator;
  6. import ch.qos.logback.core.joran.spi.JoranException;
  7. import ch.qos.logback.core.util.StatusPrinter;
  8. import com.sothr.imagetools.engine.dao.HibernateUtil;
  9. import com.sothr.imagetools.engine.util.PropertiesService;
  10. import com.sothr.imagetools.engine.util.ResourceLoader;
  11. import javafx.fxml.FXMLLoader;
  12. import javafx.stage.Stage;
  13. import net.sf.ehcache.CacheManager;
  14. import org.slf4j.LoggerFactory;
  15. import java.io.File;
  16. public class AppConfig {
  17. private static Logger logger;
  18. public static CacheManager cacheManager;
  19. // Logging defaults
  20. private static final String LOGSETTINGSFILE = "./logback.xml";
  21. private static Boolean configuredLogging = false;
  22. // Properties defaults
  23. private static final String DEFAULTPROPERTIESFILE = "application.conf";
  24. private static final String USERPROPERTIESFILE = "user.conf";
  25. private static Boolean loadedProperties = false;
  26. // Cache defaults
  27. private static Boolean configuredCache = false;
  28. // General Akka Actor System
  29. private static final ActorSystem appSystem = ActorSystem.create("ITActorSystem");
  30. // The Main App
  31. private static Stage primaryStage = null;
  32. public static Stage getPrimaryStage() {
  33. return primaryStage;
  34. }
  35. public static void setPrimaryStage(Stage newPrimaryStage) {
  36. primaryStage = newPrimaryStage;
  37. }
  38. public static FXMLLoader fxmlLoader = null;
  39. public static FXMLLoader getFxmlLoader() { return fxmlLoader; }
  40. public static void setFxmlLoader(FXMLLoader loader) { fxmlLoader = loader; }
  41. public static ActorSystem getAppActorSystem() {
  42. return appSystem;
  43. }
  44. public static void configureApp() {
  45. logger = (Logger) LoggerFactory.getLogger(AppConfig.class);
  46. loadProperties();
  47. configLogging();
  48. configCache();
  49. }
  50. private static void configLogging(String location) {
  51. //Logging Config
  52. //remove previous configuration if it exists
  53. Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  54. LoggerContext context = rootLogger.getLoggerContext();
  55. context.reset();
  56. File file = new File(location);
  57. Boolean fromFile = false;
  58. if (file.exists()) {
  59. fromFile = true;
  60. try {
  61. JoranConfigurator configurator = new JoranConfigurator();
  62. configurator.setContext(context);
  63. // Call context.reset() to clear any previous configuration, e.g. default
  64. // configuration. For multi-step configuration, omit calling context.reset().
  65. context.reset();
  66. configurator.doConfigure(location);
  67. } catch (JoranException je) {
  68. // StatusPrinter will handle this
  69. }
  70. StatusPrinter.printInCaseOfErrorsOrWarnings(context);
  71. } else {
  72. try {
  73. JoranConfigurator configurator = new JoranConfigurator();
  74. configurator.setContext(context);
  75. // Call context.reset() to clear any previous configuration, e.g. default
  76. // configuration. For multi-step configuration, omit calling context.reset().
  77. context.reset();
  78. configurator.doConfigure(ResourceLoader.get().getResourceStream("logback-minimum-config.xml"));
  79. } catch (JoranException je) {
  80. // StatusPrinter will handle this
  81. }
  82. StatusPrinter.printInCaseOfErrorsOrWarnings(context);
  83. }
  84. String message = fromFile ? "From File" : "From Defaults";
  85. logger.info(String.format("Configured Logger %s", message));
  86. logger.info(String.format("Detected Version: %s of Image Tools", PropertiesService.getVersion().toString()));
  87. logger.info(String.format("Running on %s, %s, %s",PropertiesService.OS(), PropertiesService.OS_VERSION(),PropertiesService.OS_ARCH()));
  88. }
  89. //Only configure logging from the default file once
  90. private static void configLogging() {
  91. if (!configuredLogging) {
  92. configLogging(LOGSETTINGSFILE);
  93. configuredLogging = true;
  94. logger.info("Configured logging");
  95. }
  96. }
  97. private static void loadProperties() {
  98. if (!loadedProperties) {
  99. File file = new File(USERPROPERTIESFILE);
  100. if (file.exists()) {
  101. PropertiesService.loadProperties(DEFAULTPROPERTIESFILE, USERPROPERTIESFILE);
  102. } else {
  103. PropertiesService.loadProperties(DEFAULTPROPERTIESFILE, null);
  104. }
  105. loadedProperties = true;
  106. logger.info("Loaded Properties");
  107. }
  108. }
  109. private static void configCache() {
  110. if (!configuredCache) {
  111. cacheManager = CacheManager.newInstance();
  112. configuredCache = true;
  113. logger.info("Configured EHCache");
  114. }
  115. }
  116. public static void shutdown() {
  117. saveProperties();
  118. HibernateUtil.getSessionFactory().close();
  119. }
  120. private static void saveProperties() {
  121. PropertiesService.saveConf(USERPROPERTIESFILE);
  122. logger.debug("Saved properties");
  123. }
  124. }