Permalink
Browse files

retry to get cdp4jId more than once

  • Loading branch information...
webfolderio committed Sep 2, 2018
1 parent 340df46 commit 467a0a2ac9e47be8011f7eb189ee1cec2fbeaff6
Showing with 70 additions and 7 deletions.
  1. +70 −7 src/main/java/io/webfolder/cdp/WindowsProcessManager.java
@@ -18,8 +18,14 @@
*/
package io.webfolder.cdp;

import static java.lang.Thread.sleep;

import java.util.TreeMap;

import org.jvnet.winp.WinProcess;

import io.webfolder.cdp.exception.CdpException;

public class WindowsProcessManager extends ProcessManager {

private int pid;
@@ -30,7 +36,58 @@
void setProcess(CdpProcess process) {
WinProcess winProcess = new WinProcess(process.getProcess());
pid = winProcess.getPid();
cdp4jId = winProcess.getEnvironmentVariables().get("CDP4J_ID");
cdp4jId = getCdp4jId(winProcess, 20);
if (cdp4jId == null || cdp4jId.trim().isEmpty()) {
throw new CdpException("cdp4jId not found");
}
}

private String getCdp4jId(WinProcess process, int retryCount) {
String id = getCdp4jId(process);
if ( id != null ) {
return id;
} else {
for (int i = 0; i < retryCount; i++) {
id = getCdp4jId(process);
if ( id != null ) {
return id;
} else {
try {
sleep(500);
} catch (InterruptedException e) {
throw new CdpException(e);
}
}
}
}
return id;
}

private String getCdp4jId(WinProcess process) {
String cdp4jId = null;
try {
TreeMap<String, String> variables = process.getEnvironmentVariables();
cdp4jId = variables.get("CDP4J_ID");
} catch (Throwable t) {
// ignore
}
if ( cdp4jId != null && ! cdp4jId.trim().isEmpty() ) {
return cdp4jId;
}
try {
String cmd = process.getCommandLine();
int start = cmd.indexOf("--cdp4jId=");
if (start > 0) {
int end = cmd.indexOf(" ", start + 1);
if (end == -1) {
end = cmd.length();
}
cdp4jId = cmd.substring(start + "--cdp4jId=".length(), end);
}
} catch (Throwable t) {
return null;
}
return cdp4jId;
}

@Override
@@ -42,16 +99,22 @@ public boolean kill() {
}
try {
WinProcess process = new WinProcess(pid);
String cdp4jId = process.getEnvironmentVariables().get("CDP4J_ID");
if (pid == process.getPid() &&
if (process.isRunning()) {
String cdp4jId = getCdp4jId(process, 20);
if (cdp4jId == null || cdp4jId.trim().isEmpty()) {
throw new CdpException("cdp4jId not found");
}
if (pid == process.getPid() &&
this.cdp4jId.equals(cdp4jId)) {
process.killRecursively();
return true;
} else {
return false;
process.killRecursively();
return true;
} else {
return false;
}
}
} catch (Throwable t) {
return false;
}
return false;
}
}

0 comments on commit 467a0a2

Please sign in to comment.