001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.deploy.sandbox;
016    
017    import com.liferay.portal.kernel.io.DirectoryFilter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    
023    import java.io.File;
024    
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.concurrent.CopyOnWriteArrayList;
028    
029    /**
030     * @author Igor Spasic
031     * @author Brian Wing Shun Chan
032     */
033    public class SandboxDeployDir {
034    
035            public static final String DEFAULT_NAME = "defaultSandboxDeployDir";
036    
037            public SandboxDeployDir(
038                    String name, File deployDir, long interval,
039                    List<SandboxDeployListener> sandboxDeployListeners) {
040    
041                    _name = name;
042                    _deployDir = deployDir;
043                    _interval = interval;
044                    _sandboxDeployListeners = new CopyOnWriteArrayList<>(
045                            sandboxDeployListeners);
046            }
047    
048            public File getDeployDir() {
049                    return _deployDir;
050            }
051    
052            public long getInterval() {
053                    return _interval;
054            }
055    
056            public List<SandboxDeployListener> getListeners() {
057                    return _sandboxDeployListeners;
058            }
059    
060            public String getName() {
061                    return _name;
062            }
063    
064            public void registerListener(SandboxDeployListener listener) {
065                    _sandboxDeployListeners.add(listener);
066            }
067    
068            public void start() {
069                    if (!_deployDir.exists()) {
070                            if (_log.isInfoEnabled()) {
071                                    _log.info("Creating missing directory " + _deployDir);
072                            }
073    
074                            boolean created = _deployDir.mkdirs();
075    
076                            if (!created) {
077                                    _log.error("Directory " + _deployDir + " could not be created");
078                            }
079                    }
080    
081                    if (_interval > 0) {
082                            _existingDirs = ListUtil.fromArray(
083                                    _deployDir.listFiles(_directoryFilter));
084    
085                            Iterator<File> itr = _existingDirs.iterator();
086    
087                            while (itr.hasNext()) {
088                                    File dir = itr.next();
089    
090                                    if (!FileUtil.exists(dir + "/WEB-INF/web.xml")) {
091                                            itr.remove();
092                                    }
093                            }
094    
095                            try {
096                                    Thread currentThread = Thread.currentThread();
097    
098                                    _sandboxDeployScanner = new SandboxDeployScanner(
099                                            currentThread.getThreadGroup(),
100                                            SandboxDeployScanner.class.getName(), this);
101    
102                                    _sandboxDeployScanner.start();
103    
104                                    if (_log.isInfoEnabled()) {
105                                            _log.info(
106                                                    "Sandbox 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(
120                                            "Sandbox deploy scanning is disabled for " + _deployDir);
121                            }
122                    }
123            }
124    
125            public void stop() {
126                    if (_sandboxDeployScanner != null) {
127                            _sandboxDeployScanner.pause();
128                    }
129            }
130    
131            public void unregisterListener(
132                    SandboxDeployListener sandboxDeployListener) {
133    
134                    _sandboxDeployListeners.remove(sandboxDeployListener);
135            }
136    
137            protected void deployDir(File file) {
138                    String fileName = file.getName();
139    
140                    if (!file.canRead()) {
141                            _log.error("Unable to read " + fileName);
142    
143                            return;
144                    }
145    
146                    if (!file.canWrite()) {
147                            _log.error("Unable to write " + fileName);
148    
149                            return;
150                    }
151    
152                    if (_log.isInfoEnabled()) {
153                            _log.info("Processing " + fileName);
154                    }
155    
156                    try {
157                            for (SandboxDeployListener sandboxDeployListener :
158                                            _sandboxDeployListeners) {
159    
160                                    sandboxDeployListener.deploy(file);
161                            }
162                    }
163                    catch (Exception e) {
164                            _log.error(e, e);
165                    }
166            }
167    
168            protected void scanDirectory() {
169                    File[] currentDirs = _deployDir.listFiles(_directoryFilter);
170    
171                    if (currentDirs.length != _existingDirs.size()) {
172                            for (File dir : currentDirs) {
173                                    if (!_existingDirs.contains(dir)) {
174                                            _existingDirs.add(dir);
175    
176                                            deployDir(dir);
177                                    }
178                            }
179                    }
180    
181                    Iterator<File> itr = _existingDirs.iterator();
182    
183                    while (itr.hasNext()) {
184                            File dir = itr.next();
185    
186                            if (!dir.exists()) {
187                                    itr.remove();
188    
189                                    undeployDir(dir);
190                            }
191                    }
192            }
193    
194            protected void undeployDir(File file) {
195                    try {
196                            for (SandboxDeployListener sandboxDeployListener :
197                                            _sandboxDeployListeners) {
198    
199                                    sandboxDeployListener.undeploy(file);
200                            }
201                    }
202                    catch (Exception e) {
203                            _log.error(e, e);
204                    }
205            }
206    
207            private static final Log _log = LogFactoryUtil.getLog(
208                    SandboxDeployDir.class);
209    
210            private final File _deployDir;
211            private final DirectoryFilter _directoryFilter = new DirectoryFilter();
212            private List<File> _existingDirs;
213            private final long _interval;
214            private final String _name;
215            private final List<SandboxDeployListener> _sandboxDeployListeners;
216            private SandboxDeployScanner _sandboxDeployScanner;
217    
218    }