/
JDBCResourceStore.java
375 lines (319 loc) · 12.3 KB
/
JDBCResourceStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Copyright (c) 2015 OpenPlans - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.jdbcstore;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.io.output.ProxyOutputStream;
import org.apache.commons.lang.ArrayUtils;
import org.geoserver.jdbcstore.cache.ResourceCache;
import org.geoserver.jdbcstore.internal.JDBCDirectoryStructure;
import org.geoserver.jdbcstore.internal.JDBCResourceStoreProperties;
import org.geoserver.platform.resource.LockProvider;
import org.geoserver.platform.resource.NullLockProvider;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.ResourceListener;
import org.geoserver.platform.resource.ResourceNotification;
import org.geoserver.platform.resource.ResourceStore;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;
import org.geoserver.platform.resource.Resources;
import org.geoserver.platform.resource.SimpleResourceNotificationDispatcher;
/**
* Implementation of ResourceStore backed by a JDBC DirectoryStructure.
*
* @author Kevin Smith, Boundless
* @author Niels Charlier
*
*/
public class JDBCResourceStore implements ResourceStore {
private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(JDBCResourceStore.class);
/** LockProvider used to secure resources for exclusive access */
protected LockProvider lockProvider = new NullLockProvider();
protected ResourceNotificationDispatcher resourceWatcher = new SimpleResourceNotificationDispatcher();
protected JDBCDirectoryStructure dir;
protected ResourceCache cache;
protected ResourceStore oldResourceStore;
public void setCache(ResourceCache cache) {
this.cache = cache;
}
LockProvider getLockProvider() {
return lockProvider;
}
/**
* Configure LockProvider used during {@link Resource#out()}.
*
* @param lockProvider LockProvider used for Resource#out()
*/
public void setLockProvider(LockProvider lockProvider) {
this.lockProvider = lockProvider;
}
/**
* Configure ResourceWatcher
*
* @param resourceWatcher
*/
public void setResourceWatcher(ResourceNotificationDispatcher resourceWatcher) {
this.resourceWatcher = resourceWatcher;
}
public JDBCResourceStore(JDBCDirectoryStructure dir) {
this.dir = dir;
}
public JDBCResourceStore(DataSource ds, JDBCResourceStoreProperties config) {
this(new JDBCDirectoryStructure(ds, config));
}
public JDBCResourceStore(DataSource ds, JDBCResourceStoreProperties config, ResourceStore oldResourceStore) {
this(ds, config);
this.oldResourceStore = oldResourceStore;
if (config.isImport()) {
if (oldResourceStore != null) {
try {
Resource root = oldResourceStore.get("");
for (Resource child : root.list()) {
if (!ArrayUtils.contains(config.getIgnoreDirs(), child.name())) {
Resources.copy(child, get(child.name()));
}
}
config.setImport(false);
config.save();
} catch (IOException e) {
throw new IllegalStateException(e);
}
} else {
LOGGER.warning("Cannot import resources: no old resource store available.");
}
}
}
@Override
public Resource get(String path) {
if (oldResourceStore != null && ArrayUtils.contains(dir.getConfig().getIgnoreDirs(), path)) {
return oldResourceStore.get(path);
}
return new JDBCResource(dir.createEntry(path));
}
@Override
public boolean remove(String path) {
return dir.createEntry(path).delete();
}
@Override
public boolean move(String path, String target) {
return dir.createEntry(path).renameTo(dir.createEntry(target));
}
@Override
public String toString() {
return "JDBCResourceStore";
}
/**
* Direct implementation of Resource.
*
*/
protected class JDBCResource implements Resource {
private static final long serialVersionUID = 1829574259576354635L;
private final JDBCDirectoryStructure.Entry entry;
public JDBCResource(JDBCDirectoryStructure.Entry entry) {
assert(entry != null);
this.entry = entry;
}
@Override
public String path() {
return entry.toString();
}
@Override
public String name() {
return entry.getName();
}
private InputStream getIStream() {
return entry.getContent();
}
@Override
public InputStream in() {
final Lock lock = lock();
try {
entry.createResource();
return getIStream();
} finally {
lock.release();
}
}
@Override
public OutputStream out() {
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_CREATE);
if (entry.createResource()) {
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_CREATE,
System.currentTimeMillis(), events));
}
try {
return new CachingOutputStreamWrapper(File.createTempFile("out.", entry.getName()));
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
@Override
public File file() {
if (getType() == Type.DIRECTORY) {
throw new IllegalStateException("Directory (not a file)");
}
final Lock lock = lock();
try {
return cache.cache(this, false);
} catch(IOException ex) {
throw new IllegalStateException(ex);
} finally {
lock.release();
}
}
@Override
public File dir() {
if (getType() == Type.RESOURCE) {
throw new IllegalStateException("File (not a directory)");
}
final Lock lock = lock();
try {
return cache.cache(this, true);
} catch(IOException ex) {
throw new IllegalStateException(ex);
} finally {
lock.release();
}
}
@Override
public long lastmodified() {
Timestamp ts = entry.getLastModified();
return ts == null ? 0L : ts.getTime();
}
@Override
public Resource parent() {
return new JDBCResource(entry.getParent());
}
@Override
public Resource get(String resourcePath) {
return JDBCResourceStore.this.get(path() + "/" + resourcePath);
}
@Override
public List<Resource> list() {
if (getType() != Type.DIRECTORY) {
return Collections.EMPTY_LIST;
}
List<Resource> list = new ArrayList<Resource>();
for (JDBCDirectoryStructure.Entry child : entry.getChildren()) {
list.add(new JDBCResource(child));
}
return list;
}
@Override
public Type getType() {
Boolean isDir = entry.isDirectory();
return isDir == null ? Type.UNDEFINED : isDir ? Type.DIRECTORY : Type.RESOURCE ;
}
@Override
public int hashCode() {
return entry.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof JDBCResource)) {
return false;
}
JDBCResource other = (JDBCResource) obj;
return entry.equals(other.entry);
}
@Override
public boolean delete() {
List<Lock> locks = new ArrayList<Lock>();
lockRecursively(locks);
try {
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_DELETE);
if (entry.delete()) {
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_DELETE,
System.currentTimeMillis(), events));
return true;
} else {
return false;
}
} finally {
for (Lock lock : locks) {
lock.release();
}
}
}
private void lockRecursively(List<Lock> locks) {
for (JDBCDirectoryStructure.Entry child : entry.getChildren()) {
new JDBCResource(child).lockRecursively(locks);
}
locks.add(lock());
}
@Override
public boolean renameTo(Resource dest) {
List<ResourceNotification.Event> eventsDelete = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_DELETE);
List<ResourceNotification.Event> eventsRename = SimpleResourceNotificationDispatcher.createRenameEvents(this, dest);
boolean result;
if (dest instanceof JDBCResource) {
result = entry.renameTo(((JDBCResource) dest).entry);
} else {
result = Resources.renameByCopy(this, dest);
}
if (result) {
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_DELETE,
System.currentTimeMillis(), eventsDelete));
resourceWatcher.changed(new ResourceNotification(path(), eventsRename.get(0).getKind(),
System.currentTimeMillis(), eventsRename));
}
return result;
}
@Override
public Lock lock() {
return lockProvider.acquire(entry.toString());
}
@Override
public void addListener(ResourceListener listener) {
resourceWatcher.addListener(this, listener);
}
@Override
public void removeListener(ResourceListener listener) {
resourceWatcher.removeListener(this, listener);
}
/**
* Use temporary file for writing data to resource.
*
*/
protected class CachingOutputStreamWrapper extends ProxyOutputStream {
File tempFile;
public CachingOutputStreamWrapper(File tempFile) throws IOException {
super(new FileOutputStream(tempFile));
this.tempFile = tempFile;
}
@Override
public void close() throws IOException {
final Lock lock = lock();
try {
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(JDBCResource.this,
ResourceNotification.Kind.ENTRY_MODIFY);
entry.setContent(new FileInputStream(tempFile));
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_MODIFY,
System.currentTimeMillis(), events));
} finally {
lock.release();
}
}
}
}
}