1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
38   * <a href="AutoDeployDir.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Ivica Cardic
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class AutoDeployDir {
45  
46      public AutoDeployDir(
47          String name, File deployDir, File destDir, long interval,
48          int blacklistThreshold, List<AutoDeployListener> listeners) {
49  
50          _name = name;
51          _deployDir = deployDir;
52          _destDir = destDir;
53          _interval = interval;
54          _blacklistThreshold = blacklistThreshold;
55          _listeners = listeners;
56          _inProcessFiles = new HashMap<String, IntegerWrapper>();
57          _blacklistFiles = new HashSet<String>();
58      }
59  
60      public String getName() {
61          return _name;
62      }
63  
64      public File getDeployDir() {
65          return _deployDir;
66      }
67  
68      public File getDestDir() {
69          return _destDir;
70      }
71  
72      public long getInterval() {
73          return _interval;
74      }
75  
76      public int getBlacklistThreshold() {
77          return _blacklistThreshold;
78      }
79  
80      public List<AutoDeployListener> getListeners() {
81          return _listeners;
82      }
83  
84      public void start() {
85          if (!_deployDir.exists()) {
86              if (_log.isInfoEnabled()) {
87                  _log.info("Creating missing directory " + _deployDir);
88              }
89  
90              boolean created = _deployDir.mkdirs();
91  
92              if (!created) {
93                  _log.error("Directory " + _deployDir + " could not be created");
94              }
95          }
96  
97          if (_interval > 0) {
98              try {
99                  Thread currentThread = Thread.currentThread();
100 
101                 _scanner = new AutoDeployScanner(
102                     currentThread.getThreadGroup(),
103                     AutoDeployScanner.class.getName(), this);
104 
105                 _scanner.start();
106 
107                 if (_log.isInfoEnabled()) {
108                     _log.info("Auto deploy scanner started for " + _deployDir);
109                 }
110             }
111             catch (Exception e) {
112                 _log.error(e, e);
113 
114                 stop();
115 
116                 return;
117             }
118         }
119         else {
120             if (_log.isInfoEnabled()) {
121                 _log.info("Auto deploy scanning is disabled for " + _deployDir);
122             }
123         }
124     }
125 
126     public void stop() {
127         if (_scanner != null) {
128             _scanner.pause();
129         }
130     }
131 
132     protected void scanDirectory() {
133         File[] files = _deployDir.listFiles();
134 
135         for (int i = 0; i < files.length; i++) {
136             File file = files[i];
137 
138             String fileName = file.getName().toLowerCase();
139 
140             if ((file.isFile()) &&
141                 (fileName.endsWith(".war") || fileName.endsWith(".zip") ||
142                  fileName.endsWith(".xml"))) {
143 
144                 processFile(file);
145             }
146         }
147     }
148 
149     protected void processFile(File file) {
150         String fileName = file.getName();
151 
152         if (!file.canRead()) {
153             _log.error("Unable to read " + fileName);
154 
155             return;
156         }
157 
158         if (!file.canWrite()) {
159             _log.error("Unable to write " + fileName);
160 
161             return;
162         }
163 
164         if (_blacklistFiles.contains(fileName)) {
165             if (_log.isDebugEnabled()) {
166                 _log.debug(
167                     "Skip processing of " + fileName + " 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 = _inProcessFiles.get(fileName);
176 
177         if (attempt == null) {
178             attempt = new IntegerWrapper(1);
179 
180             _inProcessFiles.put(fileName, attempt);
181 
182             if (_log.isInfoEnabled()) {
183                 _log.info("Processing " + fileName);
184             }
185         }
186         else {
187             if (_log.isInfoEnabled()) {
188                 _log.info(
189                     "Processing " + fileName + ". This is attempt " +
190                         attempt.getValue() + ".");
191             }
192         }
193 
194         try {
195             for (AutoDeployListener listener : _listeners) {
196                 listener.deploy(file);
197             }
198 
199             if (file.delete()) {
200                 _inProcessFiles.remove(fileName);
201             }
202             else {
203                 _log.error("Auto deploy failed to remove " + fileName);
204 
205                 if (_log.isInfoEnabled()) {
206                     _log.info("Add " + fileName + " to the blacklist");
207                 }
208 
209                 _blacklistFiles.add(fileName);
210             }
211         }
212         catch (Exception e) {
213             _log.error(e, e);
214 
215             attempt.increment();
216 
217             if (attempt.getValue() >= _blacklistThreshold) {
218                 if (_log.isInfoEnabled()) {
219                     _log.info("Add " + fileName + " to the blacklist");
220                 }
221 
222                 _blacklistFiles.add(fileName);
223             }
224         }
225     }
226 
227     private static Log _log = LogFactoryUtil.getLog(AutoDeployDir.class);
228 
229     private String _name;
230     private File _deployDir;
231     private File _destDir;
232     private long _interval;
233     private int _blacklistThreshold;
234     private List<AutoDeployListener> _listeners;
235     private Map<String, IntegerWrapper> _inProcessFiles;
236     private Set<String> _blacklistFiles;
237     private AutoDeployScanner _scanner;
238 
239 }