svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCResourcesStorage.java @ 46338
History | View | Annotate | Download (26.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
25 |
|
26 |
import java.io.ByteArrayInputStream; |
27 |
import java.io.ByteArrayOutputStream; |
28 |
import java.io.File; |
29 |
import java.io.FileInputStream; |
30 |
import java.io.FileNotFoundException; |
31 |
import java.io.FileOutputStream; |
32 |
import java.io.IOException; |
33 |
import java.io.InputStream; |
34 |
import java.io.OutputStream; |
35 |
import java.net.URL; |
36 |
import java.util.ArrayList; |
37 |
import java.util.Collections; |
38 |
import java.util.HashMap; |
39 |
import java.util.HashSet; |
40 |
import java.util.List; |
41 |
import java.util.Map; |
42 |
import java.util.Set; |
43 |
import org.apache.commons.codec.digest.DigestUtils; |
44 |
import org.apache.commons.io.FileUtils; |
45 |
import org.apache.commons.io.FilenameUtils; |
46 |
import org.apache.commons.io.IOUtils; |
47 |
import org.apache.commons.io.output.NullOutputStream; |
48 |
import org.apache.commons.lang3.StringUtils; |
49 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
50 |
import org.apache.commons.lang3.tuple.Pair; |
51 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
52 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
53 |
import org.gvsig.fmap.dal.DALLocator; |
54 |
import org.gvsig.fmap.dal.DataManager; |
55 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_NAME; |
56 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_RESOURCE; |
57 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
58 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
59 |
import org.gvsig.fmap.dal.feature.Feature; |
60 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
61 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
62 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
63 |
import org.gvsig.fmap.dal.store.jdbc.exception.SQLRuntimeException; |
64 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
65 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
66 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.RetrieveValueOperation; |
67 |
import org.gvsig.tools.ToolsLocator; |
68 |
import org.gvsig.tools.dispose.DisposeUtils; |
69 |
import org.gvsig.tools.exception.BaseException; |
70 |
import org.gvsig.tools.folders.FoldersManager; |
71 |
import org.gvsig.tools.resourcesstorage.AbstractResourcesStorage; |
72 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
73 |
import org.gvsig.tools.util.CachedValue; |
74 |
import org.slf4j.Logger; |
75 |
import org.slf4j.LoggerFactory; |
76 |
|
77 |
/**
|
78 |
*
|
79 |
* @author jjdelcerro
|
80 |
*/
|
81 |
@SuppressWarnings("UseSpecificCatch") |
82 |
public class JDBCResourcesStorage extends AbstractResourcesStorage { |
83 |
|
84 |
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCResourcesStorage.class); |
85 |
public static final String TABLENAME_SERVER_EXPLORER_MARK = "$ServerExplorer"; |
86 |
|
87 |
private final JDBCHelper helper; |
88 |
|
89 |
private static class ExistsResourcesCache extends CachedValue<Map<String,Boolean>> { |
90 |
@Override
|
91 |
protected void reload() { |
92 |
LOGGER.info("reload ExistsResourcesCache");
|
93 |
this.setValue(new HashMap<>()); |
94 |
} |
95 |
} |
96 |
|
97 |
private static class DataBaseResource implements Resource { |
98 |
|
99 |
private class ResourceInputStream extends InputStream { |
100 |
|
101 |
@Override
|
102 |
public int read() throws IOException { |
103 |
return in.read();
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void close() throws IOException { |
108 |
DataBaseResource.this.close(); |
109 |
} |
110 |
} |
111 |
|
112 |
private class ResourceOutputStream extends OutputStream { |
113 |
|
114 |
@Override
|
115 |
public void write(int b) throws IOException { |
116 |
out.write(b); |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void flush() throws IOException { |
121 |
out.flush(); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void close() throws IOException { |
126 |
DataBaseResource.this.close(); |
127 |
} |
128 |
} |
129 |
|
130 |
private final String name; |
131 |
private final JDBCResourcesStorage storage; |
132 |
|
133 |
private InputStream in; |
134 |
private OutputStream out; |
135 |
|
136 |
public DataBaseResource(JDBCResourcesStorage storage, String name) { |
137 |
this.storage = storage;
|
138 |
this.name = name;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public String getName() { |
143 |
return name;
|
144 |
} |
145 |
|
146 |
private JDBCStoreParameters getStoreParameters() {
|
147 |
return this.storage.resourcesStoreParameters; |
148 |
} |
149 |
|
150 |
private String getTableName() { |
151 |
return this.storage.tableName; |
152 |
} |
153 |
|
154 |
private Map<String,Boolean> getExistsResourcesCache() { |
155 |
return this.storage.existsResourcesCache.get(); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public boolean isReadOnly() { |
160 |
return this.storage.readonly; |
161 |
} |
162 |
@Override
|
163 |
public URL getURL() { |
164 |
try {
|
165 |
String url = this.getStoreParameters().getUrl(); |
166 |
return new URL(url + "&tableName=" + this.getTableName() + "&resourceName=" + this.name); |
167 |
} catch (Throwable ex) { |
168 |
return null; |
169 |
} |
170 |
} |
171 |
|
172 |
private byte[] retrieveResource() throws Exception { |
173 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
174 |
|
175 |
String filter;
|
176 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
177 |
filter = builder.eq( |
178 |
builder.column(FIELD_RESOURCES_NAME), |
179 |
builder.constant(this.name)
|
180 |
).toString(); |
181 |
|
182 |
} else {
|
183 |
filter = builder.eq( |
184 |
builder.column(FIELD_RESOURCES_NAME), |
185 |
builder.constant(this.getTableName() + "." + this.name) |
186 |
).toString(); |
187 |
} |
188 |
|
189 |
OperationsFactory operations = this.storage.helper.getOperations();
|
190 |
RetrieveValueOperation op = operations.createRetrieveValue( |
191 |
operations.createTableReference(this.getStoreParameters()),
|
192 |
filter, |
193 |
null, //order |
194 |
FIELD_RESOURCES_RESOURCE |
195 |
); |
196 |
return (byte[] ) op.perform(); |
197 |
} |
198 |
|
199 |
private byte[] retrieveResource0() throws Exception { |
200 |
FeatureStore store = null;
|
201 |
try {
|
202 |
DataManager dataManager = DALLocator.getDataManager(); |
203 |
store = (FeatureStore) dataManager.openStore( |
204 |
this.getStoreParameters().getDataStoreName(),
|
205 |
this.getStoreParameters()
|
206 |
); |
207 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
208 |
String filter;
|
209 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
210 |
filter = builder.eq( |
211 |
builder.column(FIELD_RESOURCES_NAME), |
212 |
builder.constant(this.name)
|
213 |
).toString(); |
214 |
|
215 |
} else {
|
216 |
filter = builder.eq( |
217 |
builder.column(FIELD_RESOURCES_NAME), |
218 |
builder.constant(this.getTableName() + "." + this.name) |
219 |
).toString(); |
220 |
} |
221 |
Feature feature = store.findFirst(filter); |
222 |
if (feature == null) { |
223 |
return null; |
224 |
} |
225 |
byte[] resource = feature.getByteArray(FIELD_RESOURCES_RESOURCE); |
226 |
return resource;
|
227 |
} finally {
|
228 |
DisposeUtils.disposeQuietly(store); |
229 |
} |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public boolean exists() { |
234 |
// FeatureStore store = null;
|
235 |
try {
|
236 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
237 |
Pair<String, String> key = this.getCacheID(); |
238 |
String key_s = key.getLeft() + "/"+ key.getRight(); |
239 |
Map<String, Boolean> erc = this.getExistsResourcesCache(); |
240 |
if( erc.containsKey(key_s) ) {
|
241 |
return this.getExistsResourcesCache().get(key_s); |
242 |
} |
243 |
File f = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), key.getRight()); |
244 |
if( f.exists() ) {
|
245 |
return true; |
246 |
} |
247 |
byte[] resource = retrieveResource(); |
248 |
boolean exists = resource!=null; |
249 |
if( resource!=null ) { |
250 |
putResourceInCache(resource); |
251 |
} |
252 |
this.getExistsResourcesCache().put(key_s, exists);
|
253 |
return exists;
|
254 |
} catch (Throwable ex) { |
255 |
LOGGER.warn("Can't access to the resoure '" + this.getURL() + "'.", ex); |
256 |
// } finally {
|
257 |
// DisposeUtils.disposeQuietly(store);
|
258 |
} |
259 |
return false; |
260 |
} |
261 |
|
262 |
public boolean remove() { |
263 |
FeatureStore store = null;
|
264 |
try {
|
265 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
266 |
Pair<String, String> key = this.getCacheID(); |
267 |
File f = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), key.getRight()); |
268 |
if( f.exists() ) {
|
269 |
f.delete(); |
270 |
} |
271 |
DataManager dataManager = DALLocator.getDataManager(); |
272 |
store = (FeatureStore) dataManager.openStore( |
273 |
this.getStoreParameters().getDataStoreName(),
|
274 |
this.getStoreParameters()
|
275 |
); |
276 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
277 |
String filter;
|
278 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
279 |
filter = builder.eq( |
280 |
builder.column(FIELD_RESOURCES_NAME), |
281 |
builder.constant(this.name)
|
282 |
).toString(); |
283 |
|
284 |
} else {
|
285 |
filter = builder.eq( |
286 |
builder.column(FIELD_RESOURCES_NAME), |
287 |
builder.constant(this.getTableName() + "." + this.name) |
288 |
).toString(); |
289 |
|
290 |
} |
291 |
|
292 |
Feature feature = store.findFirst(filter); |
293 |
if(feature != null){ |
294 |
store.edit(); |
295 |
store.delete(feature); |
296 |
store.finishEditing(); |
297 |
return true; |
298 |
} |
299 |
return false; |
300 |
} catch (Throwable ex) { |
301 |
FeatureStore.cancelEditingQuietly(store); |
302 |
LOGGER.warn("Can't access to the resoure '" + this.getURL() + "'.", ex); |
303 |
} finally {
|
304 |
DisposeUtils.disposeQuietly(store); |
305 |
} |
306 |
return false; |
307 |
} |
308 |
|
309 |
private Pair<String,String> getCacheID() { |
310 |
byte[] data = this.getStoreParameters().toByteArray(); |
311 |
ImmutablePair<String, String> r; |
312 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
313 |
r = new ImmutablePair<>(
|
314 |
DigestUtils.md5Hex(data), |
315 |
this.name
|
316 |
); |
317 |
} else {
|
318 |
r = new ImmutablePair<>(
|
319 |
DigestUtils.md5Hex(data), |
320 |
this.getTableName()+"."+this.name |
321 |
); |
322 |
} |
323 |
return r;
|
324 |
} |
325 |
|
326 |
private boolean removeCache() { |
327 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
328 |
Pair<String, String> key = this.getCacheID(); |
329 |
File f = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), key.getRight()); |
330 |
if( f.exists() ) {
|
331 |
try {
|
332 |
FileUtils.forceDelete(f); |
333 |
} catch (IOException ex) { |
334 |
return false; |
335 |
} |
336 |
} |
337 |
return true; |
338 |
} |
339 |
|
340 |
private InputStream getInputStreamFromCache() { |
341 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
342 |
Pair<String, String> key = this.getCacheID(); |
343 |
File f = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), key.getRight()); |
344 |
if( !f.exists() ) {
|
345 |
InputStream is = null; |
346 |
FileOutputStream os = null; |
347 |
File f2 = null; |
348 |
try {
|
349 |
is = this.getInputStream();
|
350 |
if (is==null) { |
351 |
return null; |
352 |
} |
353 |
FileUtils.forceMkdir(f.getParentFile()); |
354 |
os = new FileOutputStream(f); |
355 |
IOUtils.copy(is, os); |
356 |
IOUtils.closeQuietly(os); |
357 |
|
358 |
f2 = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), "parameters"); |
359 |
byte[] data = this.getStoreParameters().toByteArray(); |
360 |
os = new FileOutputStream(f2); |
361 |
IOUtils.write(data, os); |
362 |
} catch (IOException ex) { |
363 |
FileUtils.deleteQuietly(f); |
364 |
FileUtils.deleteQuietly(f2); |
365 |
return null; |
366 |
} finally {
|
367 |
IOUtils.closeQuietly(is); |
368 |
IOUtils.closeQuietly(os); |
369 |
} |
370 |
} |
371 |
InputStream is = null; |
372 |
try {
|
373 |
is = new FileInputStream(f); |
374 |
} catch (FileNotFoundException ex) { |
375 |
} |
376 |
return is;
|
377 |
} |
378 |
|
379 |
private void putResourceInCache(byte[] resource) { |
380 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
381 |
Pair<String, String> key = this.getCacheID(); |
382 |
File f = fm.getTemporaryFile("resources-storage", "jdbc", key.getLeft(), key.getRight()); |
383 |
FileOutputStream os = null; |
384 |
File f2 = null; |
385 |
try {
|
386 |
FileUtils.forceMkdir(f.getParentFile()); |
387 |
os = new FileOutputStream(f); |
388 |
IOUtils.write(resource, os); |
389 |
IOUtils.closeQuietly(os); |
390 |
|
391 |
f2 = fm.getTemporaryFile("resources-storage", "jdbc", key.getLeft(), "parameters"); |
392 |
byte[] data = this.getStoreParameters().toByteArray(); |
393 |
os = new FileOutputStream(f2); |
394 |
IOUtils.write(data, os); |
395 |
} catch (IOException ex) { |
396 |
FileUtils.deleteQuietly(f); |
397 |
FileUtils.deleteQuietly(f2); |
398 |
} finally {
|
399 |
IOUtils.closeQuietly(os); |
400 |
} |
401 |
} |
402 |
|
403 |
private InputStream getInputStream() throws IOException { |
404 |
try {
|
405 |
byte[] resource = retrieveResource(); |
406 |
InputStream is = new ByteArrayInputStream(resource); |
407 |
return is;
|
408 |
} catch (Throwable ex) { |
409 |
LOGGER.warn("Can't access to the resoure '" + this.getURL() + "'.", ex); |
410 |
} |
411 |
return null; |
412 |
} |
413 |
|
414 |
@Override
|
415 |
public InputStream asInputStream() throws IOException { |
416 |
if (this.in != null || this.out != null) { |
417 |
throw new IllegalStateException("Resource is already open (" + this.getURL() + ")"); |
418 |
} |
419 |
InputStream is = this.getInputStreamFromCache(); |
420 |
if( is==null ) { |
421 |
is = this.getInputStream();
|
422 |
if ( is==null) { |
423 |
return null; |
424 |
} |
425 |
} |
426 |
this.in = is;
|
427 |
return new ResourceInputStream(); |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public OutputStream asOutputStream() throws IOException { |
432 |
if (this.in != null || this.out != null) { |
433 |
throw new IllegalStateException("Resource is already open (" + this.getURL() + ")."); |
434 |
} |
435 |
if( this.isReadOnly()) { |
436 |
this.out = NullOutputStream.NULL_OUTPUT_STREAM;
|
437 |
} else {
|
438 |
this.out = new ByteArrayOutputStream(); |
439 |
} |
440 |
return new ResourceOutputStream(); |
441 |
} |
442 |
|
443 |
@Override
|
444 |
public void close() { |
445 |
if (this.in != null) { |
446 |
IOUtils.closeQuietly(this.in);
|
447 |
this.in = null; |
448 |
} |
449 |
if (!this.isReadOnly()&& this.out != null) { |
450 |
FeatureStore store = null;
|
451 |
try {
|
452 |
DataManager dataManager = DALLocator.getDataManager(); |
453 |
store = (FeatureStore) dataManager.openStore( |
454 |
this.getStoreParameters().getDataStoreName(),
|
455 |
this.getStoreParameters()
|
456 |
); |
457 |
store.edit(); |
458 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
459 |
String filter;
|
460 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
461 |
filter = builder.eq( |
462 |
builder.column(FIELD_RESOURCES_NAME), |
463 |
builder.constant(this.name)
|
464 |
).toString(); |
465 |
|
466 |
} else {
|
467 |
filter = builder.eq( |
468 |
builder.column(FIELD_RESOURCES_NAME), |
469 |
builder.constant(this.getTableName() + "." + this.name) |
470 |
).toString(); |
471 |
} |
472 |
|
473 |
Feature feature = store.findFirst(filter); |
474 |
EditableFeature efeature; |
475 |
if (feature == null) { |
476 |
efeature = store.createNewFeature(); |
477 |
|
478 |
if (StringUtils.equalsIgnoreCase(this.getTableName(), TABLENAME_SERVER_EXPLORER_MARK)) { |
479 |
efeature.set(FIELD_RESOURCES_NAME, this.name);
|
480 |
} else {
|
481 |
efeature.set(FIELD_RESOURCES_NAME, this.getTableName()+"."+this.name); |
482 |
} |
483 |
efeature.set(FIELD_RESOURCES_RESOURCE, ((ByteArrayOutputStream)this.out).toByteArray()); |
484 |
store.insert(efeature); |
485 |
} else {
|
486 |
efeature = feature.getEditable(); |
487 |
efeature.set(FIELD_RESOURCES_RESOURCE, ((ByteArrayOutputStream)this.out).toByteArray()); |
488 |
store.update(efeature); |
489 |
} |
490 |
store.finishEditing(); |
491 |
removeCache(); |
492 |
} catch (SQLRuntimeException ex) {
|
493 |
FeatureStore.cancelEditingQuietly(store); |
494 |
if( !ex.isTableOrViewNotFound() ) {
|
495 |
LOGGER.warn("Can't write the resoure '" + this.getURL() + "'.", ex); |
496 |
} |
497 |
} catch (Throwable ex) { |
498 |
FeatureStore.cancelEditingQuietly(store); |
499 |
LOGGER.warn("Can't write the resoure '" + this.getURL() + "'.", ex); |
500 |
} finally {
|
501 |
DisposeUtils.disposeQuietly(store); |
502 |
} |
503 |
} |
504 |
} |
505 |
} |
506 |
|
507 |
private final ResourcesStorage alternativeStorage; |
508 |
private final JDBCStoreParameters resourcesStoreParameters; |
509 |
private final String tableName; |
510 |
private final boolean readonly; |
511 |
private final ExistsResourcesCache existsResourcesCache; |
512 |
|
513 |
public JDBCResourcesStorage(
|
514 |
JDBCHelper helper, |
515 |
ResourcesStorage alternativeStorage, |
516 |
JDBCStoreParameters resourcesStoreParameters, |
517 |
String tableName
|
518 |
) { |
519 |
this(helper, alternativeStorage, resourcesStoreParameters, tableName, false); |
520 |
} |
521 |
|
522 |
public JDBCResourcesStorage(
|
523 |
JDBCHelper helper, |
524 |
ResourcesStorage alternativeStorage, |
525 |
JDBCStoreParameters resourcesStoreParameters, |
526 |
String tableName,
|
527 |
boolean readonly
|
528 |
) { |
529 |
this.helper = helper;
|
530 |
DisposeUtils.bind(this.helper);
|
531 |
this.readonly = readonly;
|
532 |
this.alternativeStorage = alternativeStorage;
|
533 |
if( StringUtils.equals(TABLE_RESOURCES_NAME, tableName) ) {
|
534 |
// No podemos buscar recursos de la tabla de recursos, ya que si no
|
535 |
// al abrise la tabla de recursos entraria en bucle.
|
536 |
this.resourcesStoreParameters = null; |
537 |
} else {
|
538 |
this.resourcesStoreParameters = resourcesStoreParameters;
|
539 |
} |
540 |
this.tableName = tableName;
|
541 |
this.existsResourcesCache = new ExistsResourcesCache(); |
542 |
this.existsResourcesCache.setExpireTime(60000); // 1 min |
543 |
} |
544 |
|
545 |
@Override
|
546 |
public boolean isEmpty() { |
547 |
return this.resourcesStoreParameters == null; |
548 |
} |
549 |
|
550 |
@Override
|
551 |
public List<String> getResourceNames() { |
552 |
if( this.alternativeStorage!=null ) { |
553 |
List<String> names = this.alternativeStorage.getResourceNames(); |
554 |
return names;
|
555 |
} |
556 |
if (this.resourcesStoreParameters == null) { |
557 |
return Collections.EMPTY_LIST; |
558 |
} |
559 |
|
560 |
FeatureStore store = null;
|
561 |
try {
|
562 |
DataManager dataManager = DALLocator.getDataManager(); |
563 |
Set<String> typeNames = new HashSet<>(); |
564 |
store = (FeatureStore) dataManager.openStore( |
565 |
this.resourcesStoreParameters.getDataStoreName(),
|
566 |
this.resourcesStoreParameters
|
567 |
); |
568 |
FeatureSet featureSet = store.getFeatureSet(); |
569 |
for (Feature feature : featureSet) {
|
570 |
String resName = feature.getString(FIELD_RESOURCES_NAME);
|
571 |
if (StringUtils.equalsIgnoreCase(tableName, TABLENAME_SERVER_EXPLORER_MARK)) {
|
572 |
typeNames.add(resName); |
573 |
} else {
|
574 |
String resTypeName = FilenameUtils.getExtension(resName);
|
575 |
typeNames.add(resTypeName); |
576 |
} |
577 |
} |
578 |
return new ArrayList<>(typeNames); |
579 |
} catch (Throwable ex) { |
580 |
LOGGER.warn("Can't get resources for '" + this.resourcesStoreParameters.getUrl() + "'.", ex); |
581 |
return null; |
582 |
|
583 |
} finally {
|
584 |
DisposeUtils.disposeQuietly(store); |
585 |
} |
586 |
} |
587 |
|
588 |
|
589 |
|
590 |
@Override
|
591 |
public Resource getResource(String name) { |
592 |
if( this.alternativeStorage!=null ) { |
593 |
Resource r = this.alternativeStorage.getResource(name);
|
594 |
if( r.exists() ) {
|
595 |
return r;
|
596 |
} |
597 |
} |
598 |
if (this.resourcesStoreParameters == null) { |
599 |
return null; |
600 |
} |
601 |
return new DataBaseResource(this,name); |
602 |
} |
603 |
|
604 |
@Override
|
605 |
public List<Resource> getResources(String name) { |
606 |
if (this.resourcesStoreParameters == null) { |
607 |
return null; |
608 |
} |
609 |
if( this.alternativeStorage!=null ) { |
610 |
List<Resource> r = this.alternativeStorage.getResources(name); |
611 |
if( r!=null && !r.isEmpty() ) { |
612 |
return r;
|
613 |
} |
614 |
} |
615 |
FeatureStore store = null;
|
616 |
try {
|
617 |
DataManager dataManager = DALLocator.getDataManager(); |
618 |
store = (FeatureStore) dataManager.openStore( |
619 |
this.resourcesStoreParameters.getDataStoreName(),
|
620 |
this.resourcesStoreParameters
|
621 |
); |
622 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
623 |
List<ResourcesStorage.Resource> ress = new ArrayList<>(); |
624 |
int n = 0; |
625 |
while (true) { |
626 |
String multiresourceName;
|
627 |
if (n == 0) { |
628 |
multiresourceName = name; |
629 |
} else {
|
630 |
multiresourceName = String.valueOf(n) + "." + name ; |
631 |
} |
632 |
String filter = builder.eq(
|
633 |
builder.column(FIELD_RESOURCES_NAME), |
634 |
builder.constant(this.tableName+"."+multiresourceName) |
635 |
).toString(); |
636 |
Feature feature = store.findFirst(filter); |
637 |
if( feature==null ) { |
638 |
break;
|
639 |
} |
640 |
ress.add(new DataBaseResource(this,multiresourceName)); |
641 |
n++; |
642 |
} |
643 |
if (ress.isEmpty()) {
|
644 |
return null; |
645 |
} |
646 |
return ress;
|
647 |
} catch (Throwable ex) { |
648 |
LOGGER.warn("Can't get resources for '" + this.resourcesStoreParameters.getUrl()+"&resourceName="+name + "'.", ex); |
649 |
return null; |
650 |
|
651 |
} finally {
|
652 |
DisposeUtils.disposeQuietly(store); |
653 |
} |
654 |
|
655 |
} |
656 |
|
657 |
@Override
|
658 |
public boolean add(String resourceName, InputStream is) { |
659 |
DataBaseResource res = new DataBaseResource(this, resourceName); |
660 |
try {
|
661 |
IOUtils.copy(is, res.asOutputStream()); |
662 |
res.close(); |
663 |
is.close(); |
664 |
return true; |
665 |
} catch (IOException ex) { |
666 |
return false; |
667 |
} |
668 |
} |
669 |
|
670 |
|
671 |
@Override
|
672 |
public boolean remove(String resourceName) { |
673 |
if( this.alternativeStorage!=null ) { |
674 |
try {
|
675 |
this.alternativeStorage.remove(resourceName);
|
676 |
} catch (Exception e) { |
677 |
//Do nothing
|
678 |
} |
679 |
} |
680 |
if (this.resourcesStoreParameters == null) { |
681 |
return false; |
682 |
} |
683 |
DataBaseResource resource = new DataBaseResource(this,resourceName); |
684 |
|
685 |
return resource.remove();
|
686 |
} |
687 |
|
688 |
void clearCache() throws IOException { |
689 |
byte[] data = this.resourcesStoreParameters.toByteArray(); |
690 |
|
691 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
692 |
File f = fm.getTemporaryFile("resources-storage", "jdbc", DigestUtils.md5Hex(data)); |
693 |
FileUtils.deleteDirectory(f); |
694 |
|
695 |
} |
696 |
|
697 |
@Override
|
698 |
protected void doDispose() throws BaseException { |
699 |
super.doDispose();
|
700 |
DisposeUtils.disposeQuietly(this.helper);
|
701 |
} |
702 |
|
703 |
} |