X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Fsupport%2Fdeploy%2FHttpdServerManager.java;fp=org.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Fsupport%2Fdeploy%2FHttpdServerManager.java;h=3fd76ed9db5680abc331dcfbe6fd586f42cacec7;hb=b9505fef5ba8186433e903e9de3c73c17bdf6562;hp=0000000000000000000000000000000000000000;hpb=04ef2e4533e909122a560a5cb6499fa62bac82ec;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.support/src/org/argeo/slc/support/deploy/HttpdServerManager.java b/org.argeo.slc.support/src/org/argeo/slc/support/deploy/HttpdServerManager.java new file mode 100644 index 000000000..3fd76ed9d --- /dev/null +++ b/org.argeo.slc.support/src/org/argeo/slc/support/deploy/HttpdServerManager.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * 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.argeo.slc.support.deploy; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Arrays; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.argeo.slc.deploy.DeployedSystemManager; + +public class HttpdServerManager implements DeployedSystemManager { + private final static Log log = LogFactory.getLog(HttpdServerManager.class); + + private HttpdServer httpdServer; + + public void start() { + runProcessAsync(createCommandLine("start")); + log.info("Started httpd server with root " + + getHttpdServerTargetData().getServerRoot()); + } + + public void stop() { + runProcessAsync(createCommandLine("stop")); + log.info("Stopped httpd server with root " + + getHttpdServerTargetData().getServerRoot()); + } + + protected String[] createCommandLine(String action) { + String httpdPath = getHttpdServerTargetData().getExecutables() + .getExecutablePath("httpd"); + String[] cmd = { httpdPath, "-d", + getHttpdServerTargetData().getServerRoot(), "-f", + getHttpdServerDeploymentData().getConfigFile(), "-k", action }; + if (log.isDebugEnabled()) + log.debug("Command line: " + Arrays.asList(cmd)); + return cmd; + } + + protected static void runProcessAsync(String... command) { + ProcessBuilder procBuilder = new ProcessBuilder(command); + procBuilder.redirectErrorStream(true); + try { + Process proc = procBuilder.start(); + final InputStream in = proc.getInputStream(); + Thread logThread = new Thread() { + + @Override + public void run() { + BufferedReader reader = new BufferedReader( + new InputStreamReader(in)); + String line = null; + try { + while ((line = reader.readLine()) != null) + log.info(line); + } catch (IOException e) { + log.error("Failed to read stdout", e); + } + } + }; + + logThread.start(); + } catch (IOException e) { + throw new SlcException("Could not run command", e); + } + } + + public void setDeployedSystem(HttpdServer httpdServer) { + this.httpdServer = httpdServer; + } + + protected HttpdServerDeploymentData getHttpdServerDeploymentData() { + return (HttpdServerDeploymentData) httpdServer.getDeploymentData(); + } + + protected HttpdServerTargetData getHttpdServerTargetData() { + return (HttpdServerTargetData) httpdServer.getTargetData(); + } +}