]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/support/deploy/HttpdServerManager.java
Download process log
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / support / deploy / HttpdServerManager.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.support.deploy;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.util.Arrays;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.deploy.DeployedSystemManager;
29
30 public class HttpdServerManager implements DeployedSystemManager<HttpdServer> {
31 private final static Log log = LogFactory.getLog(HttpdServerManager.class);
32
33 private HttpdServer httpdServer;
34
35 public void start() {
36 runProcessAsync(createCommandLine("start"));
37 log.info("Started httpd server with root "
38 + getHttpdServerTargetData().getServerRoot());
39 }
40
41 public void stop() {
42 runProcessAsync(createCommandLine("stop"));
43 log.info("Stopped httpd server with root "
44 + getHttpdServerTargetData().getServerRoot());
45 }
46
47 protected String[] createCommandLine(String action) {
48 String httpdPath = getHttpdServerTargetData().getExecutables()
49 .getExecutablePath("httpd");
50 String[] cmd = { httpdPath, "-d",
51 getHttpdServerTargetData().getServerRoot(), "-f",
52 getHttpdServerDeploymentData().getConfigFile(), "-k", action };
53 if (log.isDebugEnabled())
54 log.debug("Command line: " + Arrays.asList(cmd));
55 return cmd;
56 }
57
58 protected static void runProcessAsync(String... command) {
59 ProcessBuilder procBuilder = new ProcessBuilder(command);
60 procBuilder.redirectErrorStream(true);
61 try {
62 Process proc = procBuilder.start();
63 final InputStream in = proc.getInputStream();
64 Thread logThread = new Thread() {
65
66 @Override
67 public void run() {
68 BufferedReader reader = new BufferedReader(
69 new InputStreamReader(in));
70 String line = null;
71 try {
72 while ((line = reader.readLine()) != null)
73 log.info(line);
74 } catch (IOException e) {
75 log.error("Failed to read stdout", e);
76 }
77 }
78 };
79
80 logThread.start();
81 } catch (IOException e) {
82 throw new SlcException("Could not run command", e);
83 }
84 }
85
86 public void setDeployedSystem(HttpdServer httpdServer) {
87 this.httpdServer = httpdServer;
88 }
89
90 protected HttpdServerDeploymentData getHttpdServerDeploymentData() {
91 return (HttpdServerDeploymentData) httpdServer.getDeploymentData();
92 }
93
94 protected HttpdServerTargetData getHttpdServerTargetData() {
95 return (HttpdServerTargetData) httpdServer.getTargetData();
96 }
97 }