001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.deploy.appserver;
017    
018    import java.util.List;
019    import java.util.Properties;
020    
021    import org.kuali.common.deploy.DeployUtils;
022    import org.kuali.common.deploy.Deployable;
023    import org.kuali.common.util.FormatUtils;
024    import org.kuali.common.util.PropertyUtils;
025    import org.kuali.common.util.execute.Executable;
026    import org.kuali.common.util.log.LoggerLevel;
027    import org.kuali.common.util.secure.channel.Result;
028    import org.kuali.common.util.secure.channel.SecureChannel;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    public class TomcatApplicationServer implements ApplicationServer {
033    
034            private static final Logger logger = LoggerFactory.getLogger(TomcatApplicationServer.class);
035    
036            SecureChannel channel;
037            boolean validateShutdownExitValue = false;
038            String username;
039            String group;
040            String shutdown;
041            String startup;
042            // Can be either files or dirs
043            List<String> pathsToDelete;
044            // Must be dirs
045            List<String> dirsToCreate;
046            // Can be either files or dirs
047            List<String> pathsToChown;
048            // Files that need to be transferred
049            List<Deployable> deployables;
050            // Properties used to filter deployables (if filter=true)
051            Properties filterProperties;
052            // If true, no files are transferred from local to remote
053            boolean skipFiles;
054            // The sign that tomcat has started up correctly is getting an HTTP 200 from an application url
055            Executable httpWait;
056    
057            @Override
058            public void stop() {
059                    long start = System.currentTimeMillis();
060                    logger.info("[tomcat:stopping]");
061                    Result result = DeployUtils.runscript(channel, username, shutdown, false);
062                    if (result.getExitValue() != 0) {
063                            DeployUtils.logResult(result, logger, LoggerLevel.WARN);
064                    }
065                    logger.info("[tomcat:stopped] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
066            }
067    
068            @Override
069            public void prepare() {
070                    long start = System.currentTimeMillis();
071                    logger.info("[tomcat:preparing]");
072                    // Remove old stuff (jdbc drivers, logs, applications, configuration files in /home/tomcat etc)
073                    DeployUtils.delete(channel, pathsToDelete);
074                    // Re-create directories that need to be there
075                    DeployUtils.mkdirs(channel, dirsToCreate);
076                    // Copy files to the remote server
077                    if (!skipFiles) {
078                            // Copy files from local to remote
079                            // TODO Get this hack out of here. The AppDynamics config sets a system property holding values for setenv.sh
080                            Properties properties = PropertyUtils.getGlobalProperties(filterProperties);
081                            DeployUtils.copyFiles(channel, deployables, properties);
082                    }
083                    // Make sure everything is owned by tomcat:tomcat
084                    DeployUtils.chown(channel, username, group, pathsToChown);
085                    logger.info("[tomcat:prepared] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
086            }
087    
088            @Override
089            public void start() {
090                    long start = System.currentTimeMillis();
091                    logger.info("[tomcat:start]");
092                    DeployUtils.runscript(channel, username, startup);
093                    httpWait.execute();
094                    logger.info("[tomcat:started] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
095            }
096    
097            public boolean isValidateShutdownExitValue() {
098                    return validateShutdownExitValue;
099            }
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    }