1
22
23 package com.liferay.portal.kernel.deploy.auto;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.IntegerWrapper;
28
29 import java.io.File;
30
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37
43 public class AutoDeployDir {
44
45 public AutoDeployDir(
46 String name, File deployDir, File destDir, long interval,
47 int blacklistThreshold, List<AutoDeployListener> listeners) {
48
49 _name = name;
50 _deployDir = deployDir;
51 _destDir = destDir;
52 _interval = interval;
53 _blacklistThreshold = blacklistThreshold;
54 _listeners = listeners;
55 _inProcessFiles = new HashMap<String, IntegerWrapper>();
56 _blacklistFiles = new HashSet<String>();
57 }
58
59 public String getName() {
60 return _name;
61 }
62
63 public File getDeployDir() {
64 return _deployDir;
65 }
66
67 public File getDestDir() {
68 return _destDir;
69 }
70
71 public long getInterval() {
72 return _interval;
73 }
74
75 public int getBlacklistThreshold() {
76 return _blacklistThreshold;
77 }
78
79 public List<AutoDeployListener> getListeners() {
80 return _listeners;
81 }
82
83 public void start() {
84 if (!_deployDir.exists()) {
85 if (_log.isInfoEnabled()) {
86 _log.info("Creating missing directory " + _deployDir);
87 }
88
89 boolean created = _deployDir.mkdirs();
90
91 if (!created) {
92 _log.error("Directory " + _deployDir + " could not be created");
93 }
94 }
95
96 if (_interval > 0) {
97 try {
98 Thread currentThread = Thread.currentThread();
99
100 _scanner = new AutoDeployScanner(
101 currentThread.getThreadGroup(),
102 AutoDeployScanner.class.getName(), this);
103
104 _scanner.start();
105
106 if (_log.isInfoEnabled()) {
107 _log.info("Auto deploy scanner started for " + _deployDir);
108 }
109 }
110 catch (Exception e) {
111 _log.error(e, e);
112
113 stop();
114
115 return;
116 }
117 }
118 else {
119 if (_log.isInfoEnabled()) {
120 _log.info("Auto deploy scanning is disabled for " + _deployDir);
121 }
122 }
123 }
124
125 public void stop() {
126 if (_scanner != null) {
127 _scanner.pause();
128 }
129 }
130
131 protected void scanDirectory() {
132 File[] files = _deployDir.listFiles();
133
134 for (int i = 0; i < files.length; i++) {
135 File file = files[i];
136
137 String fileName = file.getName().toLowerCase();
138
139 if ((file.isFile()) &&
140 (fileName.endsWith(".war") || fileName.endsWith(".zip") ||
141 fileName.endsWith(".xml"))) {
142
143 processFile(file);
144 }
145 }
146 }
147
148 protected void processFile(File file) {
149 String fileName = file.getName();
150
151 if (!file.canRead()) {
152 _log.error("Unable to read " + fileName);
153
154 return;
155 }
156
157 if (!file.canWrite()) {
158 _log.error("Unable to write " + fileName);
159
160 return;
161 }
162
163 if (_blacklistFiles.contains(fileName)) {
164 if (_log.isDebugEnabled()) {
165 _log.debug(
166 "Skip processing of " + fileName + " because it is " +
167 "blacklisted. You must restart the server to remove " +
168 "the file from the blacklist.");
169 }
170
171 return;
172 }
173
174 IntegerWrapper attempt = _inProcessFiles.get(fileName);
175
176 if (attempt == null) {
177 attempt = new IntegerWrapper(1);
178
179 _inProcessFiles.put(fileName, attempt);
180
181 if (_log.isInfoEnabled()) {
182 _log.info("Processing " + fileName);
183 }
184 }
185 else {
186 if (_log.isInfoEnabled()) {
187 _log.info(
188 "Processing " + fileName + ". This is attempt " +
189 attempt.getValue() + ".");
190 }
191 }
192
193 try {
194 for (AutoDeployListener listener : _listeners) {
195 listener.deploy(file);
196 }
197
198 if (file.delete()) {
199 _inProcessFiles.remove(fileName);
200 }
201 else {
202 _log.error("Auto deploy failed to remove " + fileName);
203
204 if (_log.isInfoEnabled()) {
205 _log.info("Add " + fileName + " to the blacklist");
206 }
207
208 _blacklistFiles.add(fileName);
209 }
210 }
211 catch (Exception e) {
212 _log.error(e, e);
213
214 attempt.increment();
215
216 if (attempt.getValue() >= _blacklistThreshold) {
217 if (_log.isInfoEnabled()) {
218 _log.info("Add " + fileName + " to the blacklist");
219 }
220
221 _blacklistFiles.add(fileName);
222 }
223 }
224 }
225
226 private static Log _log = LogFactoryUtil.getLog(AutoDeployDir.class);
227
228 private String _name;
229 private File _deployDir;
230 private File _destDir;
231 private long _interval;
232 private int _blacklistThreshold;
233 private List<AutoDeployListener> _listeners;
234 private Map<String, IntegerWrapper> _inProcessFiles;
235 private Set<String> _blacklistFiles;
236 private AutoDeployScanner _scanner;
237
238 }