View Javadoc
1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.common.deploy.appserver;
17  
18  import java.util.List;
19  import java.util.Properties;
20  
21  import org.kuali.common.deploy.DeployUtils;
22  import org.kuali.common.deploy.Deployable;
23  import org.kuali.common.util.FormatUtils;
24  import org.kuali.common.util.PropertyUtils;
25  import org.kuali.common.util.execute.Executable;
26  import org.kuali.common.util.log.LoggerLevel;
27  import org.kuali.common.util.secure.channel.Result;
28  import org.kuali.common.util.secure.channel.SecureChannel;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public class TomcatApplicationServer implements ApplicationServer {
33  
34  	private static final Logger logger = LoggerFactory.getLogger(TomcatApplicationServer.class);
35  
36  	SecureChannel channel;
37  	boolean validateShutdownExitValue = false;
38  	String username;
39  	String group;
40  	String shutdown;
41  	String startup;
42  	// Can be either files or dirs
43  	List<String> pathsToDelete;
44  	// Must be dirs
45  	List<String> dirsToCreate;
46  	// Can be either files or dirs
47  	List<String> pathsToChown;
48  	// Files that need to be transferred
49  	List<Deployable> deployables;
50  	// Properties used to filter deployables (if filter=true)
51  	Properties filterProperties;
52  	// If true, no files are transferred from local to remote
53  	boolean skipFiles;
54  	// The sign that tomcat has started up correctly is getting an HTTP 200 from an application url
55  	Executable httpWait;
56  
57  	@Override
58  	public void stop() {
59  		long start = System.currentTimeMillis();
60  		logger.info("[tomcat:stopping]");
61  		Result result = DeployUtils.runscript(channel, username, shutdown, false);
62  		if (result.getExitValue() != 0) {
63  			DeployUtils.logResult(result, logger, LoggerLevel.WARN);
64  		}
65  		logger.info("[tomcat:stopped] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
66  	}
67  
68  	@Override
69  	public void prepare() {
70  		long start = System.currentTimeMillis();
71  		logger.info("[tomcat:preparing]");
72  		// Remove old stuff (jdbc drivers, logs, applications, configuration files in /home/tomcat etc)
73  		DeployUtils.delete(channel, pathsToDelete);
74  		// Re-create directories that need to be there
75  		DeployUtils.mkdirs(channel, dirsToCreate);
76  		// Copy files to the remote server
77  		if (!skipFiles) {
78  			// Copy files from local to remote
79  			// TODO Get this hack out of here. The AppDynamics config sets a system property holding values for setenv.sh
80  			Properties properties = PropertyUtils.getGlobalProperties(filterProperties);
81  			DeployUtils.copyFiles(channel, deployables, properties);
82  		}
83  		// Make sure everything is owned by tomcat:tomcat
84  		DeployUtils.chown(channel, username, group, pathsToChown);
85  		logger.info("[tomcat:prepared] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
86  	}
87  
88  	@Override
89  	public void start() {
90  		long start = System.currentTimeMillis();
91  		logger.info("[tomcat:start]");
92  		DeployUtils.runscript(channel, username, startup);
93  		httpWait.execute();
94  		logger.info("[tomcat:started] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
95  	}
96  
97  	public boolean isValidateShutdownExitValue() {
98  		return validateShutdownExitValue;
99  	}
100 
101 	public void setValidateShutdownExitValue(boolean validateShutdownExitValue) {
102 		this.validateShutdownExitValue = validateShutdownExitValue;
103 	}
104 
105 	public SecureChannel getChannel() {
106 		return channel;
107 	}
108 
109 	public void setChannel(SecureChannel channel) {
110 		this.channel = channel;
111 	}
112 
113 	public String getUsername() {
114 		return username;
115 	}
116 
117 	public void setUsername(String username) {
118 		this.username = username;
119 	}
120 
121 	public String getShutdown() {
122 		return shutdown;
123 	}
124 
125 	public void setShutdown(String shutdown) {
126 		this.shutdown = shutdown;
127 	}
128 
129 	public String getStartup() {
130 		return startup;
131 	}
132 
133 	public void setStartup(String startup) {
134 		this.startup = startup;
135 	}
136 
137 	public String getGroup() {
138 		return group;
139 	}
140 
141 	public void setGroup(String group) {
142 		this.group = group;
143 	}
144 
145 	public List<String> getPathsToDelete() {
146 		return pathsToDelete;
147 	}
148 
149 	public void setPathsToDelete(List<String> pathsToDelete) {
150 		this.pathsToDelete = pathsToDelete;
151 	}
152 
153 	public List<String> getDirsToCreate() {
154 		return dirsToCreate;
155 	}
156 
157 	public void setDirsToCreate(List<String> dirsToCreate) {
158 		this.dirsToCreate = dirsToCreate;
159 	}
160 
161 	public List<String> getPathsToChown() {
162 		return pathsToChown;
163 	}
164 
165 	public void setPathsToChown(List<String> pathsToChown) {
166 		this.pathsToChown = pathsToChown;
167 	}
168 
169 	public List<Deployable> getDeployables() {
170 		return deployables;
171 	}
172 
173 	public void setDeployables(List<Deployable> deployables) {
174 		this.deployables = deployables;
175 	}
176 
177 	public boolean isSkipFiles() {
178 		return skipFiles;
179 	}
180 
181 	public void setSkipFiles(boolean skipFiles) {
182 		this.skipFiles = skipFiles;
183 	}
184 
185 	public Properties getFilterProperties() {
186 		return filterProperties;
187 	}
188 
189 	public void setFilterProperties(Properties filterProperties) {
190 		this.filterProperties = filterProperties;
191 	}
192 
193 	public Executable getHttpWait() {
194 		return httpWait;
195 	}
196 
197 	public void setHttpWait(Executable httpWait) {
198 		this.httpWait = httpWait;
199 	}
200 
201 }