2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.support
.deploy
;
18 import java
.io
.BufferedReader
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.io
.InputStreamReader
;
22 import java
.util
.Arrays
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.slc
.SlcException
;
27 import org
.argeo
.slc
.deploy
.DeployedSystemManager
;
29 public class HttpdServerManager
implements DeployedSystemManager
<HttpdServer
> {
30 private final static Log log
= LogFactory
.getLog(HttpdServerManager
.class);
32 private HttpdServer httpdServer
;
35 runProcessAsync(createCommandLine("start"));
36 log
.info("Started httpd server with root "
37 + getHttpdServerTargetData().getServerRoot());
41 runProcessAsync(createCommandLine("stop"));
42 log
.info("Stopped httpd server with root "
43 + getHttpdServerTargetData().getServerRoot());
46 protected String
[] createCommandLine(String action
) {
47 String httpdPath
= getHttpdServerTargetData().getExecutables()
48 .getExecutablePath("httpd");
49 String
[] cmd
= { httpdPath
, "-d",
50 getHttpdServerTargetData().getServerRoot(), "-f",
51 getHttpdServerDeploymentData().getConfigFile(), "-k", action
};
52 if (log
.isDebugEnabled())
53 log
.debug("Command line: " + Arrays
.asList(cmd
));
57 protected static void runProcessAsync(String
... command
) {
58 ProcessBuilder procBuilder
= new ProcessBuilder(command
);
59 procBuilder
.redirectErrorStream(true);
61 Process proc
= procBuilder
.start();
62 final InputStream in
= proc
.getInputStream();
63 Thread logThread
= new Thread() {
67 BufferedReader reader
= new BufferedReader(
68 new InputStreamReader(in
));
71 while ((line
= reader
.readLine()) != null)
73 } catch (IOException e
) {
74 log
.error("Failed to read stdout", e
);
80 } catch (IOException e
) {
81 throw new SlcException("Could not run command", e
);
85 public void setDeployedSystem(HttpdServer httpdServer
) {
86 this.httpdServer
= httpdServer
;
89 protected HttpdServerDeploymentData
getHttpdServerDeploymentData() {
90 return (HttpdServerDeploymentData
) httpdServer
.getDeploymentData();
93 protected HttpdServerTargetData
getHttpdServerTargetData() {
94 return (HttpdServerTargetData
) httpdServer
.getTargetData();