[FIX] resource Uri finder

This commit is contained in:
Edouard DUPIN 2024-06-10 22:08:16 +02:00
parent 5a5e7de99d
commit 0067726c33

View File

@ -14,12 +14,13 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class Uri { public class Uri {
final static Logger LOGGER = LoggerFactory.getLogger(Uri.class); final static Logger LOGGER = LoggerFactory.getLogger(Uri.class);
private record LibraryElement( private record LibraryElement(
Class<?> klass, Class<?> klass,
String basePath) {} String basePath) {}
@ -37,7 +38,8 @@ public class Uri {
} }
public static void addLibrary(final String libName, final Class<?> classHandle, String basePath) { 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()) { if (basePath == null || basePath.isEmpty()) {
basePath = "/"; basePath = "/";
} }
@ -65,42 +67,11 @@ public class Uri {
return data; 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 { private static List<String> getResourceFiles(final Class<?> clazz, final String path) throws IOException {
final List<String> filenames = new ArrayList<>(); 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; String resource;
while ((resource = br.readLine()) != null) { while ((resource = br.readLine()) != null) {
filenames.add(resource); filenames.add(resource);
@ -110,6 +81,7 @@ public class Uri {
} }
public static InputStream getStream(final Uri uri) { public static InputStream getStream(final Uri uri) {
LOGGER.trace("????????????????????????????????????????????");
LOGGER.trace("Load resource: {}", uri); LOGGER.trace("Load resource: {}", uri);
String offsetGroup = ""; String offsetGroup = "";
if (uri.group != null) { if (uri.group != null) {
@ -134,25 +106,31 @@ public class Uri {
if (Uri.applicationClass == null) { if (Uri.applicationClass == null) {
LOGGER.trace(" !! Application data class is not defined ..."); LOGGER.trace(" !! Application data class is not defined ...");
} else { } else {
String tmpPath = Uri.applicationBasePath + offsetGroup + uri.path; String tmpPath = "/" + Uri.applicationBasePath + offsetGroup + uri.path;
tmpPath = tmpPath.replace("//", "/"); tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());// + " ==> " + applicationClass.getProtectionDomain().getCodeSource().getLocation().getPath()); LOGGER.trace("(appl) Try to load '{}' in {}", tmpPath, Uri.applicationClass.getCanonicalName());
final URL realFileName = Uri.applicationClass.getClassLoader().getResource(tmpPath); final URL realFileName = Uri.applicationClass.getClassLoader().getResource(tmpPath);
if (realFileName != null) { if (realFileName != null) {
LOGGER.trace("(appl) >>> {}", realFileName.getFile()); LOGGER.trace("(appl) >>> {}", realFileName.getFile());
} else { } 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) { if (out == null) {
LOGGER.trace("(appl) ==> element does not exist ..."); LOGGER.error("(appl) ==> element does not exist ... {}", uri);
// try { /*
// LOGGER.warn("elements: " + getResourceFiles(applicationClass, applicationBasePath + offsetGroup + "/*.*")); try {
// } catch (IOException e) { LOGGER.warn("elements: " + getResourceFiles(applicationClass,
// // TODO Auto-generated catch block BASE_RESOURCE_FOLDER + applicationBasePath + offsetGroup + "/*.*"));
// e.printStackTrace(); } catch (final IOException e) {
// } // TODO Auto-generated catch block
e.printStackTrace();
}
*/
} }
} }
if (out == null) { if (out == null) {
@ -172,16 +150,18 @@ public class Uri {
// // TODO Auto-generated catch block // // TODO Auto-generated catch block
// e.printStackTrace(); // e.printStackTrace();
// } // }
String tmpPath = libraryElement.basePath + offsetGroup + uri.path; String tmpPath = "/" + libraryElement.basePath + offsetGroup + uri.path;
tmpPath = tmpPath.replace("//", "/"); tmpPath = tmpPath.replace("///", "/").replace("//", "/").replaceFirst("^/*", "");
;
LOGGER.trace("(lib) Try to load '{}' in {}", tmpPath, libraryElement.klass.getCanonicalName()); LOGGER.trace("(lib) Try to load '{}' in {}", tmpPath, libraryElement.klass.getCanonicalName());
final URL realFileName = libraryElement.klass.getClassLoader().getResource(tmpPath); final URL realFileName = libraryElement.klass.getClassLoader().getResource(tmpPath);
if (realFileName != null) { if (realFileName != null) {
LOGGER.trace("(lib) >>> {}", realFileName.getFile()); LOGGER.trace("(lib) >>> {}", realFileName.getFile());
} else { } 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) { if (out == null) {
LOGGER.trace("(lib) ==> element does not exist ..."); LOGGER.trace("(lib) ==> element does not exist ...");
} }
@ -246,18 +226,16 @@ public class Uri {
if (index != -1) { if (index != -1) {
final String valuesMetadata = value.substring(index + 1); final String valuesMetadata = value.substring(index + 1);
final String[] elementMetaData = valuesMetadata.split("&"); final String[] elementMetaData = valuesMetadata.split("&");
for (int iii = 0; iii < elementMetaData.length; iii++) { for (final String element : elementMetaData) {
final String[] keyVal = elementMetaData[iii].split("=", 2); final String[] keyVal = element.split("=", 2);
if (keyVal.length == 1) { if (keyVal.length == 1) {
prop.put(keyVal[0], ""); prop.put(keyVal[0], "");
} else { } else if (keyVal[0].equals("lib")) {
if (keyVal[0].equals("lib")) {
prop.put(keyVal[0], keyVal[1].toLowerCase()); prop.put(keyVal[0], keyVal[1].toLowerCase());
} else { } else {
prop.put(keyVal[0], keyVal[1]); prop.put(keyVal[0], keyVal[1]);
} }
} }
}
path = value.substring(0, index); path = value.substring(0, index);
} else { } else {
path = value; path = value;