Skip to content

Commit

Permalink
Fix default config load crash when run as jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Mar 29, 2019
1 parent 8c5d860 commit 3f7af2f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/me/coley/j2h/config/Importer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package me.coley.j2h.config;

import me.coley.j2h.config.model.Configuration;
import org.apache.commons.io.IOUtils;

import javax.xml.bind.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.*;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -64,9 +66,18 @@ public static Configuration importFromText(String text) throws JAXBException {
*/
public static Configuration importDefault() throws JAXBException, IOException {
ClassLoader classloader = Importer.class.getClassLoader();
String uri = classloader.getResource(DEFAULT_CONF).toExternalForm();
Path path = Paths.get(URI.create(uri));
return importFromFile(path.toString());
String uriPath = classloader.getResource(DEFAULT_CONF).toExternalForm();
URI uri = URI.create(uriPath);
switch(uri.getScheme()) {
case "file":
Path path = Paths.get(uri);
return importFromFile(path.toString());
case "jar":
InputStream in = classloader.getResourceAsStream(DEFAULT_CONF);
byte[] data = IOUtils.toByteArray(in);
return importFromText(new String(data, UTF_8));
}
throw new IOException("Default configuration location unknown");
}

/**
Expand Down

0 comments on commit 3f7af2f

Please sign in to comment.