Browse Source

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.

master
Drew Short 11 years ago
parent
commit
9c9c17ad5e
  1. 43
      src/main/java/com/sothr/imagetools/App.java
  2. 27
      src/main/java/com/sothr/imagetools/util/FileLoader.java
  3. 8
      src/main/scala/com/sothr/imagetools/ui/controller/AppController.scala

43
src/main/java/com/sothr/imagetools/App.java

@ -1,7 +1,7 @@
package com.sothr.imagetools; package com.sothr.imagetools;
import com.sothr.imagetools.errors.ImageToolsException; 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.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
@ -13,6 +13,8 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties; import java.util.Properties;
/** /**
@ -22,16 +24,27 @@ import java.util.Properties;
public class App extends Application 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"; private static final String MAINGUI_FXML = "fxml/mainapp/MainApp.fxml";
public static void main( String[] args ) 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 //Logging Config
File file = new File("log4j.properties");
File file = new File(propertiesLocation);
if (file.exists()) { if (file.exists()) {
PropertyConfigurator.configure("log4j.properties");
PropertyConfigurator.configure(propertiesLocation);
} else { } else {
//Simple error logging configuration //Simple error logging configuration
Properties defaultProps = new Properties(); 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"); defaultProps.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n");
PropertyConfigurator.configure(defaultProps); PropertyConfigurator.configure(defaultProps);
} }
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 = LoggerFactory.getLogger(App.class);
} }
logger.info("Image-Tools is shutting down");
@Override
public void init() throws Exception{
configLogging("./log4j.properties");
super.init();
} }
@Override @Override
public void start(Stage primaryStage) throws Exception { 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)); logger.info(String.format("Launching GUI with FXML file %s", MAINGUI_FXML));
ClassLoader cl = this.getClass().getClassLoader(); ClassLoader cl = this.getClass().getClassLoader();
try { try {
@ -85,4 +94,10 @@ public class App extends Application
throw ite; throw ite;
} }
} }
@Override
public void stop() throws Exception {
logger.info("Image-Tools is shutting down");
super.stop();
}
} }

27
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);
}
}

8
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.Group
import javafx.scene.text.Text import javafx.scene.text.Text
import java.io.{IOException, File} import java.io.{IOException, File}
import java.net.URL
import java.util.Scanner import java.util.Scanner
import com.sothr.imagetools.util.FileLoader
import java.net.URL
/** /**
* Created by drew on 12/31/13. * Created by drew on 12/31/13.
@ -29,10 +30,7 @@ class AppController {
logger.debug("Displaying about screen") logger.debug("Displaying about screen")
var aboutMessage = "Simple About Message" var aboutMessage = "Simple About Message"
try { 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 { } catch {
case ioe:IOException => case ioe:IOException =>
logger.error("Unable to read about file") logger.error("Unable to read about file")

Loading…
Cancel
Save