]> 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
Refactor package names
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / support / deploy / HttpdServerManager.java
1 package org.argeo.slc.support.deploy;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.Arrays;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.deploy.DeployedSystemManager;
13
14 public class HttpdServerManager implements DeployedSystemManager<HttpdServer> {
15 private final static Log log = LogFactory.getLog(HttpdServerManager.class);
16
17 private HttpdServer httpdServer;
18
19 public void start() {
20 runProcessAsync(createCommandLine("start"));
21 log.info("Started httpd server with root "
22 + getHttpdServerTargetData().getServerRoot());
23 }
24
25 public void stop() {
26 runProcessAsync(createCommandLine("stop"));
27 log.info("Stopped httpd server with root "
28 + getHttpdServerTargetData().getServerRoot());
29 }
30
31 protected String[] createCommandLine(String action) {
32 String httpdPath = getHttpdServerTargetData().getExecutables()
33 .getExecutablePath("httpd");
34 String[] cmd = { httpdPath, "-d",
35 getHttpdServerTargetData().getServerRoot(), "-f",
36 getHttpdServerDeploymentData().getConfigFile(), "-k", action };
37 if (log.isDebugEnabled())
38 log.debug("Command line: " + Arrays.asList(cmd));
39 return cmd;
40 }
41
42 protected static void runProcessAsync(String... command) {
43 ProcessBuilder procBuilder = new ProcessBuilder(command);
44 procBuilder.redirectErrorStream(true);
45 try {
46 Process proc = procBuilder.start();
47 final InputStream in = proc.getInputStream();
48 Thread logThread = new Thread() {
49
50 @Override
51 public void run() {
52 BufferedReader reader = new BufferedReader(
53 new InputStreamReader(in));
54 String line = null;
55 try {
56 while ((line = reader.readLine()) != null)
57 log.info(line);
58 } catch (IOException e) {
59 log.error("Failed to read stdout", e);
60 }
61 }
62 };
63
64 logThread.start();
65 } catch (IOException e) {
66 throw new SlcException("Could not run command", e);
67 }
68 }
69
70 public void setDeployedSystem(HttpdServer httpdServer) {
71 this.httpdServer = httpdServer;
72 }
73
74 protected HttpdServerDeploymentData getHttpdServerDeploymentData() {
75 return (HttpdServerDeploymentData) httpdServer.getDeploymentData();
76 }
77
78 protected HttpdServerTargetData getHttpdServerTargetData() {
79 return (HttpdServerTargetData) httpdServer.getTargetData();
80 }
81 }