aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2023-03-04 09:04:47 -0500
committerGary Gregory <garydgregory@gmail.com>2023-03-04 09:04:47 -0500
commite9f3ece022854a7c415b6ce8435ca803dc8aee8b (patch)
tree6ca4c910603b886327fe6e4e77e9408a24028e43
parent8ba794181daee4a827184771300bb172b5de8e42 (diff)
downloadapache-commons-lang-e9f3ece022854a7c415b6ce8435ca803dc8aee8b.tar.gz
Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().
-rw-r--r--src/changes/changes.xml1
-rw-r--r--src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java30
-rw-r--r--src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java17
3 files changed, 44 insertions, 4 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 34e5c9aec..e12e39408 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -188,6 +188,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Arturo Bernal">Add CalendarUtils#getDayOfYear() #968</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add NumberRange, DoubleRange, IntegerRange, LongRange.</action>
<action type="add" dev="ggregory" due-to="Diego Marcilio, Bruno P. Kinoshita, Gary Gregory">Add missing exception javadoc/tests for some null arguments #869.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java b/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java
index a87156a08..dda4828a2 100644
--- a/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java
@@ -17,6 +17,7 @@
package org.apache.commons.lang3;
+import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
@@ -27,6 +28,32 @@ import java.util.Arrays;
*/
public class ClassLoaderUtils {
+ private static final URL[] EMPTY_URL_ARRAY = new URL[] {};
+
+ /**
+ * Gets the system class loader's URLs, if any.
+ *
+ * @return the system class loader's URLs, if any.
+ * @since 3.13.0
+ */
+ public static URL[] getSystemURLs() {
+ return getURLs(ClassLoader.getSystemClassLoader());
+ }
+
+ /**
+ * Gets the current thread's context class loader's URLs, if any.
+ *
+ * @return the current thread's context class loader's URLs, if any.
+ * @since 3.13.0
+ */
+ public static URL[] getThreadURLs() {
+ return getURLs(Thread.currentThread().getContextClassLoader());
+ }
+
+ private static URL[] getURLs(final ClassLoader cl) {
+ return cl instanceof URLClassLoader ? ((URLClassLoader) cl).getURLs() : EMPTY_URL_ARRAY;
+ }
+
/**
* Converts the given class loader to a String calling {@link #toString(URLClassLoader)}.
*
@@ -41,8 +68,7 @@ public class ClassLoaderUtils {
}
/**
- * Converts the given URLClassLoader to a String in the format
- * {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}.
+ * Converts the given URLClassLoader to a String in the format {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}.
*
* @param classLoader to URLClassLoader to convert.
* @return the formatted string.
diff --git a/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java
index ea56f50c8..b51ed3635 100644
--- a/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java
@@ -17,6 +17,8 @@
package org.apache.commons.lang3;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -30,6 +32,18 @@ import org.junit.jupiter.api.Test;
public class ClassLoaderUtilsTest extends AbstractLangTest {
@Test
+ public void testGetSystemURLs() {
+ // TODO How to better test considering this test may be called from an IDE and Maven?
+ assertNotNull(ClassLoaderUtils.getSystemURLs());
+ }
+
+ @Test
+ public void testGetThreadURLs() {
+ // TODO How to better test considering this test may be called from an IDE and Maven?
+ assertNotNull(ClassLoaderUtils.getThreadURLs());
+ }
+
+ @Test
public void testToString_ClassLoader() throws IOException {
final URL url = new URL("http://localhost");
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) {
@@ -43,8 +57,7 @@ public class ClassLoaderUtilsTest extends AbstractLangTest {
public void testToString_URLClassLoader() throws IOException {
final URL url = new URL("http://localhost");
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) {
- Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url),
- ClassLoaderUtils.toString(urlClassLoader));
+ Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url), ClassLoaderUtils.toString(urlClassLoader));
}
}
}