Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Support #32

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CForgroundManager {

private static Logger LOGGER = Logger.getLogger(CForgroundManager.class);
private String [] launchArray;
private String [] envArray = null;
private Process process;
/**
* The exit value of the forked process. Initialized to 9999.
Expand Down Expand Up @@ -51,6 +52,16 @@ public void setLaunchArray(String[] launchArray) {
}


/**
* Set the fork command environment variables

* @param envArray
*/
public void setEvnArray(String[] envArray) {
this.envArray = envArray;
}


/**
* Add a handler for system out events

Expand Down Expand Up @@ -86,10 +97,18 @@ public void go() {
* String launch = "/bin/bash -c \"env - CASSANDRA_CONF=" + instanceConf.getAbsolutePath()
* +" JAVA_HOME="+ "/usr/java/jdk1.7.0_45 " + cstart.getAbsolutePath().toString() + " -f \"";
*/
LOGGER.debug(Arrays.asList(this.launchArray));
LOGGER.debug("ENVIRONMENT: " + Arrays.asList(this.envArray));
LOGGER.debug("LAUNCH COMANDS: " + Arrays.asList(this.launchArray));
Runtime rt = Runtime.getRuntime();
try {
process = rt.exec(launchArray);
if (envArray != null)
{
process = rt.exec(launchArray,envArray);
}
else
{
process = rt.exec(launchArray);
}
} catch (IOException e1) {
LOGGER.error(e1.getMessage());
throw new RuntimeException(e1);
Expand Down Expand Up @@ -129,7 +148,63 @@ public void run() {
* End the process but do not wait for shutdown latch. Non blocking
*/
public void destroy(){
process.destroy();
if (isWindows()) {

// Unlike in unix killing a process does not kill any child processes.
// In order to destroy our Cassandra java process, we'll use wimc to query
// for a java process with our instance name in the classpath.
String confPath = null;
for (String s : envArray)
{
if (s.contains("CASSANDRA_CONF")) {
confPath = s.split("=")[1];

// wimc requires paths to be double-quoted backslashes.
String wmicKill = "wmic PROCESS where " +
"\"" + "name like '%java%' and CommandLine like " +
"'%" + confPath.replace("\\", "\\\\") + "%' and " +
"CommandLine like '%CassandraDaemon%'" + "\"" +
" call Terminate";
try {
Process p = Runtime.getRuntime().exec(wmicKill);
p.getOutputStream().close();
p.waitFor(10, TimeUnit.SECONDS);
int exitCode = p.exitValue();
if (exitCode == 0) {
LOGGER.info("Cassandra process destroyed.");
}else {
LOGGER.error("Non-zero exit code from killing the cassandra process.");
}

} catch (IOException | InterruptedException e) {
LOGGER.error("Could not kill the Cassandra java child process");
}
break;
}
}
if (confPath == null) {
LOGGER.error("Could not locate and kill java child process");
}

process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
; // nothing to do
}
} else {
process.destroy();
}
}
/**
* Determines if the operating system is Windows
*
* @return a boolean value indicating if we're on Windows OS
*/

private boolean isWindows() {
String os = System.getProperty("os.name");
return os.contains("Windows");
}

/**
Expand Down
Loading