1
0
mirror of https://github.com/russ-p/external-sql.git synced 2025-12-14 01:14:24 +00:00

fix FileNotFoundException when sql in jar

This commit is contained in:
2019-10-06 12:02:35 +03:00
parent 1eb4af13b4
commit 6e72ef07ab

View File

@@ -1,9 +1,10 @@
package com.github.russ_p.externalsql; package com.github.russ_p.externalsql;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -17,11 +18,11 @@ class SqlResolver {
private final Map<String, String> queries = new HashMap<>(); private final Map<String, String> queries = new HashMap<>();
public SqlResolver(String filename) throws IOException { public SqlResolver(String filename) throws IOException {
this(new File(resolvePath(filename))); this(resolvePath(filename));
} }
public SqlResolver(File file) throws IOException { public SqlResolver(InputStream is) throws IOException {
parseFile(file); parseFile(is);
} }
public Set<String> getQueryNames() { public Set<String> getQueryNames() {
@@ -41,10 +42,10 @@ class SqlResolver {
queries.put(name, query); queries.put(name, query);
} }
private void parseFile(File file) throws IOException { private void parseFile(InputStream is) throws IOException {
String queryName = ""; String queryName = "";
StringBuffer query = new StringBuffer(); StringBuffer query = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
String line = reader.readLine(); String line = reader.readLine();
while (line != null) { while (line != null) {
if (isSqlHeaderComment(line)) { if (isSqlHeaderComment(line)) {
@@ -84,11 +85,11 @@ class SqlResolver {
throw new IllegalStateException("No sql query name found in line " + line); throw new IllegalStateException("No sql query name found in line " + line);
} }
private static String resolvePath(String path) { private static InputStream resolvePath(String path) throws IOException {
if (path.startsWith("classpath:")) { if (path.startsWith("classpath:")) {
return SqlResolver.class.getClassLoader().getResource(path.substring(10)).getFile(); return SqlResolver.class.getClassLoader().getResourceAsStream(path.substring(10));
} }
return path; return new FileInputStream(path);
} }
} }