Skip to content

Commit

Permalink
Provide a valid class loader even if there is none from thread context
Browse files Browse the repository at this point in the history
Fixes #131
  • Loading branch information
pmwmedia committed Feb 3, 2020
1 parent 7d1651c commit 08afcca
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.tinylog.Level;
import org.tinylog.provider.InternalLogger;
import org.tinylog.runtime.RuntimeProvider;

/**
* Global configuration for tinylog.
Expand Down Expand Up @@ -212,7 +213,7 @@ private static Properties load() {
if (URL_DETECTION_PATTERN.matcher(file).matches()) {
stream = new URL(file).openStream();
} else {
stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
stream = RuntimeProvider.getClassLoader().getResourceAsStream(file);
if (stream == null) {
stream = new FileInputStream(file);
}
Expand All @@ -221,7 +222,7 @@ private static Properties load() {
} else {
for (String configurationFile : CONFIGURATION_FILES) {
file = configurationFile;
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
InputStream stream = RuntimeProvider.getClassLoader().getResourceAsStream(file);
if (stream != null) {
load(properties, stream);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.tinylog.Level;
import org.tinylog.provider.InternalLogger;
import org.tinylog.runtime.RuntimeProvider;

/**
* Alternative service loader that supports constructors with arguments in opposite to {@link java.util.ServiceLoader}.
Expand Down Expand Up @@ -55,7 +56,7 @@ public final class ServiceLoader<T> {
public ServiceLoader(final Class<? extends T> service, final Class<?>... argumentTypes) {
this.service = service;
this.argumentTypes = argumentTypes;
this.classLoader = Thread.currentThread().getContextClassLoader();
this.classLoader = RuntimeProvider.getClassLoader();
this.classes = loadClasses(classLoader, service);
}

Expand Down
15 changes: 15 additions & 0 deletions tinylog-api/src/main/java/org/tinylog/runtime/RuntimeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ public final class RuntimeProvider {
private RuntimeProvider() {
}

/**
* Gets a valid class loader.
*
* @return Valid class loader instance
*/
public static ClassLoader getClassLoader() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

if (classLoader == null) {
classLoader = RuntimeProvider.class.getClassLoader();
}

return classLoader;
}

/**
* Gets the name of the default writer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.tinylog.util.SimpleTimestamp;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link RuntimeProvider}.
Expand Down Expand Up @@ -56,6 +57,35 @@ public void reset() throws Exception {
Whitebox.setInternalState(RuntimeProvider.class, RuntimeDialect.class, dialect);
}

/**
* Verifies that the thread context class loader will be returned, if available.
*/
@Test
public void classLoaderFromThread() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
ClassLoader mock = mock(ClassLoader.class);
Thread.currentThread().setContextClassLoader(mock);
assertThat(RuntimeProvider.getClassLoader()).isSameAs(mock);
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
}

/**
* Verifies that a class loader will be returned, even if there is no thread context class loader.
*/
@Test
public void classLoaderNotFromThread() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(null);
assertThat(RuntimeProvider.getClassLoader()).isNotNull();
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
}

/**
* Verifies that the name of a default writer will be returned.
*/
Expand Down

0 comments on commit 08afcca

Please sign in to comment.