
org.artofsolving.jodconverter.process.WindowsProcessManager Maven / Gradle / Ivy
/**
* Copyright (C) 2010 Daniel Manzke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.artofsolving.jodconverter.process;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
/**
* {@link ProcessManager} implementation for Windows.
*
* Requires wmic.exe and taskkill.exe, that should be available at least on
* Windows XP, Windows Vista, and Windows 7 (except Home versions).
*/
public class WindowsProcessManager implements ProcessManager {
private static final Pattern PROCESS_GET_LINE = Pattern.compile("^(.*?)\\s+(\\d+)\\s*$");
public String findPid(String regex) throws IOException {
Pattern commandPattern = Pattern.compile(regex);
for (String line : execute("wmic", "process", "get", "CommandLine,ProcessId")) {
Matcher lineMatcher = PROCESS_GET_LINE.matcher(line);
if (lineMatcher.matches()) {
String commandLine = lineMatcher.group(1);
String pid = lineMatcher.group(2);
Matcher commandMatcher = commandPattern.matcher(commandLine);
if (commandMatcher.find()) {
return pid;
}
}
}
return null;
}
public void kill(Process process, String pid) throws IOException {
execute("taskkill", "/t", "/f", "/pid", pid);
}
public boolean isUsable() {
try {
execute("wmic", "quit");
execute("taskkill", "/?");
return true;
} catch (IOException ioException) {
return false;
}
}
private List execute(String... command) throws IOException {
Process process = new ProcessBuilder(command).start();
process.getOutputStream().close(); // don't wait for stdin
@SuppressWarnings("unchecked")
List lines = IOUtils.readLines(process.getInputStream());
try {
process.waitFor();
} catch (InterruptedException interruptedException) {
// sorry for the interruption
}
return lines;
}
}