Revision 45739 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
JDBCResourcesStorage.java | ||
---|---|---|
34 | 34 |
import java.io.OutputStream; |
35 | 35 |
import java.net.URL; |
36 | 36 |
import java.util.ArrayList; |
37 |
import java.util.HashMap; |
|
37 | 38 |
import java.util.List; |
39 |
import java.util.Map; |
|
38 | 40 |
import org.apache.commons.codec.digest.DigestUtils; |
39 | 41 |
import org.apache.commons.io.FileUtils; |
40 | 42 |
import org.apache.commons.io.IOUtils; |
... | ... | |
58 | 60 |
import org.gvsig.tools.folders.FoldersManager; |
59 | 61 |
import org.gvsig.tools.resourcesstorage.AbstractResourcesStorage; |
60 | 62 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
63 |
import org.gvsig.tools.util.CachedValue; |
|
61 | 64 |
import org.slf4j.Logger; |
62 | 65 |
import org.slf4j.LoggerFactory; |
63 | 66 |
|
... | ... | |
70 | 73 |
|
71 | 74 |
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCResourcesStorage.class); |
72 | 75 |
|
76 |
private static class ExistsResourcesCache extends CachedValue<Map<String,Boolean>> { |
|
77 |
@Override |
|
78 |
protected void reload() { |
|
79 |
this.setValue(new HashMap<>()); |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
73 | 83 |
private static class DataBaseResource implements Resource { |
74 | 84 |
|
75 | 85 |
private class ResourceInputStream extends InputStream { |
... | ... | |
103 | 113 |
} |
104 | 114 |
} |
105 | 115 |
|
106 |
private final JDBCStoreParameters storeParameters; |
|
107 |
private final String tableName; |
|
108 | 116 |
private final String name; |
109 |
private final boolean readonly;
|
|
117 |
private final JDBCResourcesStorage storage;
|
|
110 | 118 |
|
111 | 119 |
private InputStream in; |
112 | 120 |
private OutputStream out; |
113 | 121 |
|
114 |
public DataBaseResource(JDBCStoreParameters storeParameters, String tableName, String name, boolean readonly) { |
|
115 |
this.storeParameters = storeParameters; |
|
116 |
this.tableName = tableName; |
|
122 |
public DataBaseResource(JDBCResourcesStorage storage, String name) { |
|
123 |
this.storage = storage; |
|
117 | 124 |
this.name = name; |
118 |
this.readonly = readonly; |
|
119 | 125 |
} |
120 | 126 |
|
121 | 127 |
@Override |
... | ... | |
123 | 129 |
return name; |
124 | 130 |
} |
125 | 131 |
|
132 |
private JDBCStoreParameters getStoreParameters() { |
|
133 |
return this.storage.resourcesStoreParameters; |
|
134 |
} |
|
135 |
|
|
136 |
private String getTableName() { |
|
137 |
return this.storage.tableName; |
|
138 |
} |
|
139 |
|
|
140 |
private Map<String,Boolean> getExistsResourcesCache() { |
|
141 |
return this.storage.existsResourcesCache.get(); |
|
142 |
} |
|
143 |
|
|
126 | 144 |
@Override |
127 | 145 |
public boolean isReadOnly() { |
128 |
return false;
|
|
146 |
return this.storage.readonly;
|
|
129 | 147 |
} |
130 | 148 |
@Override |
131 | 149 |
public URL getURL() { |
132 | 150 |
try { |
133 |
String url = this.storeParameters.getUrl();
|
|
134 |
return new URL(url + "&tableName=" + this.tableName + "&resourceName=" + this.name);
|
|
151 |
String url = this.getStoreParameters().getUrl();
|
|
152 |
return new URL(url + "&tableName=" + this.getTableName() + "&resourceName=" + this.name);
|
|
135 | 153 |
} catch (Throwable ex) { |
136 | 154 |
return null; |
137 | 155 |
} |
... | ... | |
143 | 161 |
try { |
144 | 162 |
FoldersManager fm = ToolsLocator.getFoldersManager(); |
145 | 163 |
Pair<String, String> key = this.getCacheID(); |
164 |
String key_s = key.getLeft() + "/"+ key.getRight(); |
|
165 |
Map<String, Boolean> erc = this.getExistsResourcesCache(); |
|
166 |
if( erc.containsKey(key_s) ) { |
|
167 |
return this.getExistsResourcesCache().get(key_s); |
|
168 |
} |
|
146 | 169 |
File f = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), key.getRight()); |
147 | 170 |
if( f.exists() ) { |
148 | 171 |
return true; |
149 | 172 |
} |
150 | 173 |
DataManager dataManager = DALLocator.getDataManager(); |
151 | 174 |
store = (FeatureStore) dataManager.openStore( |
152 |
this.storeParameters.getDataStoreName(),
|
|
153 |
this.storeParameters
|
|
175 |
this.getStoreParameters().getDataStoreName(),
|
|
176 |
this.getStoreParameters()
|
|
154 | 177 |
); |
155 | 178 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
156 | 179 |
String filter = builder.eq( |
157 | 180 |
builder.column(FIELD_RESOURCES_NAME), |
158 |
builder.constant(this.tableName+"."+this.name)
|
|
181 |
builder.constant(this.getTableName()+"."+this.name)
|
|
159 | 182 |
).toString(); |
160 | 183 |
Feature feature = store.findFirst(filter); |
161 |
return feature!=null; |
|
184 |
boolean exists = feature!=null; |
|
185 |
this.getExistsResourcesCache().put(key_s, exists); |
|
186 |
return exists; |
|
162 | 187 |
} catch (Throwable ex) { |
163 | 188 |
LOGGER.warn("Can't access to the resoure '" + this.getURL() + "'.", ex); |
164 | 189 |
} finally { |
... | ... | |
178 | 203 |
} |
179 | 204 |
DataManager dataManager = DALLocator.getDataManager(); |
180 | 205 |
store = (FeatureStore) dataManager.openStore( |
181 |
this.storeParameters.getDataStoreName(),
|
|
182 |
this.storeParameters
|
|
206 |
this.getStoreParameters().getDataStoreName(),
|
|
207 |
this.getStoreParameters()
|
|
183 | 208 |
); |
184 | 209 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
185 | 210 |
String filter = builder.eq( |
186 | 211 |
builder.column(FIELD_RESOURCES_NAME), |
187 |
builder.constant(this.tableName+"."+this.name)
|
|
212 |
builder.constant(this.getTableName()+"."+this.name)
|
|
188 | 213 |
).toString(); |
189 | 214 |
Feature feature = store.findFirst(filter); |
190 | 215 |
if(feature != null){ |
... | ... | |
204 | 229 |
} |
205 | 230 |
|
206 | 231 |
private Pair<String,String> getCacheID() { |
207 |
byte[] data = this.storeParameters.toByteArray();
|
|
232 |
byte[] data = this.getStoreParameters().toByteArray();
|
|
208 | 233 |
ImmutablePair<String, String> r = new ImmutablePair<>( |
209 | 234 |
DigestUtils.md5Hex(data), |
210 |
this.tableName+"."+this.name
|
|
235 |
this.getTableName()+"."+this.name
|
|
211 | 236 |
); |
212 | 237 |
return r; |
213 | 238 |
} |
... | ... | |
244 | 269 |
IOUtils.copy(is, os); |
245 | 270 |
|
246 | 271 |
f2 = fm.getTemporaryFile("resources-storage","jdbc", key.getLeft(), "parameters"); |
247 |
byte[] data = this.storeParameters.toByteArray();
|
|
272 |
byte[] data = this.getStoreParameters().toByteArray();
|
|
248 | 273 |
os = new FileOutputStream(f2); |
249 | 274 |
IOUtils.write(data, os); |
250 | 275 |
} catch (IOException ex) { |
... | ... | |
269 | 294 |
try { |
270 | 295 |
DataManager dataManager = DALLocator.getDataManager(); |
271 | 296 |
store = (FeatureStore) dataManager.openStore( |
272 |
this.storeParameters.getDataStoreName(),
|
|
273 |
this.storeParameters
|
|
297 |
this.getStoreParameters().getDataStoreName(),
|
|
298 |
this.getStoreParameters()
|
|
274 | 299 |
); |
275 | 300 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
276 | 301 |
String filter = builder.eq( |
277 | 302 |
builder.column(FIELD_RESOURCES_NAME), |
278 |
builder.constant(this.tableName+"."+this.name)
|
|
303 |
builder.constant(this.getTableName()+"."+this.name)
|
|
279 | 304 |
).toString(); |
280 | 305 |
Feature feature = store.findFirst(filter); |
281 | 306 |
if (feature == null) { |
... | ... | |
313 | 338 |
if (this.in != null || this.out != null) { |
314 | 339 |
throw new IllegalStateException("Resource is already open (" + this.getURL() + ")."); |
315 | 340 |
} |
316 |
if( this.readonly ) {
|
|
317 |
this.out = new NullOutputStream();
|
|
341 |
if( this.isReadOnly()) {
|
|
342 |
this.out = NullOutputStream.NULL_OUTPUT_STREAM;
|
|
318 | 343 |
} else { |
319 | 344 |
this.out = new ByteArrayOutputStream(); |
320 | 345 |
} |
... | ... | |
327 | 352 |
IOUtils.closeQuietly(this.in); |
328 | 353 |
this.in = null; |
329 | 354 |
} |
330 |
if (!this.readonly && this.out != null) {
|
|
355 |
if (!this.isReadOnly()&& this.out != null) {
|
|
331 | 356 |
FeatureStore store = null; |
332 | 357 |
try { |
333 | 358 |
DataManager dataManager = DALLocator.getDataManager(); |
334 | 359 |
store = (FeatureStore) dataManager.openStore( |
335 |
this.storeParameters.getDataStoreName(),
|
|
336 |
this.storeParameters
|
|
360 |
this.getStoreParameters().getDataStoreName(),
|
|
361 |
this.getStoreParameters()
|
|
337 | 362 |
); |
338 | 363 |
store.edit(); |
339 | 364 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
340 | 365 |
String filter = builder.eq( |
341 | 366 |
builder.column(FIELD_RESOURCES_NAME), |
342 |
builder.constant(this.tableName+"."+this.name)
|
|
367 |
builder.constant(this.getTableName()+"."+this.name)
|
|
343 | 368 |
).toString(); |
344 | 369 |
Feature feature = store.findFirst(filter); |
345 | 370 |
EditableFeature efeature; |
346 | 371 |
if (feature == null) { |
347 | 372 |
efeature = store.createNewFeature(); |
348 |
efeature.set(FIELD_RESOURCES_NAME, this.tableName+"."+this.name);
|
|
373 |
efeature.set(FIELD_RESOURCES_NAME, this.getTableName()+"."+this.name);
|
|
349 | 374 |
efeature.set(FIELD_RESOURCES_RESOURCE, ((ByteArrayOutputStream)this.out).toByteArray()); |
350 | 375 |
store.insert(efeature); |
351 | 376 |
} else { |
... | ... | |
369 | 394 |
private final JDBCStoreParameters resourcesStoreParameters; |
370 | 395 |
private final String tableName; |
371 | 396 |
private final boolean readonly; |
397 |
private final ExistsResourcesCache existsResourcesCache; |
|
372 | 398 |
|
373 | 399 |
public JDBCResourcesStorage( |
374 | 400 |
ResourcesStorage alternativeStorge, |
... | ... | |
394 | 420 |
this.resourcesStoreParameters = resourcesStoreParameters; |
395 | 421 |
} |
396 | 422 |
this.tableName = tableName; |
423 |
this.existsResourcesCache = new ExistsResourcesCache(); |
|
424 |
this.existsResourcesCache.setExpireTime(60000); // 1 min |
|
397 | 425 |
} |
398 | 426 |
|
399 | 427 |
@Override |
... | ... | |
412 | 440 |
if (this.resourcesStoreParameters == null) { |
413 | 441 |
return null; |
414 | 442 |
} |
415 |
return new DataBaseResource( |
|
416 |
this.resourcesStoreParameters, |
|
417 |
this.tableName, |
|
418 |
name, |
|
419 |
this.readonly |
|
420 |
); |
|
443 |
return new DataBaseResource(this,name); |
|
421 | 444 |
} |
422 | 445 |
|
423 | 446 |
@Override |
... | ... | |
456 | 479 |
if( feature==null ) { |
457 | 480 |
break; |
458 | 481 |
} |
459 |
ress.add(new DataBaseResource( |
|
460 |
this.resourcesStoreParameters, |
|
461 |
this.tableName, |
|
462 |
multiresourceName, |
|
463 |
this.readonly |
|
464 |
) |
|
465 |
); |
|
482 |
ress.add(new DataBaseResource(this,multiresourceName)); |
|
466 | 483 |
n++; |
467 | 484 |
} |
468 | 485 |
if (ress.isEmpty()) { |
... | ... | |
491 | 508 |
if (this.resourcesStoreParameters == null) { |
492 | 509 |
return false; |
493 | 510 |
} |
494 |
DataBaseResource resource = new DataBaseResource( |
|
495 |
this.resourcesStoreParameters, |
|
496 |
this.tableName, |
|
497 |
resourceName, |
|
498 |
this.readonly |
|
499 |
); |
|
511 |
DataBaseResource resource = new DataBaseResource(this,resourceName); |
|
500 | 512 |
|
501 | 513 |
return resource.remove(); |
502 | 514 |
} |
Also available in: Unified diff