001: /*
002: * Tomcat: The deployer for the tomcat daemon
003: * Copyright (C) 2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * Tomcat.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.tomcat;
024:
025: // java imports
026: import java.io.File;
027: import java.util.Arrays;
028: import java.util.Map;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Vector;
032: import com.rift.coad.daemon.tomcat.TomcatWrapper;
033:
034: // logging import
035: import org.apache.log4j.Logger;
036:
037: // coadunation imports
038: import com.rift.coad.lib.common.FileUtil;
039: import com.rift.coad.lib.thread.BasicThread;
040: import com.rift.coad.lib.thread.ThreadStateMonitor;
041: import com.rift.coad.lib.configuration.ConfigurationFactory;
042: import com.rift.coad.lib.configuration.Configuration;
043:
044: /**
045: * This class is responsible for deploying the tomcat daemon instance.
046: *
047: * @author Brett Chaldecott
048: */
049: public class TomcatDeployer extends BasicThread {
050:
051: /**
052: * This object contains the deployment information for a given entry.
053: */
054: public class DeployEntry {
055: // private member variables
056: private String path = null;
057: private File file = null;
058: private long lastModified = 0;
059:
060: /**
061: * The constructor of the deploy entry.
062: *
063: * @param file The reference to the file object.
064: */
065: public DeployEntry(File file) {
066: path = file.getPath();
067: this .file = file;
068: lastModified = file.lastModified();
069: }
070:
071: /**
072: * The reference to the path entries.
073: *
074: * @return The reference to the path value.
075: */
076: public String getPath() {
077: return path;
078: }
079:
080: /**
081: * This method returns a reference to h
082: */
083: public File getFile() {
084: return file;
085: }
086:
087: /**
088: * This method returns the last modified date.
089: *
090: * @return The long containing the last modified date.
091: */
092: public long getLastModified() {
093: return lastModified;
094: }
095: }
096:
097: // the logger reference
098: protected Logger log = Logger.getLogger(TomcatDeployer.class
099: .getName());
100:
101: // private member variables
102: private ThreadStateMonitor state = null;
103: private TomcatWrapper tomcatWrapper = null;
104: private Map deployedEntries = new HashMap();
105: private File deploymentDir = null;
106:
107: /**
108: * Creates a new instance of TomcatDeployer
109: */
110: public TomcatDeployer() throws Exception {
111: try {
112: Configuration configuration = ConfigurationFactory
113: .getInstance().getConfig(this .getClass());
114: state = new ThreadStateMonitor(configuration.getLong(
115: "DeployCheckInterval", 1000));
116: Configuration deployConfig = ConfigurationFactory
117: .getInstance()
118: .getConfig(
119: com.rift.coad.lib.deployment.DeploymentManager.class);
120: deploymentDir = new File(deployConfig
121: .getString("directory"));
122: tomcatWrapper = new TomcatWrapper();
123:
124: } catch (Exception ex) {
125: log.error(
126: "Failed to initialize the configuration because : "
127: + ex.getMessage(), ex);
128: throw new TomcatException(
129: "Failed to initialize the configuration because : "
130: + ex.getMessage(), ex);
131: }
132: }
133:
134: /**
135: * This method will be called to perform the processing. This method
136: * replaces the traditional run method.
137: */
138: public void process() throws Exception {
139: log.info("The processor is running");
140: while (!state.isTerminated()) {
141: // retrieve a list of files
142: File[] files = FileUtil.filter(deploymentDir.listFiles(),
143: ".war");
144: Arrays.sort(files);
145:
146: // loop through the existing entry
147: Vector oldEntries = new Vector();
148: for (Iterator iter = deployedEntries.keySet().iterator(); iter
149: .hasNext();) {
150: DeployEntry entry = (DeployEntry) deployedEntries
151: .get(iter.next());
152: boolean found = false;
153: for (int index = 0; index < files.length; index++) {
154: if (files[index].getPath().equals(entry.getPath())) {
155: if (files[index].lastModified() == entry
156: .getLastModified()) {
157: found = true;
158: }
159: break;
160: }
161: }
162: if (!found) {
163: oldEntries.add(entry.getPath());
164: tomcatWrapper.unloadWar(entry.getFile());
165: }
166: }
167: for (Iterator iter = oldEntries.iterator(); iter.hasNext();) {
168: deployedEntries.remove(iter.next());
169: }
170:
171: // attempt to load
172: for (int index = 0; index < files.length; index++) {
173: if (!deployedEntries
174: .containsKey(files[index].getPath())) {
175: deployedEntries.put(files[index].getPath(),
176: new DeployEntry(files[index]));
177: tomcatWrapper.loadWar(files[index]);
178: }
179: }
180:
181: // check the state
182: state.monitor();
183: }
184:
185: log.info("Stopping the engine");
186: try {
187: tomcatWrapper.stop();
188: } catch (Exception ex) {
189: log.error("Failed to stop the tomcat instance : "
190: + ex.getMessage(), ex);
191: }
192:
193: }
194:
195: /**
196: * This method is called to soft terminate the processing thread.
197: */
198: public void terminate() {
199: state.terminate(true);
200: try {
201: this .join();
202: } catch (Exception ex) {
203: log.error("Failed to wait for tomcat :" + ex.getMessage(),
204: ex);
205: }
206: log.info("Finished waiting for tomcat to shut down.");
207: }
208:
209: }
|