branch feature/jdk11 updated (88318f2 -> 0f0d32c)
This is an automated email from the git hooks/post-receive script. New change to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git omits 88318f2 Rewrite org.nuiton.util.Resource#getURLs(java.lang.String, java.net.URLClassLoader) to be compatible with Java 9+ omits 6eaf28a Add some basic tests about Resource#getURL omits 5494472 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader adds f4a5b78 Merge branch 'feature/jdk11' into 'develop' new 18461e8 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader new 3007512 Add some basic tests about Resource#getURL new 0f0d32c Rewrite org.nuiton.util.Resource#getURLs(java.lang.String, java.net.URLClassLoader) to be compatible with Java 9+ This update added new revisions after undoing existing revisions. That is to say, some revisions that were in the old version of the branch are not in the new version. This situation occurs when a user --force pushes a change and generates a repository containing something like this: * -- * -- B -- O -- O -- O (88318f2) \ N -- N -- N refs/heads/feature/jdk11 (0f0d32c) You should already have received notification emails for all of the O revisions, and so the following emails describe only the N revisions from the common base, B. Any revisions marked "omits" are not gone; other references still refer to them. Any revisions marked "discards" are gone forever. The 3 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 0f0d32cbf3e1cebac4f9af899a3bfbe4bafa8bd4 Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 17:30:29 2020 +0100 Rewrite org.nuiton.util.Resource#getURLs(java.lang.String, java.net.URLClassLoader) to be compatible with Java 9+ commit 300751291d515e8a541105c2fae9eee7b1fd898b Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 15:26:26 2020 +0100 Add some basic tests about Resource#getURL commit 18461e82bd3d3671a22e4191fd2f76b8772a8167 Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 14:33:04 2020 +0100 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader Summary of changes: -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git commit 18461e82bd3d3671a22e4191fd2f76b8772a8167 Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 14:33:04 2020 +0100 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader --- src/main/java/org/nuiton/util/Resource.java | 34 -------- src/test/java/org/nuiton/util/ResourceTest.java | 102 +----------------------- 2 files changed, 2 insertions(+), 134 deletions(-) diff --git a/src/main/java/org/nuiton/util/Resource.java b/src/main/java/org/nuiton/util/Resource.java index ca7d6ab..5c9c078 100644 --- a/src/main/java/org/nuiton/util/Resource.java +++ b/src/main/java/org/nuiton/util/Resource.java @@ -30,7 +30,6 @@ import javax.swing.ImageIcon; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.lang.reflect.Method; import java.net.JarURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -74,39 +73,6 @@ public class Resource { // Resource } - /** - * Permet d'ajouter dans le classloader par defaut une nouvelle URL dans - * lequel il faut rechercher les fichiers. - * - * @param url l'url a ajouter - */ - public static void addDefaultClassLoader(URL url) { - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - addClassLoader(classLoader, url); - } - - /** - * Permet d'ajouter dans un classloader une nouvelle URL dans - * lequel il faut rechercher les fichiers. - * - * @param classLoader le classloader a modifier - * @param url l'url a ajouter - */ - public static void addClassLoader(ClassLoader classLoader, URL url) { - if (classLoader instanceof URLClassLoader) { - try { - Method method = URLClassLoader.class.getDeclaredMethod("addURL", - new Class[]{URL.class}); - method.setAccessible(true); - method.invoke(classLoader, url); - } catch (Exception eee) { - throw new RuntimeException(t("nuitonutil.error.add.url.in.classloader", classLoader, eee)); - } - } else { - throw new UnsupportedOperationException(Resource.class.getName() + " is not compatible with Java 9+ so far. See https://gitlab.nuiton.org/nuiton/nuiton-utils/issues/319"); - } - } - /** * Recherche la ressource nom. * diff --git a/src/test/java/org/nuiton/util/ResourceTest.java b/src/test/java/org/nuiton/util/ResourceTest.java index e047949..76d6353 100644 --- a/src/test/java/org/nuiton/util/ResourceTest.java +++ b/src/test/java/org/nuiton/util/ResourceTest.java @@ -25,22 +25,16 @@ package org.nuiton.util; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.After; import org.junit.Assert; import org.junit.Assume; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.net.URL; -import java.net.URLClassLoader; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -54,77 +48,6 @@ public class ResourceTest { // ResourceTest /** Logger. */ private static final Log log = LogFactory.getLog(ResourceTest.class); - // unaltered classloader - static ClassLoader systemClassLoader; - - @BeforeClass - public static void beforeClass() { - systemClassLoader = ClassLoader.getSystemClassLoader(); - } - - @Before - public void beforeTest() throws NoSuchFieldException, IllegalAccessException { - String errorMessage = "Test is disabled because " + Resource.class.getName() + " is not compatible with Java 9+ so far. See https://gitlab.nuiton.org/nuiton/nuiton-utils/issues/319"; - boolean testEnabled = systemClassLoader instanceof URLClassLoader; - if (!testEnabled && log.isErrorEnabled()) { - log.error(errorMessage); - } - Assume.assumeTrue(errorMessage, testEnabled); - URLClassLoader classLoader = (URLClassLoader) systemClassLoader; - ClassLoader parent = classLoader.getParent(); - ClassLoader currentClassLoader = new URLClassLoader(classLoader.getURLs(), parent); - Field field = ClassLoader.class.getDeclaredField("scl"); - field.setAccessible(true); - field.set(null, currentClassLoader); - } - - @After - public void afterTest() throws NoSuchFieldException, IllegalAccessException { - Field field = ClassLoader.class.getDeclaredField("scl"); - field.setAccessible(true); - field.set(null, systemClassLoader); - } - - @Test - public void testAddDefaultClassLoader() throws Exception { - String javaExecFilename = getJavaExecName(); - - Assert.assertNull(ResourceTest.class.getResource("/bin/" + javaExecFilename)); - - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - - //FIXME-tchemit-2012-07-23 On windows os java does not eixsts but java.exe does - File result = new File(repository, "bin" + File.separator + javaExecFilename); - - Assert.assertTrue(result.exists()); - - URL resultURL = Resource.getURL("bin/" + javaExecFilename); - Assert.assertEquals(result.toURI().toURL(), resultURL); - } - - @Test - public void testGetURL() throws Exception { - URL url; - - String javaExecFilename = getJavaExecName(); - - url = Resource.getURL("README.md"); - Assert.assertNotNull(url); - - try { - Resource.getURL("bin/" + javaExecFilename); - Assert.fail(); - } catch (ResourceNotFoundException e) { - Assert.assertTrue(true); - } - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - url = Resource.getURL("bin/" + javaExecFilename); - Assert.assertNotNull(url); - - } - @Test public void testGetURLsFromDirectory() throws Exception { @@ -147,37 +70,16 @@ public class ResourceTest { // ResourceTest } @Test - public void testGetURLsFromJar() throws Exception { + public void testGetURLsFromJarBeforeJava9() throws Exception { File repository = new File(System.getProperty("java.home")); File file = new File(repository, "lib" + File.separator + "rt.jar"); + Assume.assumeTrue(file.exists()); List<URL> result = Resource.getURLsFromJar(file, ".*OutOfMemoryError.*"); Assert.assertNotNull(result); Assert.assertEquals(1, result.size()); } - @Test - public void testGetURLs() throws Exception { - if (log.isInfoEnabled()) { - log.info(Arrays.asList(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs())); - } - - String javaExecFilename = getJavaExecName(); - String fileSeparatorRegex = StringUtil.getFileSeparatorRegex(); - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - List<URL> result; - - // comes - result = Resource.getURLs(".*bin" + fileSeparatorRegex + javaExecFilename); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - - result = Resource.getURLs("META-INF/MANIFEST.MF"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - } - @Test public void testIsJar() throws Exception { Assert.assertTrue(Resource.isJar("toto.jar")); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git commit 300751291d515e8a541105c2fae9eee7b1fd898b Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 15:26:26 2020 +0100 Add some basic tests about Resource#getURL --- pom.xml | 2 +- src/test/java/org/nuiton/util/ResourceTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 20a0668..da03306 100644 --- a/pom.xml +++ b/pom.xml @@ -138,7 +138,7 @@ <signatureArtifactId>java18</signatureArtifactId> <signatureVersion>1.0</signatureVersion> - <nuitonI18nVersion>4.0-rc-1</nuitonI18nVersion> + <nuitonI18nVersion>4.0-SNAPSHOT</nuitonI18nVersion> <!-- i18n configuration --> <i18n.bundles>fr_FR,en_GB,es_ES</i18n.bundles> diff --git a/src/test/java/org/nuiton/util/ResourceTest.java b/src/test/java/org/nuiton/util/ResourceTest.java index 76d6353..b9fd902 100644 --- a/src/test/java/org/nuiton/util/ResourceTest.java +++ b/src/test/java/org/nuiton/util/ResourceTest.java @@ -48,6 +48,16 @@ public class ResourceTest { // ResourceTest /** Logger. */ private static final Log log = LogFactory.getLog(ResourceTest.class); + @Test + public void testGetURL() { + Resource.getURL("/zip/not-a-zip.zip"); + } + + @Test(expected = ResourceNotFoundException.class) + public void testGetURLNotFound() { + Resource.getURL("/zip/non-existent-file.zip"); + } + @Test public void testGetURLsFromDirectory() throws Exception { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git commit 0f0d32cbf3e1cebac4f9af899a3bfbe4bafa8bd4 Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 17:30:29 2020 +0100 Rewrite org.nuiton.util.Resource#getURLs(java.lang.String, java.net.URLClassLoader) to be compatible with Java 9+ --- src/main/java/org/nuiton/util/Resource.java | 80 ++++++++++++++++++++++--- src/test/java/org/nuiton/util/ResourceTest.java | 32 +++++++++- 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/nuiton/util/Resource.java b/src/main/java/org/nuiton/util/Resource.java index 5c9c078..6df76ab 100644 --- a/src/main/java/org/nuiton/util/Resource.java +++ b/src/main/java/org/nuiton/util/Resource.java @@ -32,18 +32,26 @@ import java.io.FileInputStream; import java.io.IOException; import java.net.JarURLConnection; import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; +import java.nio.file.FileSystemNotFoundException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; @@ -158,23 +166,79 @@ public class Resource { // Resource * Retourner la liste des fichiers du classLoader. Ces fichiers doivent * correspondre au pattern donne. * - * @param classLoader classloader to use (if null, use {@link ClassLoader#getSystemClassLoader()} + * @param urlClassLoader classloader to use (if null, use {@link ClassLoader#getSystemClassLoader()} * @param pattern le nom du fichier a extraire du fichier compressé ou * du repertoire doit correspondre au pattern (repertoire + nom * compris). * @return la liste des urls correspondant au pattern */ - public static List<URL> getURLs(String pattern, URLClassLoader classLoader) { - if (classLoader == null) { + public static List<URL> getURLs(String pattern, URLClassLoader urlClassLoader) { + + Optional<URLClassLoader> notNullUrlClassLoader = Optional.ofNullable(urlClassLoader); + + // Si on a pas d'URLClassLoader on essaye de prendre celui du system + if (!notNullUrlClassLoader.isPresent()) { ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); if (systemClassLoader instanceof URLClassLoader) { - classLoader = (URLClassLoader) systemClassLoader; - } else { - throw new UnsupportedOperationException(Resource.class.getName() + " is not compatible with Java 9+ so far. See https://gitlab.nuiton.org/nuiton/nuiton-utils/issues/319"); + notNullUrlClassLoader = Optional.of((URLClassLoader) systemClassLoader); + } + } + + if (notNullUrlClassLoader.isPresent()) { + URL[] urls = ClassLoaderUtil.getURLs(notNullUrlClassLoader.get()); + List<URL> result = getURLs(pattern, urls); + return result; + } else { + // Malgré les tentatives impossible de trouver un URLClassLoader, donc on va parcourir les ressources accessibles + List<URL> urls = walkThroughClassLoaderAndGetURLs(pattern); + return urls; + } + } + + private static URL safeUriToUrlOrNull(URI uri) { + try { + return uri.toURL(); + } catch (MalformedURLException e) { + if (log.isWarnEnabled()) { + log.warn("An error occured while walking through classpath, it may not work as expected",e); } + return null; + } + } + + protected static List<URL> walkThroughClassLoaderAndGetURLs(String pattern) { + + try { + List<URL> classpathUrls = new ArrayList<>(); + + Enumeration<URL> urls = ClassLoader.getSystemResources(""); + while (urls.hasMoreElements()) { + try { + try (Stream<Path> walk = Files.walk(Paths.get(urls.nextElement().toURI()))) { + + walk.filter(Files::isRegularFile) + .map(Path::toUri) + .map(Resource::safeUriToUrlOrNull) + .filter(Objects::nonNull) + .filter(url -> url.getPath().matches(pattern)) + .forEach(classpathUrls::add); + + } catch (IOException | FileSystemNotFoundException e) { + if (log.isWarnEnabled()) { + log.warn("An error occurred while walking through classpath, it may not work as expected", e); + } + } + } catch (URISyntaxException urise) { + if (log.isWarnEnabled()) { + log.warn("An error occurred while walking through classpath, it may not work as expected", urise); + } + } + } + + return classpathUrls; + } catch (IOException e) { + return new ArrayList<>(); } - URL[] arrayURL = ClassLoaderUtil.getURLs(classLoader); - return getURLs(pattern, arrayURL); } /** diff --git a/src/test/java/org/nuiton/util/ResourceTest.java b/src/test/java/org/nuiton/util/ResourceTest.java index b9fd902..c61d25c 100644 --- a/src/test/java/org/nuiton/util/ResourceTest.java +++ b/src/test/java/org/nuiton/util/ResourceTest.java @@ -50,7 +50,12 @@ public class ResourceTest { // ResourceTest @Test public void testGetURL() { - Resource.getURL("/zip/not-a-zip.zip"); + + URL url = Resource.getURL("README.md"); + Assert.assertNotNull(url); + + url = Resource.getURL("/zip/not-a-zip.zip"); + Assert.assertNotNull(url); } @Test(expected = ResourceNotFoundException.class) @@ -58,6 +63,31 @@ public class ResourceTest { // ResourceTest Resource.getURL("/zip/non-existent-file.zip"); } + @Test + public void testGetURLs() { + List<URL> result; + + result = Resource.getURLs(".*zip"); + Assert.assertNotNull(result); + Assert.assertEquals(2, result.size()); + + result = Resource.getURLs(".*zap"); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + + result = Resource.getURLs(".*zop"); + Assert.assertNotNull(result); + Assert.assertEquals(0, result.size()); + + result = Resource.getURLs(".*windows.properties"); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + + result = Resource.getURLs(".*properties/windows.properties"); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + } + @Test public void testGetURLsFromDirectory() throws Exception { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm