001
014
015 package com.liferay.portal.kernel.deploy.auto;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.IntegerWrapper;
020
021 import java.io.File;
022
023 import java.util.HashMap;
024 import java.util.HashSet;
025 import java.util.List;
026 import java.util.Map;
027 import java.util.Set;
028 import java.util.concurrent.CopyOnWriteArrayList;
029
030
034 public class AutoDeployDir {
035
036 public static final String DEFAULT_NAME = "defaultAutoDeployDir";
037
038 public AutoDeployDir(
039 String name, File deployDir, File destDir, long interval,
040 int blacklistThreshold, List<AutoDeployListener> autoDeployListeners) {
041
042 _name = name;
043 _deployDir = deployDir;
044 _destDir = destDir;
045 _interval = interval;
046 _blacklistThreshold = blacklistThreshold;
047 _autoDeployListeners = new CopyOnWriteArrayList<AutoDeployListener>(
048 autoDeployListeners);
049 _inProcessFiles = new HashMap<String, IntegerWrapper>();
050 _blacklistFiles = new HashSet<String>();
051 }
052
053 public int getBlacklistThreshold() {
054 return _blacklistThreshold;
055 }
056
057 public File getDeployDir() {
058 return _deployDir;
059 }
060
061 public File getDestDir() {
062 return _destDir;
063 }
064
065 public long getInterval() {
066 return _interval;
067 }
068
069 public List<AutoDeployListener> getListeners() {
070 return _autoDeployListeners;
071 }
072
073 public String getName() {
074 return _name;
075 }
076
077 public void registerListener(AutoDeployListener listener) {
078 _autoDeployListeners.add(listener);
079 }
080
081 public void start() {
082 if (!_deployDir.exists()) {
083 if (_log.isInfoEnabled()) {
084 _log.info("Creating missing directory " + _deployDir);
085 }
086
087 boolean created = _deployDir.mkdirs();
088
089 if (!created) {
090 _log.error("Directory " + _deployDir + " could not be created");
091 }
092 }
093
094 if (_interval > 0) {
095 try {
096 Thread currentThread = Thread.currentThread();
097
098 _autoDeployScanner = new AutoDeployScanner(
099 currentThread.getThreadGroup(),
100 AutoDeployScanner.class.getName(), this);
101
102 _autoDeployScanner.start();
103
104 if (_log.isInfoEnabled()) {
105 _log.info("Auto deploy scanner started for " + _deployDir);
106 }
107 }
108 catch (Exception e) {
109 _log.error(e, e);
110
111 stop();
112
113 return;
114 }
115 }
116 else {
117 if (_log.isInfoEnabled()) {
118 _log.info("Auto deploy scanning is disabled for " + _deployDir);
119 }
120 }
121 }
122
123 public void stop() {
124 if (_autoDeployScanner != null) {
125 _autoDeployScanner.pause();
126 }
127 }
128
129 public void unregisterListener(AutoDeployListener autoDeployListener) {
130 _autoDeployListeners.remove(autoDeployListener);
131 }
132
133 protected void processFile(File file) {
134 String fileName = file.getName();
135
136 if (!file.canRead()) {
137 _log.error("Unable to read " + fileName);
138
139 return;
140 }
141
142 if (!file.canWrite()) {
143 _log.error("Unable to write " + fileName);
144
145 return;
146 }
147
148 if (_blacklistFiles.contains(fileName)) {
149 if (_log.isDebugEnabled()) {
150 _log.debug(
151 "Skip processing of " + fileName + " because it is " +
152 "blacklisted. You must restart the server to remove " +
153 "the file from the blacklist.");
154 }
155
156 return;
157 }
158
159 IntegerWrapper attempt = _inProcessFiles.get(fileName);
160
161 if (attempt == null) {
162 attempt = new IntegerWrapper(1);
163
164 _inProcessFiles.put(fileName, attempt);
165
166 if (_log.isInfoEnabled()) {
167 _log.info("Processing " + fileName);
168 }
169 }
170 else {
171 if (_log.isInfoEnabled()) {
172 _log.info(
173 "Processing " + fileName + ". This is attempt " +
174 attempt.getValue() + ".");
175 }
176 }
177
178 try {
179 for (AutoDeployListener autoDeployListener : _autoDeployListeners) {
180 autoDeployListener.deploy(file);
181 }
182
183 if (file.delete()) {
184 _inProcessFiles.remove(fileName);
185 }
186 else {
187 _log.error("Auto deploy failed to remove " + fileName);
188
189 if (_log.isInfoEnabled()) {
190 _log.info("Add " + fileName + " to the blacklist");
191 }
192
193 _blacklistFiles.add(fileName);
194 }
195 }
196 catch (Exception e) {
197 _log.error(e, e);
198
199 attempt.increment();
200
201 if (attempt.getValue() >= _blacklistThreshold) {
202 if (_log.isInfoEnabled()) {
203 _log.info("Add " + fileName + " to the blacklist");
204 }
205
206 _blacklistFiles.add(fileName);
207 }
208 }
209 }
210
211 protected void scanDirectory() {
212 File[] files = _deployDir.listFiles();
213
214 for (File file : files) {
215 String fileName = file.getName().toLowerCase();
216
217 if ((file.isFile()) &&
218 (fileName.endsWith(".war") || fileName.endsWith(".zip") ||
219 fileName.endsWith(".xml"))) {
220
221 processFile(file);
222 }
223 }
224 }
225
226 private static Log _log = LogFactoryUtil.getLog(AutoDeployDir.class);
227
228 private List<AutoDeployListener> _autoDeployListeners;
229 private AutoDeployScanner _autoDeployScanner;
230 private Set<String> _blacklistFiles;
231 private int _blacklistThreshold;
232 private File _deployDir;
233 private File _destDir;
234 private Map<String, IntegerWrapper> _inProcessFiles;
235 private long _interval;
236 private String _name;
237
238 }