1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.deploy.auto;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.IntegerWrapper;
20  
21  import java.io.File;
22  
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.concurrent.CopyOnWriteArrayList;
29  
30  /**
31   * <a href="AutoDeployDir.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Ivica Cardic
34   * @author Brian Wing Shun Chan
35   */
36  public class AutoDeployDir {
37  
38      public static final String DEFAULT_NAME = "defaultAutoDeployDir";
39  
40      public AutoDeployDir(
41          String name, File deployDir, File destDir, long interval,
42          int blacklistThreshold, List<AutoDeployListener> listeners) {
43  
44          _name = name;
45          _deployDir = deployDir;
46          _destDir = destDir;
47          _interval = interval;
48          _blacklistThreshold = blacklistThreshold;
49          _listeners = new CopyOnWriteArrayList<AutoDeployListener>(listeners);
50          _inProcessFiles = new HashMap<String, IntegerWrapper>();
51          _blacklistFiles = new HashSet<String>();
52      }
53  
54      public int getBlacklistThreshold() {
55          return _blacklistThreshold;
56      }
57  
58      public File getDeployDir() {
59          return _deployDir;
60      }
61  
62      public File getDestDir() {
63          return _destDir;
64      }
65  
66      public long getInterval() {
67          return _interval;
68      }
69  
70      public List<AutoDeployListener> getListeners() {
71          return _listeners;
72      }
73  
74      public String getName() {
75          return _name;
76      }
77  
78      public void registerListener(AutoDeployListener listener) {
79          _listeners.add(listener);
80      }
81  
82      public void start() {
83          if (!_deployDir.exists()) {
84              if (_log.isInfoEnabled()) {
85                  _log.info("Creating missing directory " + _deployDir);
86              }
87  
88              boolean created = _deployDir.mkdirs();
89  
90              if (!created) {
91                  _log.error("Directory " + _deployDir + " could not be created");
92              }
93          }
94  
95          if (_interval > 0) {
96              try {
97                  Thread currentThread = Thread.currentThread();
98  
99                  _scanner = new AutoDeployScanner(
100                     currentThread.getThreadGroup(),
101                     AutoDeployScanner.class.getName(), this);
102 
103                 _scanner.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 (_scanner != null) {
126             _scanner.pause();
127         }
128     }
129 
130     public void unregisterListener(AutoDeployListener listener) {
131         _listeners.remove(listener);
132     }
133 
134     protected void processFile(File file) {
135         String fileName = file.getName();
136 
137         if (!file.canRead()) {
138             _log.error("Unable to read " + fileName);
139 
140             return;
141         }
142 
143         if (!file.canWrite()) {
144             _log.error("Unable to write " + fileName);
145 
146             return;
147         }
148 
149         if (_blacklistFiles.contains(fileName)) {
150             if (_log.isDebugEnabled()) {
151                 _log.debug(
152                     "Skip processing of " + fileName + " because it is " +
153                         "blacklisted. You must restart the server to remove " +
154                             "the file from the blacklist.");
155             }
156 
157             return;
158         }
159 
160         IntegerWrapper attempt = _inProcessFiles.get(fileName);
161 
162         if (attempt == null) {
163             attempt = new IntegerWrapper(1);
164 
165             _inProcessFiles.put(fileName, attempt);
166 
167             if (_log.isInfoEnabled()) {
168                 _log.info("Processing " + fileName);
169             }
170         }
171         else {
172             if (_log.isInfoEnabled()) {
173                 _log.info(
174                     "Processing " + fileName + ". This is attempt " +
175                         attempt.getValue() + ".");
176             }
177         }
178 
179         try {
180             for (AutoDeployListener listener : _listeners) {
181                 listener.deploy(file);
182             }
183 
184             if (file.delete()) {
185                 _inProcessFiles.remove(fileName);
186             }
187             else {
188                 _log.error("Auto deploy failed to remove " + fileName);
189 
190                 if (_log.isInfoEnabled()) {
191                     _log.info("Add " + fileName + " to the blacklist");
192                 }
193 
194                 _blacklistFiles.add(fileName);
195             }
196         }
197         catch (Exception e) {
198             _log.error(e, e);
199 
200             attempt.increment();
201 
202             if (attempt.getValue() >= _blacklistThreshold) {
203                 if (_log.isInfoEnabled()) {
204                     _log.info("Add " + fileName + " to the blacklist");
205                 }
206 
207                 _blacklistFiles.add(fileName);
208             }
209         }
210     }
211 
212     protected void scanDirectory() {
213         File[] files = _deployDir.listFiles();
214 
215         for (int i = 0; i < files.length; i++) {
216             File file = files[i];
217 
218             String fileName = file.getName().toLowerCase();
219 
220             if ((file.isFile()) &&
221                 (fileName.endsWith(".war") || fileName.endsWith(".zip") ||
222                  fileName.endsWith(".xml"))) {
223 
224                 processFile(file);
225             }
226         }
227     }
228 
229     private static Log _log = LogFactoryUtil.getLog(AutoDeployDir.class);
230 
231     private Set<String> _blacklistFiles;
232     private int _blacklistThreshold;
233     private File _deployDir;
234     private File _destDir;
235     private Map<String, IntegerWrapper> _inProcessFiles;
236     private long _interval;
237     private List<AutoDeployListener> _listeners;
238     private String _name;
239     private AutoDeployScanner _scanner;
240 
241 }