--- /dev/null
+/*
+ * 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<HttpdServer> {
+ 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();
+ }
+}