Skip to content

Commit

Permalink
Read the Config.json from the jar's resource folder, not /etc/JRobo/C…
Browse files Browse the repository at this point in the history
…onfig.json
  • Loading branch information
BullShark committed Feb 9, 2022
1 parent 6773af7 commit 1e6dd68
Showing 1 changed file with 14 additions and 45 deletions.
59 changes: 14 additions & 45 deletions src/main/java/expectusafterlun/ch/jrobo/FileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.gson.JsonSyntaxException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.err;
Expand All @@ -31,7 +30,6 @@
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;

/**
* FileReader is used to supply the Config class with configuration read in from
Expand Down Expand Up @@ -119,59 +117,30 @@ protected Config getConfig() {
out.println(TermColors.info("System user directory: " + System.getProperty("user.dir")));

//Thread.dumpStack();
//String json = "";

/*
* Check if the Config file exists at /etc/JRobo/Config.json.
* And if it doesn't exist, then proceed to read it from resources in the jar.
/*
* The Config.json is bundled in the jar's resource folder.
*/
if (new File("/etc/JRobo/" + CONFIGFILE).exists()) {
System.out.println(TermColors.info("Reading Config from the jar as a resource stream:\t" + CONFIGFILE));

System.out.println(TermColors.info("Reading Config from:\t/etc/JRobo/" + CONFIGFILE));

try (FileInputStream fis = new FileInputStream("/etc/JRobo/" + CONFIGFILE); ) {
String json = IOUtils.toString(fis, "UTF-8");

System.out.println(TermColors.info("json: " + json));

Gson gson = new Gson();
config = gson.fromJson(json, Config.class);

ranOnce = true;

} catch (JsonSyntaxException | NullPointerException | IOException ex) {
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
System.exit(1);
try ( BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(CONFIGFILE)))) {

String json = "";
while (reader.ready()) {
json += reader.readLine();
}

/*
* File /etc/JRobo/Config.json does not exist.
* So we are using the Config.json bundled in the jar's resource folder instead.
*/
} else {
System.out.println(TermColors.info("json: " + json));

System.out.println(TermColors.info("Reading Config from the jar as a resource stream:\t" + CONFIGFILE));

try ( BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(CONFIGFILE)))) {
Gson gson = new Gson();
config = gson.fromJson(json, Config.class);

String json = "";
while (reader.ready()) {
json += reader.readLine();
}
ranOnce = true;

System.out.println(TermColors.info("json: " + json));

Gson gson = new Gson();
config = gson.fromJson(json, Config.class);

ranOnce = true;

} catch (JsonSyntaxException | NullPointerException | IOException ex) {
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
System.exit(1);
} catch (JsonSyntaxException | NullPointerException | IOException ex) {
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
System.exit(1);

}
}

// Verifiying important settings for connection
Expand Down

0 comments on commit 1e6dd68

Please sign in to comment.