Skip to content

Commit

Permalink
rethrow initialization error in Native.sendCtrlC (jenkinsci#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
segrey committed Jan 12, 2019
1 parent c9537f4 commit 4d7d374
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
57 changes: 57 additions & 0 deletions src/main/java/org/jvnet/winp/LazyValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jvnet.winp;

import java.util.concurrent.Callable;

final class LazyValue<T> {
private final Callable<T> myProvider;
private final Object myLock = new Object();
private volatile Result<T> myResult;

LazyValue(Callable<T> provider) {
myProvider = provider;
}

public T getValue() throws Exception {
Result<T> result = myResult;
if (result != null) {
return unpack(result);
}
synchronized (myLock) {
result = myResult;
if (result == null) {
try {
T value = myProvider.call();
result = new Result<T>(value, null);
}
catch (Throwable t) {
result = new Result<T>(null, t);
}
myResult = result;
}
}
return unpack(result);
}

private T unpack(Result<T> result) throws Exception {
if (result.throwable != null) {
if (result.throwable instanceof Exception) {
throw (Exception)result.throwable;
}
if (result.throwable instanceof Error) {
throw (Error)result.throwable;
}
throw new RuntimeException("Rethrowing unknown Throwable", result.throwable);
}
return result.value;
}

private static class Result<T> {
private final T value;
private final Throwable throwable;

private Result(T value, Throwable throwable) {
this.value = value;
this.throwable = throwable;
}
}
}
31 changes: 23 additions & 8 deletions src/main/java/org/jvnet/winp/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import javax.annotation.CheckReturnValue;
import java.io.*;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URISyntaxException;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -70,7 +71,20 @@ class Native {
private static final String DLL_TARGET = "winp.folder.preferred";
private static final String UNPACK_DLL_TO_PARENT_DIR = "winp.unpack.dll.to.parent.dir";

private static String ctrlCExePath;
private static final LazyValue<String> ctrlCExePathLazyValue = new LazyValue<String>(new Callable<String>() {
public String call() {
File exeFile = load();
return exeFile == null ? null : exeFile.getPath();
}
});

static {
try {
ctrlCExePathLazyValue.getValue(); // load winp library
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, "Cannot init winp native", e);
}
}

/**
* Sends Ctrl+C to the process.
Expand All @@ -82,18 +96,19 @@ class Native {
*/
@CheckReturnValue
public static boolean sendCtrlC(int pid) throws WinpException {
String ctrlCExePath;
try {
ctrlCExePath = ctrlCExePathLazyValue.getValue();
} catch (Throwable e) {
throw new WinpException("Cannot send the CtrlC signal to the process", e);
}
if (ctrlCExePath == null) {
LOGGER.log(Level.WARNING, "Cannot send the CtrlC signal to the process. Cannot find the executable {0}.dll", CTRLCEXE_NAME);
return false;
}
return CtrlCSender.sendCtrlC(pid, ctrlCExePath);
}

static {
File exeFile = load();
ctrlCExePath = (exeFile == null) ? null : exeFile.getPath();
}

private static String md5(URL res) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
Expand Down

0 comments on commit 4d7d374

Please sign in to comment.