View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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  package org.argeo.slc.support.deploy;
17  
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;
23  
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;
28  
29  public class HttpdServerManager implements DeployedSystemManager<HttpdServer> {
30  	private final static Log log = LogFactory.getLog(HttpdServerManager.class);
31  
32  	private HttpdServer httpdServer;
33  
34  	public void start() {
35  		runProcessAsync(createCommandLine("start"));
36  		log.info("Started httpd server with root "
37  				+ getHttpdServerTargetData().getServerRoot());
38  	}
39  
40  	public void stop() {
41  		runProcessAsync(createCommandLine("stop"));
42  		log.info("Stopped httpd server with root "
43  				+ getHttpdServerTargetData().getServerRoot());
44  	}
45  
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));
54  		return cmd;
55  	}
56  
57  	protected static void runProcessAsync(String... command) {
58  		ProcessBuilder procBuilder = new ProcessBuilder(command);
59  		procBuilder.redirectErrorStream(true);
60  		try {
61  			Process proc = procBuilder.start();
62  			final InputStream in = proc.getInputStream();
63  			Thread logThread = new Thread() {
64  
65  				@Override
66  				public void run() {
67  					BufferedReader reader = new BufferedReader(
68  							new InputStreamReader(in));
69  					String line = null;
70  					try {
71  						while ((line = reader.readLine()) != null)
72  							log.info(line);
73  					} catch (IOException e) {
74  						log.error("Failed to read stdout", e);
75  					}
76  				}
77  			};
78  
79  			logThread.start();
80  		} catch (IOException e) {
81  			throw new SlcException("Could not run command", e);
82  		}
83  	}
84  
85  	public void setDeployedSystem(HttpdServer httpdServer) {
86  		this.httpdServer = httpdServer;
87  	}
88  
89  	protected HttpdServerDeploymentData getHttpdServerDeploymentData() {
90  		return (HttpdServerDeploymentData) httpdServer.getDeploymentData();
91  	}
92  
93  	protected HttpdServerTargetData getHttpdServerTargetData() {
94  		return (HttpdServerTargetData) httpdServer.getTargetData();
95  	}
96  }