001    /**
002     * Copyright (c) 2000-2013 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 =
045                            new CopyOnWriteArrayList<SandboxDeployListener>(
046                                    sandboxDeployListeners);
047            }
048    
049            public File getDeployDir() {
050                    return _deployDir;
051            }
052    
053            public long getInterval() {
054                    return _interval;
055            }
056    
057            public List<SandboxDeployListener> getListeners() {
058                    return _sandboxDeployListeners;
059            }
060    
061            public String getName() {
062                    return _name;
063            }
064    
065            public void registerListener(SandboxDeployListener listener) {
066                    _sandboxDeployListeners.add(listener);
067            }
068    
069            public void start() {
070                    if (!_deployDir.exists()) {
071                            if (_log.isInfoEnabled()) {
072                                    _log.info("Creating missing directory " + _deployDir);
073                            }
074    
075                            boolean created = _deployDir.mkdirs();
076    
077                            if (!created) {
078                                    _log.error("Directory " + _deployDir + " could not be created");
079                            }
080                    }
081    
082                    if (_interval > 0) {
083                            _existingDirs = ListUtil.fromArray(
084                                    _deployDir.listFiles(_directoryFilter));
085    
086                            Iterator<File> itr = _existingDirs.iterator();
087    
088                            while (itr.hasNext()) {
089                                    File dir = itr.next();
090    
091                                    if (!FileUtil.exists(dir + "/WEB-INF/web.xml")) {
092                                            itr.remove();
093                                    }
094                            }
095    
096                            try {
097                                    Thread currentThread = Thread.currentThread();
098    
099                                    _sandboxDeployScanner = new SandboxDeployScanner(
100                                            currentThread.getThreadGroup(),
101                                            SandboxDeployScanner.class.getName(), this);
102    
103                                    _sandboxDeployScanner.start();
104    
105                                    if (_log.isInfoEnabled()) {
106                                            _log.info(
107                                                    "Sandbox 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(
121                                            "Sandbox deploy scanning is disabled for " + _deployDir);
122                            }
123                    }
124            }
125    
126            public void stop() {
127                    if (_sandboxDeployScanner != null) {
128                            _sandboxDeployScanner.pause();
129                    }
130            }
131    
132            public void unregisterListener(
133                    SandboxDeployListener sandboxDeployListener) {
134    
135                    _sandboxDeployListeners.remove(sandboxDeployListener);
136            }
137    
138            protected void deployDir(File file) {
139                    String fileName = file.getName();
140    
141                    if (!file.canRead()) {
142                            _log.error("Unable to read " + fileName);
143    
144                            return;
145                    }
146    
147                    if (!file.canWrite()) {
148                            _log.error("Unable to write " + fileName);
149    
150                            return;
151                    }
152    
153                    if (_log.isInfoEnabled()) {
154                            _log.info("Processing " + fileName);
155                    }
156    
157                    try {
158                            for (SandboxDeployListener sandboxDeployListener :
159                                            _sandboxDeployListeners) {
160    
161                                    sandboxDeployListener.deploy(file);
162                            }
163                    }
164                    catch (Exception e) {
165                            _log.error(e, e);
166                    }
167            }
168    
169            protected void scanDirectory() {
170                    File[] currentDirs = _deployDir.listFiles(_directoryFilter);
171    
172                    if (currentDirs.length != _existingDirs.size()) {
173                            for (File dir : currentDirs) {
174                                    if (!_existingDirs.contains(dir)) {
175                                            _existingDirs.add(dir);
176    
177                                            deployDir(dir);
178                                    }
179                            }
180                    }
181    
182                    Iterator<File> itr = _existingDirs.iterator();
183    
184                    while (itr.hasNext()) {
185                            File dir = itr.next();
186    
187                            if (!dir.exists()) {
188                                    itr.remove();
189    
190                                    undeployDir(dir);
191                            }
192                    }
193            }
194    
195            protected void undeployDir(File file) {
196                    try {
197                            for (SandboxDeployListener sandboxDeployListener :
198                                            _sandboxDeployListeners) {
199    
200                                    sandboxDeployListener.undeploy(file);
201                            }
202                    }
203                    catch (Exception e) {
204                            _log.error(e, e);
205                    }
206            }
207    
208            private static Log _log = LogFactoryUtil.getLog(SandboxDeployDir.class);
209    
210            private File _deployDir;
211            private DirectoryFilter _directoryFilter = new DirectoryFilter();
212            private List<File> _existingDirs;
213            private long _interval;
214            private String _name;
215            private List<SandboxDeployListener> _sandboxDeployListeners;
216            private SandboxDeployScanner _sandboxDeployScanner;
217    
218    }