2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.support
.deploy
;
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
;
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
;
30 public class HttpdServerManager
implements DeployedSystemManager
<HttpdServer
> {
31 private final static Log log
= LogFactory
.getLog(HttpdServerManager
.class);
33 private HttpdServer httpdServer
;
36 runProcessAsync(createCommandLine("start"));
37 log
.info("Started httpd server with root "
38 + getHttpdServerTargetData().getServerRoot());
42 runProcessAsync(createCommandLine("stop"));
43 log
.info("Stopped httpd server with root "
44 + getHttpdServerTargetData().getServerRoot());
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
));
58 protected static void runProcessAsync(String
... command
) {
59 ProcessBuilder procBuilder
= new ProcessBuilder(command
);
60 procBuilder
.redirectErrorStream(true);
62 Process proc
= procBuilder
.start();
63 final InputStream in
= proc
.getInputStream();
64 Thread logThread
= new Thread() {
68 BufferedReader reader
= new BufferedReader(
69 new InputStreamReader(in
));
72 while ((line
= reader
.readLine()) != null)
74 } catch (IOException e
) {
75 log
.error("Failed to read stdout", e
);
81 } catch (IOException e
) {
82 throw new SlcException("Could not run command", e
);
86 public void setDeployedSystem(HttpdServer httpdServer
) {
87 this.httpdServer
= httpdServer
;
90 protected HttpdServerDeploymentData
getHttpdServerDeploymentData() {
91 return (HttpdServerDeploymentData
) httpdServer
.getDeploymentData();
94 protected HttpdServerTargetData
getHttpdServerTargetData() {
95 return (HttpdServerTargetData
) httpdServer
.getTargetData();