001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.deploy.auto;
016    
017    import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.IntegerWrapper;
021    
022    import java.io.File;
023    
024    import java.util.HashMap;
025    import java.util.HashSet;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    import java.util.concurrent.CopyOnWriteArrayList;
030    
031    /**
032     * @author Ivica Cardic
033     * @author Brian Wing Shun Chan
034     */
035    public class AutoDeployDir {
036    
037            public static final String DEFAULT_NAME = "defaultAutoDeployDir";
038    
039            public AutoDeployDir(
040                    String name, File deployDir, File destDir, long interval,
041                    int blacklistThreshold, List<AutoDeployListener> autoDeployListeners) {
042    
043                    _name = name;
044                    _deployDir = deployDir;
045                    _destDir = destDir;
046                    _interval = interval;
047                    _blacklistThreshold = blacklistThreshold;
048                    _autoDeployListeners = new CopyOnWriteArrayList<AutoDeployListener>(
049                            autoDeployListeners);
050                    _inProcessFiles = new HashMap<String, IntegerWrapper>();
051                    _blacklistFiles = new HashSet<String>();
052            }
053    
054            public int getBlacklistThreshold() {
055                    return _blacklistThreshold;
056            }
057    
058            public File getDeployDir() {
059                    return _deployDir;
060            }
061    
062            public File getDestDir() {
063                    return _destDir;
064            }
065    
066            public long getInterval() {
067                    return _interval;
068            }
069    
070            public List<AutoDeployListener> getListeners() {
071                    return _autoDeployListeners;
072            }
073    
074            public String getName() {
075                    return _name;
076            }
077    
078            public void registerListener(AutoDeployListener listener) {
079                    _autoDeployListeners.add(listener);
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 + " could not be created");
092                            }
093                    }
094    
095                    if (_interval > 0) {
096                            try {
097                                    Thread currentThread = Thread.currentThread();
098    
099                                    _autoDeployScanner = new AutoDeployScanner(
100                                            currentThread.getThreadGroup(),
101                                            AutoDeployScanner.class.getName(), this);
102    
103                                    _autoDeployScanner.start();
104    
105                                    if (_log.isInfoEnabled()) {
106                                            _log.info("Auto deploy scanner started for " + _deployDir);
107                                    }
108                            }
109                            catch (Exception e) {
110                                    _log.error(e, e);
111    
112                                    stop();
113    
114                                    return;
115                            }
116                    }
117                    else {
118                            if (_log.isInfoEnabled()) {
119                                    _log.info("Auto deploy scanning is disabled for " + _deployDir);
120                            }
121                    }
122            }
123    
124            public void stop() {
125                    if (_autoDeployScanner != null) {
126                            _autoDeployScanner.pause();
127                    }
128            }
129    
130            public void unregisterListener(AutoDeployListener autoDeployListener) {
131                    _autoDeployListeners.remove(autoDeployListener);
132            }
133    
134            protected AutoDeploymentContext buildAutoDeploymentContext(File file) {
135                    AutoDeploymentContext autoDeploymentContext =
136                            new AutoDeploymentContext();
137    
138                    autoDeploymentContext.setFile(file);
139    
140                    return autoDeploymentContext;
141            }
142    
143            protected void processFile(File file) {
144                    String fileName = file.getName();
145    
146                    if (!file.canRead()) {
147                            _log.error("Unable to read " + fileName);
148    
149                            return;
150                    }
151    
152                    if (!file.canWrite()) {
153                            _log.error("Unable to write " + fileName);
154    
155                            return;
156                    }
157    
158                    if (_blacklistFiles.contains(fileName)) {
159                            if (_log.isDebugEnabled()) {
160                                    _log.debug(
161                                            "Skip processing of " + fileName + " because it is " +
162                                                    "blacklisted. You must restart the server to remove " +
163                                                            "the file from the blacklist.");
164                            }
165    
166                            return;
167                    }
168    
169                    IntegerWrapper attempt = _inProcessFiles.get(fileName);
170    
171                    if (attempt == null) {
172                            attempt = new IntegerWrapper(1);
173    
174                            _inProcessFiles.put(fileName, attempt);
175    
176                            if (_log.isInfoEnabled()) {
177                                    _log.info("Processing " + fileName);
178                            }
179                    }
180                    else {
181                            if (_log.isInfoEnabled()) {
182                                    _log.info(
183                                            "Processing " + fileName + ". This is attempt " +
184                                                    attempt.getValue() + ".");
185                            }
186                    }
187    
188                    try {
189                            AutoDeploymentContext autoDeploymentContext =
190                                    buildAutoDeploymentContext(file);
191    
192                            for (AutoDeployListener autoDeployListener : _autoDeployListeners) {
193                                    autoDeployListener.deploy(autoDeploymentContext);
194                            }
195    
196                            if (file.delete()) {
197                                    _inProcessFiles.remove(fileName);
198                            }
199                            else {
200                                    _log.error("Auto deploy failed to remove " + fileName);
201    
202                                    if (_log.isInfoEnabled()) {
203                                            _log.info("Add " + fileName + " to the blacklist");
204                                    }
205    
206                                    _blacklistFiles.add(fileName);
207                            }
208                    }
209                    catch (Exception e) {
210                            _log.error(e, e);
211    
212                            attempt.increment();
213    
214                            if (attempt.getValue() >= _blacklistThreshold) {
215                                    if (_log.isInfoEnabled()) {
216                                            _log.info("Add " + fileName + " to the blacklist");
217                                    }
218    
219                                    _blacklistFiles.add(fileName);
220                            }
221                    }
222            }
223    
224            protected void scanDirectory() {
225                    File[] files = _deployDir.listFiles();
226    
227                    for (File file : files) {
228                            String fileName = file.getName().toLowerCase();
229    
230                            if (file.isFile() &&
231                                    (fileName.endsWith(".jar") || fileName.endsWith(".lpkg") ||
232                                     fileName.endsWith(".war") || fileName.endsWith(".xml") ||
233                                     fileName.endsWith(".zip"))) {
234    
235                                    processFile(file);
236                            }
237                    }
238            }
239    
240            private static Log _log = LogFactoryUtil.getLog(AutoDeployDir.class);
241    
242            private List<AutoDeployListener> _autoDeployListeners;
243            private AutoDeployScanner _autoDeployScanner;
244            private Set<String> _blacklistFiles;
245            private int _blacklistThreshold;
246            private File _deployDir;
247            private File _destDir;
248            private Map<String, IntegerWrapper> _inProcessFiles;
249            private long _interval;
250            private String _name;
251    
252    }