001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.deploy.auto;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.util.IntegerWrapper;
026:
027: import java.io.File;
028:
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Set;
035:
036: /**
037: * <a href="AutoDeployDir.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Ivica Cardic
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class AutoDeployDir {
044:
045: public AutoDeployDir(String name, File deployDir, File destDir,
046: long interval, int blacklistThreshold, List listeners) {
047:
048: _name = name;
049: _deployDir = deployDir;
050: _destDir = destDir;
051: _interval = interval;
052: _blacklistThreshold = blacklistThreshold;
053: _listeners = listeners;
054: _inProcessFiles = new HashMap();
055: _blacklistFiles = new HashSet();
056: }
057:
058: public String getName() {
059: return _name;
060: }
061:
062: public File getDeployDir() {
063: return _deployDir;
064: }
065:
066: public File getDestDir() {
067: return _destDir;
068: }
069:
070: public long getInterval() {
071: return _interval;
072: }
073:
074: public int getBlacklistThreshold() {
075: return _blacklistThreshold;
076: }
077:
078: public List getListeners() {
079: return _listeners;
080: }
081:
082: public void start() {
083: if (!_deployDir.exists()) {
084: if (_log.isInfoEnabled()) {
085: _log.info("Creating missing directory " + _deployDir);
086: }
087:
088: boolean created = _deployDir.mkdirs();
089:
090: if (!created) {
091: _log.error("Directory " + _deployDir
092: + " could not be created");
093: }
094: }
095:
096: if (_interval > 0) {
097: try {
098: _scanner = new AutoDeployScanner(Thread.currentThread()
099: .getThreadGroup(), AutoDeployScanner.class
100: .getName(), this );
101:
102: _scanner.start();
103:
104: if (_log.isInfoEnabled()) {
105: _log.info("Auto deploy scanner started for "
106: + _deployDir);
107: }
108: } catch (Exception e) {
109: _log.error(e, e);
110:
111: stop();
112:
113: return;
114: }
115: } else {
116: if (_log.isInfoEnabled()) {
117: _log.info("Auto deploy scanning is disabled for "
118: + _deployDir);
119: }
120: }
121: }
122:
123: public void stop() {
124: if (_scanner != null) {
125: _scanner.pause();
126: }
127: }
128:
129: protected void scanDirectory() {
130: File[] files = _deployDir.listFiles();
131:
132: for (int i = 0; i < files.length; i++) {
133: File file = files[i];
134:
135: String fileName = file.getName().toLowerCase();
136:
137: if ((file.isFile())
138: && (fileName.endsWith(".war")
139: || fileName.endsWith(".zip") || fileName
140: .endsWith(".xml"))) {
141:
142: processFile(file);
143: }
144: }
145: }
146:
147: protected void processFile(File file) {
148: String fileName = file.getName();
149:
150: if (!file.canRead()) {
151: _log.error("Unable to read " + fileName);
152:
153: return;
154: }
155:
156: if (!file.canWrite()) {
157: _log.error("Unable to write " + fileName);
158:
159: return;
160: }
161:
162: if (_blacklistFiles.contains(fileName)) {
163: if (_log.isDebugEnabled()) {
164: _log
165: .debug("Skip processing of "
166: + fileName
167: + " because it is "
168: + "blacklisted. You must restart the server to remove "
169: + "the file from the blacklist.");
170: }
171:
172: return;
173: }
174:
175: IntegerWrapper attempt = (IntegerWrapper) _inProcessFiles
176: .get(fileName);
177:
178: if (attempt == null) {
179: attempt = new IntegerWrapper(1);
180:
181: _inProcessFiles.put(fileName, attempt);
182:
183: if (_log.isInfoEnabled()) {
184: _log.info("Processing " + fileName);
185: }
186: } else {
187: if (_log.isInfoEnabled()) {
188: _log.info("Processing " + fileName
189: + ". This is attempt " + attempt.getValue()
190: + ".");
191: }
192: }
193:
194: try {
195: Iterator itr = _listeners.iterator();
196:
197: while (itr.hasNext()) {
198: AutoDeployListener listener = (AutoDeployListener) itr
199: .next();
200:
201: listener.deploy(file);
202: }
203:
204: if (file.delete()) {
205: _inProcessFiles.remove(fileName);
206: } else {
207: _log.error("Auto deploy failed to remove " + fileName);
208:
209: if (_log.isInfoEnabled()) {
210: _log.info("Add " + fileName + " to the blacklist");
211: }
212:
213: _blacklistFiles.add(fileName);
214: }
215: } catch (Exception e) {
216: _log.error(e, e);
217:
218: attempt.increment();
219:
220: if (attempt.getValue() >= _blacklistThreshold) {
221: if (_log.isInfoEnabled()) {
222: _log.info("Add " + fileName + " to the blacklist");
223: }
224:
225: _blacklistFiles.add(fileName);
226: }
227: }
228: }
229:
230: private static Log _log = LogFactoryUtil
231: .getLog(AutoDeployDir.class);
232:
233: private String _name;
234: private File _deployDir;
235: private File _destDir;
236: private long _interval;
237: private int _blacklistThreshold;
238: private List _listeners;
239: private Map _inProcessFiles;
240: private Set _blacklistFiles;
241: private AutoDeployScanner _scanner;
242:
243: }
|