1
14
15 package com.liferay.portal.kernel.deploy.sandbox;
16
17 import com.liferay.portal.kernel.io.DirectoryFilter;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.util.ListUtil;
21
22 import java.io.File;
23
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.concurrent.CopyOnWriteArrayList;
27
28
34 public class SandboxDeployDir {
35
36 public static final String DEFAULT_NAME = "defaultSandboxDeployDir";
37
38 public SandboxDeployDir(
39 String name, File deployDir, long interval,
40 List<SandboxDeployListener> sandboxDeployListeners) {
41
42 _name = name;
43 _deployDir = deployDir;
44 _interval = interval;
45 _sandboxDeployListeners =
46 new CopyOnWriteArrayList<SandboxDeployListener>(
47 sandboxDeployListeners);
48 }
49
50 public File getDeployDir() {
51 return _deployDir;
52 }
53
54 public long getInterval() {
55 return _interval;
56 }
57
58 public List<SandboxDeployListener> getListeners() {
59 return _sandboxDeployListeners;
60 }
61
62 public String getName() {
63 return _name;
64 }
65
66 public void registerListener(SandboxDeployListener listener) {
67 _sandboxDeployListeners.add(listener);
68 }
69
70 public void start() {
71 if (!_deployDir.exists()) {
72 if (_log.isInfoEnabled()) {
73 _log.info("Creating missing directory " + _deployDir);
74 }
75
76 boolean created = _deployDir.mkdirs();
77
78 if (!created) {
79 _log.error("Directory " + _deployDir + " could not be created");
80 }
81 }
82
83 if (_interval > 0) {
84 _existingDirs = ListUtil.fromArray(
85 _deployDir.listFiles(_directoryFilter));
86
87 try {
88 Thread currentThread = Thread.currentThread();
89
90 _sandboxDeployScanner = new SandboxDeployScanner(
91 currentThread.getThreadGroup(),
92 SandboxDeployScanner.class.getName(), this);
93
94 _sandboxDeployScanner.start();
95
96 if (_log.isInfoEnabled()) {
97 _log.info(
98 "Sandbox deploy scanner started for " + _deployDir);
99 }
100 }
101 catch (Exception e) {
102 _log.error(e, e);
103
104 stop();
105
106 return;
107 }
108 }
109 else {
110 if (_log.isInfoEnabled()) {
111 _log.info(
112 "Sandbox deploy scanning is disabled for " + _deployDir);
113 }
114 }
115 }
116
117 public void stop() {
118 if (_sandboxDeployScanner != null) {
119 _sandboxDeployScanner.pause();
120 }
121 }
122
123 public void unregisterListener(
124 SandboxDeployListener sandboxDeployListener) {
125
126 _sandboxDeployListeners.remove(sandboxDeployListener);
127 }
128
129 protected void deployDir(File file) {
130 String fileName = file.getName();
131
132 if (!file.canRead()) {
133 _log.error("Unable to read " + fileName);
134
135 return;
136 }
137
138 if (!file.canWrite()) {
139 _log.error("Unable to write " + fileName);
140
141 return;
142 }
143
144 if (_log.isInfoEnabled()) {
145 _log.info("Processing " + fileName);
146 }
147
148 try {
149 for (SandboxDeployListener sandboxDeployListener :
150 _sandboxDeployListeners) {
151
152 sandboxDeployListener.deploy(file);
153 }
154 }
155 catch (Exception e) {
156 _log.error(e, e);
157 }
158 }
159
160 protected void scanDirectory() {
161 File[] currentDirs = _deployDir.listFiles(_directoryFilter);
162
163 if (currentDirs.length != _existingDirs.size()) {
164 for (File dir : currentDirs) {
165 if (!_existingDirs.contains(dir)) {
166 _existingDirs.add(dir);
167
168 deployDir(dir);
169 }
170 }
171 }
172
173 Iterator<File> itr = _existingDirs.iterator();
174
175 while (itr.hasNext()) {
176 File dir = itr.next();
177
178 if (!dir.exists()) {
179 itr.remove();
180
181 undeployDir(dir);
182 }
183 }
184 }
185
186 protected void undeployDir(File file) {
187 try {
188 for (SandboxDeployListener sandboxDeployListener :
189 _sandboxDeployListeners) {
190
191 sandboxDeployListener.undeploy(file);
192 }
193 }
194 catch (Exception e) {
195 _log.error(e, e);
196 }
197 }
198
199 private static Log _log = LogFactoryUtil.getLog(SandboxDeployDir.class);
200
201 private File _deployDir;
202 private DirectoryFilter _directoryFilter = new DirectoryFilter();
203 private List<File> _existingDirs;
204 private long _interval;
205 private String _name;
206 private List<SandboxDeployListener> _sandboxDeployListeners;
207 private SandboxDeployScanner _sandboxDeployScanner;
208
209 }