Revision 44397 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/impl/DefaultDatabaseWorkspaceManager.java
DefaultDatabaseWorkspaceManager.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.impl; |
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 |
import java.util.ArrayList; |
|
5 |
import java.util.Collection; |
|
6 |
import java.util.Collections; |
|
7 |
import java.util.List; |
|
4 | 8 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
5 | 9 |
import org.apache.commons.lang3.StringUtils; |
6 | 10 |
import org.apache.commons.lang3.mutable.Mutable; |
7 | 11 |
import org.apache.commons.lang3.mutable.MutableObject; |
8 | 12 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
9 | 13 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
14 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
15 |
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
|
10 | 16 |
import org.gvsig.fmap.dal.DALLocator; |
11 | 17 |
import org.gvsig.fmap.dal.DataManager; |
12 | 18 |
import org.gvsig.fmap.dal.DataServerExplorer; |
... | ... | |
33 | 39 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES; |
34 | 40 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
35 | 41 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
42 |
import org.gvsig.tools.dynobject.DynClass; |
|
43 |
import org.gvsig.tools.dynobject.DynField; |
|
44 |
import org.gvsig.tools.exception.BaseException; |
|
36 | 45 |
import org.gvsig.tools.resourcesstorage.FilesResourcesStorage; |
37 | 46 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
47 |
import org.gvsig.tools.util.FileTools; |
|
48 |
import org.gvsig.tools.util.HasAFile; |
|
49 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
50 |
import org.gvsig.tools.visitor.Visitor; |
|
38 | 51 |
import org.slf4j.Logger; |
39 | 52 |
import org.slf4j.LoggerFactory; |
40 | 53 |
|
... | ... | |
44 | 57 |
*/ |
45 | 58 |
@SuppressWarnings("UseSpecificCatch") |
46 | 59 |
public class DefaultDatabaseWorkspaceManager |
47 |
implements DatabaseWorkspaceManager |
|
60 |
extends AbstractSymbolTable |
|
61 |
implements DatabaseWorkspaceManager |
|
48 | 62 |
{ |
49 | 63 |
|
50 | 64 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
65 |
|
|
66 |
private static final String CONFIG_NAME_BASEFOLDER = "BASEFOLDER"; |
|
67 |
private static final String CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH = "ALTERNATIVE_RESOURCES_PATH"; |
|
51 | 68 |
|
69 |
private static abstract class CachedValue<T> { |
|
70 |
|
|
71 |
protected T value = null; |
|
72 |
private long lastAccess = 0; |
|
73 |
private final int millis; |
|
74 |
|
|
75 |
protected CachedValue(int millis) { |
|
76 |
this.millis = millis; |
|
77 |
} |
|
78 |
|
|
79 |
protected abstract void reload(); |
|
80 |
|
|
81 |
public boolean isExpired() { |
|
82 |
long now = System.currentTimeMillis(); |
|
83 |
return now - lastAccess > millis; |
|
84 |
} |
|
85 |
|
|
86 |
public T get() { |
|
87 |
if (isExpired()) { |
|
88 |
reload(); |
|
89 |
} |
|
90 |
lastAccess = System.currentTimeMillis(); |
|
91 |
return value; |
|
92 |
} |
|
93 |
|
|
94 |
public void set(T value) { |
|
95 |
this.value = value; |
|
96 |
lastAccess = System.currentTimeMillis(); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
private class BaseFolder extends CachedValue<File> { |
|
101 |
public BaseFolder() { |
|
102 |
super(3000); |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
protected void reload() { |
|
107 |
String s = DefaultDatabaseWorkspaceManager.this.get(CONFIG_NAME_BASEFOLDER); |
|
108 |
if( StringUtils.isBlank(s) ) { |
|
109 |
this.value = null; |
|
110 |
} else { |
|
111 |
this.value = new File(s); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public void set(File value) { |
|
117 |
DefaultDatabaseWorkspaceManager.this.set(CONFIG_NAME_BASEFOLDER, value.toString()); |
|
118 |
super.set(value); |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
52 | 122 |
private final DataServerExplorerParameters serverParameters; |
53 | 123 |
private Boolean existsConfiguration = null; |
54 | 124 |
private StoresRepository storesRepository; |
55 | 125 |
private Mutable<File> alternativeResourcesFolder = null; |
126 |
private BaseFolder baseFolder = null; |
|
56 | 127 |
|
57 |
|
|
58 | 128 |
public DefaultDatabaseWorkspaceManager(DataServerExplorerParameters serverParameters) { |
59 | 129 |
this.serverParameters = serverParameters; |
130 |
this.baseFolder = new BaseFolder(); |
|
60 | 131 |
} |
61 | 132 |
|
62 | 133 |
@Override |
... | ... | |
289 | 360 |
return null; |
290 | 361 |
} |
291 | 362 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE); |
363 |
value = ExpressionUtils.evaluateDynamicText(this, value); |
|
292 | 364 |
return value; |
293 | 365 |
|
294 | 366 |
} catch (Exception ex) { |
... | ... | |
341 | 413 |
this.storesRepository = new DatabaseWorkspaceStoresRepository( |
342 | 414 |
id, |
343 | 415 |
label, |
344 |
this.serverParameters
|
|
416 |
this |
|
345 | 417 |
); |
346 | 418 |
} |
347 | 419 |
return this.storesRepository; |
... | ... | |
358 | 430 |
} |
359 | 431 |
|
360 | 432 |
@Override |
433 |
public File getBaseFolder() { |
|
434 |
return this.baseFolder.get(); |
|
435 |
} |
|
436 |
|
|
437 |
@Override |
|
438 |
public void setBaseFolder(File baseFolder) { |
|
439 |
this.baseFolder.set(baseFolder); |
|
440 |
} |
|
441 |
|
|
442 |
private DataStoreParameters replaceInFilesToUseBaseFolder(DataStoreParameters parameters) { |
|
443 |
DynClass dynClass = parameters.getDynClass(); |
|
444 |
if( dynClass == null ) { |
|
445 |
return parameters; |
|
446 |
} |
|
447 |
File theBaseFolder = this.getBaseFolder(); |
|
448 |
if( theBaseFolder==null ) { |
|
449 |
return parameters; |
|
450 |
} |
|
451 |
DataStoreParameters replaced = parameters; |
|
452 |
for (DynField dynField : dynClass.getDynFields()) { |
|
453 |
switch(dynField.getType()) { |
|
454 |
case DataTypes.FILE: |
|
455 |
case DataTypes.FOLDER: |
|
456 |
File f = (File) parameters.getDynValue(dynField.getName()); |
|
457 |
if( f!=null ) { |
|
458 |
File f2 = FileTools.relativizeFile(theBaseFolder, f); |
|
459 |
if( f2!=f ) { |
|
460 |
f = new File("<%="+CONFIG_NAME_BASEFOLDER+"%>/"+f2.getPath()); |
|
461 |
if( replaced == parameters ) { |
|
462 |
replaced = (DataStoreParameters) replaced.getCopy(); |
|
463 |
} |
|
464 |
replaced.setDynValue(dynField.getName(), f); |
|
465 |
} |
|
466 |
} |
|
467 |
break; |
|
468 |
} |
|
469 |
} |
|
470 |
return replaced; |
|
471 |
} |
|
472 |
|
|
473 |
@Override |
|
361 | 474 |
public boolean writeStoresRepositoryEntry(String name, DataStoreParameters parameters) { |
362 | 475 |
try { |
476 |
parameters = this.replaceInFilesToUseBaseFolder(parameters); |
|
363 | 477 |
byte[] data = parameters.toByteArray(); |
364 | 478 |
if( data == null ) { |
365 | 479 |
throw new RuntimeException("Can't convert parameters to byte array."); |
... | ... | |
417 | 531 |
public ResourcesStorage getAlternativeResourcesStorage(String tableName) { |
418 | 532 |
if( this.alternativeResourcesFolder==null ) { |
419 | 533 |
this.alternativeResourcesFolder = new MutableObject<>(null); |
420 |
String resourcesPath = this.get("ALTERNATIVE_RESOURCES_PATH");
|
|
534 |
String resourcesPath = this.get(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH);
|
|
421 | 535 |
if( !StringUtils.isBlank(resourcesPath) ) { |
422 | 536 |
try { |
423 | 537 |
resourcesPath = (String) ExpressionUtils.evaluate(resourcesPath); |
... | ... | |
467 | 581 |
this.set(CONFIG_NAME_STORESREPOSITORYID,id); |
468 | 582 |
this.set(CONFIG_NAME_STORESREPOSITORYLABEL,description); |
469 | 583 |
this.set(CONFIG_CAN_ANONYMOUS_USER_WRITE_IN_THE_TABLES,"true"); |
470 |
this.set("ALTERNATIVE_RESOURCES_PATH",""); |
|
584 |
this.set(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH,""); |
|
585 |
File theBaseFolder = this.getBaseFolder(); |
|
586 |
if( this.serverParameters instanceof HasAFile && theBaseFolder==null ) { |
|
587 |
File f = ((HasAFile)this.serverParameters).getFile(); |
|
588 |
if( f != null ) { |
|
589 |
theBaseFolder = f.getParentFile(); |
|
590 |
this.setBaseFolder(theBaseFolder); |
|
591 |
} else { |
|
592 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
|
593 |
} |
|
594 |
} else { |
|
595 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
|
596 |
} |
|
471 | 597 |
} |
598 |
|
|
599 |
@Override |
|
600 |
public boolean exists(String name) { |
|
601 |
try { |
|
602 |
if (this.existsConfiguration()) { |
|
603 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION); |
|
604 |
if (store != null) { |
|
605 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
606 |
String filter = builder.eq( |
|
607 |
builder.column(FIELD_CONFIGURATION_NAME), |
|
608 |
builder.constant(name) |
|
609 |
).toString(); |
|
610 |
Feature feature = store.findFirst(filter); |
|
611 |
if (feature != null) { |
|
612 |
return true; |
|
613 |
} |
|
614 |
} |
|
615 |
} |
|
616 |
return super.exists(name); |
|
617 |
} catch (Exception ex) { |
|
618 |
LOGGER.warn("Can't read configuration value '" + name + "'", ex); |
|
619 |
return false; |
|
620 |
} |
|
621 |
} |
|
622 |
|
|
623 |
@Override |
|
624 |
public Object value(String name) { |
|
625 |
String v = this.get(name); |
|
626 |
if( v!=null ) { |
|
627 |
return v; |
|
628 |
} |
|
629 |
return super.value(name); |
|
630 |
} |
|
631 |
|
|
632 |
@Override |
|
633 |
public Collection<String> localvariables() { |
|
634 |
try { |
|
635 |
final List<String> theVars = new ArrayList<>(); |
|
636 |
if (this.existsConfiguration()) { |
|
637 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION); |
|
638 |
if (store != null) { |
|
639 |
store.accept(new Visitor() { |
|
640 |
@Override |
|
641 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
|
642 |
Feature feature = (Feature) obj; |
|
643 |
theVars.add(feature.getString(FIELD_CONFIGURATION_NAME)); |
|
644 |
} |
|
645 |
}); |
|
646 |
} |
|
647 |
} |
|
648 |
return theVars; |
|
649 |
} catch (Exception ex) { |
|
650 |
LOGGER.warn("Can't read configuration variables", ex); |
|
651 |
return Collections.EMPTY_LIST; |
|
652 |
} |
|
653 |
} |
|
654 |
|
|
655 |
|
|
472 | 656 |
} |
Also available in: Unified diff