001
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.ListUtil;
021
022 import java.io.File;
023
024 import java.util.Iterator;
025 import java.util.List;
026 import java.util.concurrent.CopyOnWriteArrayList;
027
028
032 public class SandboxDeployDir {
033
034 public static final String DEFAULT_NAME = "defaultSandboxDeployDir";
035
036 public SandboxDeployDir(
037 String name, File deployDir, long interval,
038 List<SandboxDeployListener> sandboxDeployListeners) {
039
040 _name = name;
041 _deployDir = deployDir;
042 _interval = interval;
043 _sandboxDeployListeners =
044 new CopyOnWriteArrayList<SandboxDeployListener>(
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 try {
086 Thread currentThread = Thread.currentThread();
087
088 _sandboxDeployScanner = new SandboxDeployScanner(
089 currentThread.getThreadGroup(),
090 SandboxDeployScanner.class.getName(), this);
091
092 _sandboxDeployScanner.start();
093
094 if (_log.isInfoEnabled()) {
095 _log.info(
096 "Sandbox deploy scanner started for " + _deployDir);
097 }
098 }
099 catch (Exception e) {
100 _log.error(e, e);
101
102 stop();
103
104 return;
105 }
106 }
107 else {
108 if (_log.isInfoEnabled()) {
109 _log.info(
110 "Sandbox deploy scanning is disabled for " + _deployDir);
111 }
112 }
113 }
114
115 public void stop() {
116 if (_sandboxDeployScanner != null) {
117 _sandboxDeployScanner.pause();
118 }
119 }
120
121 public void unregisterListener(
122 SandboxDeployListener sandboxDeployListener) {
123
124 _sandboxDeployListeners.remove(sandboxDeployListener);
125 }
126
127 protected void deployDir(File file) {
128 String fileName = file.getName();
129
130 if (!file.canRead()) {
131 _log.error("Unable to read " + fileName);
132
133 return;
134 }
135
136 if (!file.canWrite()) {
137 _log.error("Unable to write " + fileName);
138
139 return;
140 }
141
142 if (_log.isInfoEnabled()) {
143 _log.info("Processing " + fileName);
144 }
145
146 try {
147 for (SandboxDeployListener sandboxDeployListener :
148 _sandboxDeployListeners) {
149
150 sandboxDeployListener.deploy(file);
151 }
152 }
153 catch (Exception e) {
154 _log.error(e, e);
155 }
156 }
157
158 protected void scanDirectory() {
159 File[] currentDirs = _deployDir.listFiles(_directoryFilter);
160
161 if (currentDirs.length != _existingDirs.size()) {
162 for (File dir : currentDirs) {
163 if (!_existingDirs.contains(dir)) {
164 _existingDirs.add(dir);
165
166 deployDir(dir);
167 }
168 }
169 }
170
171 Iterator<File> itr = _existingDirs.iterator();
172
173 while (itr.hasNext()) {
174 File dir = itr.next();
175
176 if (!dir.exists()) {
177 itr.remove();
178
179 undeployDir(dir);
180 }
181 }
182 }
183
184 protected void undeployDir(File file) {
185 try {
186 for (SandboxDeployListener sandboxDeployListener :
187 _sandboxDeployListeners) {
188
189 sandboxDeployListener.undeploy(file);
190 }
191 }
192 catch (Exception e) {
193 _log.error(e, e);
194 }
195 }
196
197 private static Log _log = LogFactoryUtil.getLog(SandboxDeployDir.class);
198
199 private File _deployDir;
200 private DirectoryFilter _directoryFilter = new DirectoryFilter();
201 private List<File> _existingDirs;
202 private long _interval;
203 private String _name;
204 private List<SandboxDeployListener> _sandboxDeployListeners;
205 private SandboxDeployScanner _sandboxDeployScanner;
206
207 }