org.openengsb.openengsbplugin.tools.OpenEngSBJavaRunner Maven / Gradle / Ivy
/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI licenses this file to you 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.openengsb.openengsbplugin.tools;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import org.ops4j.io.Pipe;
import org.ops4j.pax.runner.platform.PlatformException;
import org.ops4j.pax.runner.platform.internal.CommandLineBuilder;
public class OpenEngSBJavaRunner {
private Process process;
private Thread shutdownHook;
private Pipe errorStreamMapper;
private Pipe outStreamMapper;
private Pipe inStreamMapper;
private Runnable shutdownRunnable;
private String[] commandLine;
private Map environment;
public OpenEngSBJavaRunner(CommandLineBuilder command, Map env) {
commandLine = command.toArray();
environment = env;
}
public synchronized void exec() throws PlatformException {
startProcess();
createShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
waitForExit();
}
private void startProcess() throws PlatformException {
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(commandLine);
Map env = processBuilder.environment();
for (Entry entry : environment.entrySet()) {
env.put(entry.getKey(), entry.getValue());
}
process = processBuilder.start();
} catch (IOException e) {
throw new PlatformException("Could not start up the process", e);
}
}
public void shutdown() {
try {
synchronized (shutdownHook) {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
process = null;
shutdownHook.run();
shutdownHook = null;
}
}
} catch (IllegalStateException ignore) {
}
}
public void waitForExit() {
synchronized (process) {
try {
process.waitFor();
shutdown();
} catch (Throwable e) {
shutdown();
}
}
}
private void createShutdownHook() {
createPipes();
createShutdownRunnable();
shutdownHook = new Thread(shutdownRunnable, "OpenEngSB Runner Shutdown Hook");
}
private void createPipes() {
errorStreamMapper = new Pipe(process.getErrorStream(), System.err).start("Error pipe");
outStreamMapper = new Pipe(process.getInputStream(), System.out).start("Out pipe");
inStreamMapper = new Pipe(process.getOutputStream(), System.in).start("In pipe");
}
private void createShutdownRunnable() {
shutdownRunnable = new Runnable() {
@Override
public void run() {
stopPipes();
destroyProcess();
}
};
}
private void stopPipes() {
inStreamMapper.stop();
outStreamMapper.stop();
errorStreamMapper.stop();
}
private void destroyProcess() {
try {
process.destroy();
} catch (Exception ignore) {
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy