From 9c9c17ad5ef95d3b37718c88edf99c9b1f6dfd98 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Mon, 6 Jan 2014 01:29:41 -0600 Subject: [PATCH] Implemented fixes that apply to resource loading in the JFX Jar. Should now be launchable, configure loggers correctly, and be able to load embedded files. Added a FileLoader helper class to manage loading resources. Is not thread safe currently, but should be made so. --- src/main/java/com/sothr/imagetools/App.java | 45 ++++++++++++------- .../com/sothr/imagetools/util/FileLoader.java | 27 +++++++++++ .../ui/controller/AppController.scala | 8 ++-- 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/sothr/imagetools/util/FileLoader.java diff --git a/src/main/java/com/sothr/imagetools/App.java b/src/main/java/com/sothr/imagetools/App.java index 5c56893..28c7f98 100644 --- a/src/main/java/com/sothr/imagetools/App.java +++ b/src/main/java/com/sothr/imagetools/App.java @@ -1,7 +1,7 @@ package com.sothr.imagetools; import com.sothr.imagetools.errors.ImageToolsException; -import com.sothr.imagetools.ui.controller.AppController; +import com.sothr.imagetools.util.FileLoader; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -13,6 +13,8 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Properties; /** @@ -22,16 +24,27 @@ import java.util.Properties; public class App extends Application { - private static Logger logger = LoggerFactory.getLogger(App.class); + private static Logger logger; private static final String MAINGUI_FXML = "fxml/mainapp/MainApp.fxml"; public static void main( String[] args ) { + configLogging("./log4j.properties"); + try { + //try to run the UI + launch(args); + } catch (Exception ex) { + logger.error("A fatal error has occurred: ", ex); + //show popup about the error to the user then exit + } + } + + private static void configLogging(String propertiesLocation) { //Logging Config - File file = new File("log4j.properties"); + File file = new File(propertiesLocation); if (file.exists()) { - PropertyConfigurator.configure("log4j.properties"); + PropertyConfigurator.configure(propertiesLocation); } else { //Simple error logging configuration Properties defaultProps = new Properties(); @@ -45,22 +58,18 @@ public class App extends Application defaultProps.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n"); PropertyConfigurator.configure(defaultProps); } + logger = LoggerFactory.getLogger(App.class); + } - logger.info("Image-Tools is starting"); - - try { - //try to run the UI - launch(args); - } catch (Exception ex) { - logger.error("A fatal error has occurred: ", ex); - //show popup about the error to the user then exit - } - - logger.info("Image-Tools is shutting down"); + @Override + public void init() throws Exception{ + configLogging("./log4j.properties"); + super.init(); } @Override public void start(Stage primaryStage) throws Exception { + logger.info("Image-Tools is starting"); logger.info(String.format("Launching GUI with FXML file %s", MAINGUI_FXML)); ClassLoader cl = this.getClass().getClassLoader(); try { @@ -85,4 +94,10 @@ public class App extends Application throw ite; } } + + @Override + public void stop() throws Exception { + logger.info("Image-Tools is shutting down"); + super.stop(); + } } diff --git a/src/main/java/com/sothr/imagetools/util/FileLoader.java b/src/main/java/com/sothr/imagetools/util/FileLoader.java new file mode 100644 index 0000000..cf8e2cc --- /dev/null +++ b/src/main/java/com/sothr/imagetools/util/FileLoader.java @@ -0,0 +1,27 @@ +package com.sothr.imagetools.util; + +import java.io.InputStream; +import java.net.URL; + +/** + * Created by drew on 1/5/14. + */ +public class FileLoader { + + private static final FileLoader instance = new FileLoader(); + + private FileLoader() {} + + public static FileLoader get() { + return instance; + } + + public URL getResource(String location) { + return Thread.currentThread().getContextClassLoader().getResource(location); + } + + public InputStream getResourceStream(String location) { + return Thread.currentThread().getContextClassLoader().getResourceAsStream(location); + } + +} \ No newline at end of file diff --git a/src/main/scala/com/sothr/imagetools/ui/controller/AppController.scala b/src/main/scala/com/sothr/imagetools/ui/controller/AppController.scala index cb3b3a9..33aad01 100644 --- a/src/main/scala/com/sothr/imagetools/ui/controller/AppController.scala +++ b/src/main/scala/com/sothr/imagetools/ui/controller/AppController.scala @@ -9,8 +9,9 @@ import javafx.scene.Scene import javafx.scene.Group import javafx.scene.text.Text import java.io.{IOException, File} -import java.net.URL import java.util.Scanner +import com.sothr.imagetools.util.FileLoader +import java.net.URL /** * Created by drew on 12/31/13. @@ -29,10 +30,7 @@ class AppController { logger.debug("Displaying about screen") var aboutMessage = "Simple About Message" try { - val cl = this.getClass.getClassLoader - val url = cl.getResource("documents/about") - val uri = url.toURI - aboutMessage = new Scanner(new File(uri)).useDelimiter("\\A").next() + aboutMessage = new Scanner(FileLoader.get().getResourceStream("documents/about")).useDelimiter("\\A").next() } catch { case ioe:IOException => logger.error("Unable to read about file")