[FIX] resource Uri finder
This commit is contained in:
parent
5a5e7de99d
commit
0067726c33
@ -14,12 +14,13 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class Uri {
|
||||
final static Logger LOGGER = LoggerFactory.getLogger(Uri.class);
|
||||
|
||||
private record LibraryElement(
|
||||
Class<?> klass,
|
||||
String basePath) {}
|
||||
@ -37,7 +38,8 @@ public class Uri {
|
||||
}
|
||||
|
||||
public static void addLibrary(final String libName, final Class<?> classHandle, String basePath) {
|
||||
LOGGER.trace("Add library reference: lib={} ==> {} base path={}", libName, classHandle.getCanonicalName(), basePath);
|
||||
LOGGER.trace("Add library reference: lib={} ==> {} base path={}", libName, classHandle.getCanonicalName(),
|
||||
basePath);
|
||||
if (basePath == null || basePath.isEmpty()) {
|
||||
basePath = "/";
|
||||
}
|
||||
@ -65,42 +67,11 @@ public class Uri {
|
||||
return data;
|
||||
}
|
||||
|
||||
// public static Stream<Path> getResources(final URL element) {
|
||||
// try {
|
||||
// final URI uri = element.toURI();
|
||||
// FileSystem fs;
|
||||
// Path path;
|
||||
// if (uri.getScheme().contentEquals("jar")) {
|
||||
// try {
|
||||
// fs = FileSystems.getFileSystem(uri);
|
||||
// } catch (final FileSystemNotFoundException e) {
|
||||
// fs = FileSystems.newFileSystem(uri, Collections.<String, String> emptyMap());
|
||||
// }
|
||||
// String pathInJar = "/";
|
||||
// final String tmpPath = element.getPath();
|
||||
// final int idSeparate = tmpPath.indexOf('!');
|
||||
// if (idSeparate != -1) {
|
||||
// pathInJar = tmpPath.substring(idSeparate + 1);
|
||||
// while (pathInJar.startsWith("/")) {
|
||||
// pathInJar = pathInJar.substring(1);
|
||||
// }
|
||||
// }
|
||||
// path = fs.getPath(pathInJar);
|
||||
// } else {
|
||||
// fs = FileSystems.getDefault();
|
||||
// path = Paths.get(uri);
|
||||
// }
|
||||
// return Files.walk(path, 1);
|
||||
// } catch (URISyntaxException | IOException e) {
|
||||
// e.printStackTrace();
|
||||
// return Stream.of();
|
||||
// }
|
||||
// }
|
||||
|
||||
private static List<String> getResourceFiles(final Class<?> clazz, final String path) throws IOException {
|
||||
final List<String> filenames = new ArrayList<>();
|
||||
|
||||
try (InputStream in = clazz.getResourceAsStream(path); BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
|
||||
try (InputStream in = clazz.getResourceAsStream(path);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
|
||||
String resource;
|
||||
while ((resource = br.readLine()) != null) {
|
||||
filenames.add(resource);
|
||||
@ -110,6 +81,7 @@ public class Uri {
|
||||
}
|
||||
|
||||
public static InputStream getStream(final Uri uri) {
|
||||
LOGGER.trace("????????????????????????????????????????????");
|
||||
LOGGER.trace("Load resource: {}", uri);
|
||||
String offsetGroup = "";
|
||||
if (uri.group != null) {
|
||||
@ -134,25 +106,31 @@ public class Uri {
|
||||
if (Uri.applicationClass == null) {
|
||||
LOGGER.trace(" !! Application data class is not defined ...");
|
||||
} else {
|
||||
String tmpPath = Uri.applicationBasePath + offsetGroup + uri.path;
|
||||
tmpPath = tmpPath.replace("//", "/");
|
||||
LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());// + " ==> " + applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath());
|
||||
String tmpPath = "/" + Uri.applicationBasePath + offsetGroup + uri.path;
|
||||
tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
|
||||
LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());
|
||||
final URL realFileName = Uri.applicationClass.getClassLoader().getResource(tmpPath);
|
||||
if (realFileName != null) {
|
||||
LOGGER.trace("(appl) >>> {}", realFileName.getFile());
|
||||
} else {
|
||||
LOGGER.trace("(appl) ??? base folder: {}", Uri.applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath() + Uri.applicationBasePath + offsetGroup + uri.path);
|
||||
LOGGER.trace("(appl) ??? base folder: {}",
|
||||
Uri.applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath() + tmpPath);
|
||||
}
|
||||
out = Uri.applicationClass.getResourceAsStream(tmpPath);
|
||||
LOGGER.trace("(appl) {} getResourceAsStream({})", Uri.applicationClass.getCanonicalName(), tmpPath);
|
||||
|
||||
out = Uri.applicationClass.getResourceAsStream("/" + tmpPath);
|
||||
|
||||
if (out == null) {
|
||||
LOGGER.trace("(appl) ==> element does not exist ...");
|
||||
// try {
|
||||
// LOGGER.warn("elements: " + getResourceFiles(applicationClass, applicationBasePath + offsetGroup + "/*.*"));
|
||||
// } catch (IOException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
LOGGER.error("(appl) ==> element does not exist ... {}", uri);
|
||||
/*
|
||||
try {
|
||||
LOGGER.warn("elements: " + getResourceFiles(applicationClass,
|
||||
BASE_RESOURCE_FOLDER + applicationBasePath + offsetGroup + "/*.*"));
|
||||
} catch (final IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
if (out == null) {
|
||||
@ -172,16 +150,18 @@ public class Uri {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
String tmpPath = libraryElement.basePath + offsetGroup + uri.path;
|
||||
tmpPath = tmpPath.replace("//", "/");
|
||||
String tmpPath = "/" + libraryElement.basePath + offsetGroup + uri.path;
|
||||
tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
|
||||
;
|
||||
LOGGER.trace("(lib) Try to load '{}' in {}", tmpPath, libraryElement.klass.getCanonicalName());
|
||||
final URL realFileName = libraryElement.klass.getClassLoader().getResource(tmpPath);
|
||||
if (realFileName != null) {
|
||||
LOGGER.trace("(lib) >>> {}", realFileName.getFile());
|
||||
} else {
|
||||
LOGGER.trace("(lib) ??? base folder: {}", libraryElement.klass.getProtectionDomain().getCodeSource().getLocation().getPath() + libraryElement.basePath + offsetGroup + uri.path);
|
||||
LOGGER.trace("(lib) ??? base folder: {}",
|
||||
libraryElement.klass.getProtectionDomain().getCodeSource().getLocation().getPath() + tmpPath);
|
||||
}
|
||||
out = libraryElement.klass.getResourceAsStream(tmpPath);
|
||||
out = libraryElement.klass.getResourceAsStream("/" + tmpPath);
|
||||
if (out == null) {
|
||||
LOGGER.trace("(lib) ==> element does not exist ...");
|
||||
}
|
||||
@ -246,18 +226,16 @@ public class Uri {
|
||||
if (index != -1) {
|
||||
final String valuesMetadata = value.substring(index + 1);
|
||||
final String[] elementMetaData = valuesMetadata.split("&");
|
||||
for (int iii = 0; iii < elementMetaData.length; iii++) {
|
||||
final String[] keyVal = elementMetaData[iii].split("=", 2);
|
||||
for (final String element : elementMetaData) {
|
||||
final String[] keyVal = element.split("=", 2);
|
||||
if (keyVal.length == 1) {
|
||||
prop.put(keyVal[0], "");
|
||||
} else {
|
||||
if (keyVal[0].equals("lib")) {
|
||||
} else if (keyVal[0].equals("lib")) {
|
||||
prop.put(keyVal[0], keyVal[1].toLowerCase());
|
||||
} else {
|
||||
prop.put(keyVal[0], keyVal[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
path = value.substring(0, index);
|
||||
} else {
|
||||
path = value;
|
||||
|
Loading…
x
Reference in New Issue
Block a user