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.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38
45 public class AutoDeployDir {
46
47 public AutoDeployDir(String name, File deployDir, File destDir,
48 long interval, int blacklistThreshold,
49 List listeners) {
50
51 _name = name;
52 _deployDir = deployDir;
53 _destDir = destDir;
54 _interval = interval;
55 _blacklistThreshold = blacklistThreshold;
56 _listeners = listeners;
57 _inProcessFiles = new HashMap();
58 _blacklistFiles = new HashSet();
59 }
60
61 public String getName() {
62 return _name;
63 }
64
65 public File getDeployDir() {
66 return _deployDir;
67 }
68
69 public File getDestDir() {
70 return _destDir;
71 }
72
73 public long getInterval() {
74 return _interval;
75 }
76
77 public int getBlacklistThreshold() {
78 return _blacklistThreshold;
79 }
80
81 public List getListeners() {
82 return _listeners;
83 }
84
85 public void start() {
86 if (!_deployDir.exists()) {
87 if (_log.isInfoEnabled()) {
88 _log.info("Creating missing directory " + _deployDir);
89 }
90
91 boolean created = _deployDir.mkdirs();
92
93 if (!created) {
94 _log.error("Directory " + _deployDir + " could not be created");
95 }
96 }
97
98 if (_interval > 0) {
99 try {
100 _scanner = new AutoDeployScanner(
101 Thread.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 = (IntegerWrapper)_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 Iterator itr = _listeners.iterator();
195
196 while (itr.hasNext()) {
197 AutoDeployListener listener = (AutoDeployListener)itr.next();
198
199 listener.deploy(file);
200 }
201
202 if (file.delete()) {
203 _inProcessFiles.remove(fileName);
204 }
205 else {
206 _log.error("Auto deploy failed to remove " + fileName);
207
208 if (_log.isInfoEnabled()) {
209 _log.info("Add " + fileName + " to the blacklist");
210 }
211
212 _blacklistFiles.add(fileName);
213 }
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.getLog(AutoDeployDir.class);
231
232 private String _name;
233 private File _deployDir;
234 private File _destDir;
235 private long _interval;
236 private int _blacklistThreshold;
237 private List _listeners;
238 private Map _inProcessFiles;
239 private Set _blacklistFiles;
240 private AutoDeployScanner _scanner;
241
242 }