Skip to content

Commit

Permalink
Merge pull request #940 from isavin/fix_911_npe_in_log_callback
Browse files Browse the repository at this point in the history
#911 Fix NPE in LogCallback
  • Loading branch information
rhuss authored Feb 8, 2018
2 parents d2a7861 + bbb0a67 commit 7ac52ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;

import com.google.common.io.Files;
import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.util.Timestamp;

Expand Down Expand Up @@ -46,6 +47,7 @@ public synchronized void open() throws IOException {
} else {
SharedPrintStream cachedPs = printStreamMap.get(file);
if (cachedPs == null) {
Files.createParentDirs(new File(file));
PrintStream ps = new PrintStream(new FileOutputStream(file), true);
cachedPs = new SharedPrintStream(ps);
printStreamMap.put(file, cachedPs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void allocate() {
boolean close() {
int nrUsers = numUsers.decrementAndGet();
if (nrUsers == 0 && printStream != System.out) {
printStream.close();;
printStream.close();
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.google.common.io.Files;
import org.apache.maven.shared.utils.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -147,6 +149,18 @@ public void shouldLogInParallel() throws IOException, DoneException, Interrupted
assertThat(indexes, is(empty()));
}

@Test
public void shouldCreateParentDirs() throws IOException {
File dir = Files.createTempDir();
dir.deleteOnExit();
file = new File(dir, "non/existing/dirs/file.log");
spec = new LogOutputSpec.Builder().prefix("callback-test> ")
.file(file.toString()).build();
callback = new DefaultLogCallback(spec);
callback.open();
assertTrue(file.exists());
}

private class LoggerTask implements Runnable {

private LogCallback cb;
Expand Down

0 comments on commit 7ac52ef

Please sign in to comment.