Skip to content

Commit 3786292

Browse files
committed
Remove hard dependency on Slf4J in API
1 parent 066f268 commit 3786292

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

docker-java-api/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<groupId>org.slf4j</groupId>
2727
<artifactId>slf4j-api</artifactId>
2828
<version>${slf4j-api.version}</version>
29+
<scope>provided</scope>
2930
</dependency>
3031

3132
<dependency>

docker-java-api/src/main/java/com/github/dockerjava/api/async/ResultCallbackTemplate.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.util.concurrent.CountDownLatch;
1212
import java.util.concurrent.TimeUnit;
13+
import java.util.function.Consumer;
1314

1415
/**
1516
* Abstract template implementation of {@link ResultCallback}
@@ -20,7 +21,19 @@
2021
public abstract class ResultCallbackTemplate<RC_T extends ResultCallback<A_RES_T>, A_RES_T> implements
2122
ResultCallback<A_RES_T> {
2223

23-
private static final Logger LOGGER = LoggerFactory.getLogger(ResultCallbackTemplate.class);
24+
private static final Consumer<Throwable> ERROR_LOGGER;
25+
26+
static {
27+
Consumer<Throwable> errorLogger = e -> {};
28+
try {
29+
if (Class.forName("org.slf4j.LoggerFactory") != null) {
30+
Logger LOGGER = LoggerFactory.getLogger(ResultCallbackTemplate.class);
31+
errorLogger = e -> LOGGER.error("Error during callback", e);
32+
}
33+
} catch (ClassNotFoundException ignored) {
34+
}
35+
ERROR_LOGGER = errorLogger;
36+
}
2437

2538
private final CountDownLatch started = new CountDownLatch(1);
2639

@@ -49,7 +62,7 @@ public void onError(Throwable throwable) {
4962
}
5063

5164
try {
52-
LOGGER.error("Error during callback", throwable);
65+
ERROR_LOGGER.accept(throwable);
5366
} finally {
5467
try {
5568
close();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.dockerjava.api.async;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.io.File;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.net.URLClassLoader;
10+
import java.util.Set;
11+
import java.util.concurrent.atomic.AtomicBoolean;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
15+
public class ResultCallbackTemplateTest {
16+
17+
static Set<URL> CLASSPATH = Stream.of(System.getProperty("java.class.path").split(":"))
18+
.map(it -> {
19+
try {
20+
return new File(it).toURI().toURL();
21+
} catch (MalformedURLException e) {
22+
throw new RuntimeException(e);
23+
}
24+
})
25+
.collect(Collectors.toSet());
26+
27+
@Test
28+
public void shouldNotFailIfSlf4jIsNotOnClasspath() throws Exception {
29+
try(
30+
URLClassLoader classLoader = new URLClassLoader(
31+
CLASSPATH.stream().filter(it -> !it.getPath().contains("slf4j")).toArray(URL[]::new),
32+
null
33+
)
34+
) {
35+
Class<?> clazz = classLoader.loadClass(DummyResultCallbackTemplate.class.getName());
36+
Object template = clazz.newInstance();
37+
AtomicBoolean classNotFound = new AtomicBoolean();
38+
clazz.getMethod("onNext", AtomicBoolean.class).invoke(template, classNotFound);
39+
Assert.assertTrue("Slf4J is not on classpath", classNotFound.get());
40+
clazz.getMethod("onError", Throwable.class).invoke(template, new Exception("Boom"));
41+
}
42+
}
43+
44+
public static class DummyResultCallbackTemplate extends ResultCallbackTemplate<DummyResultCallbackTemplate, AtomicBoolean> {
45+
46+
@Override
47+
public void onNext(AtomicBoolean atomicBoolean) {
48+
try {
49+
atomicBoolean.set(Class.forName("org.slf4j.LoggerFactory") == null);
50+
} catch (ClassNotFoundException ignored) {
51+
atomicBoolean.set(true);
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)