Revision 44750 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

View differences:

DefaultDatabaseWorkspaceManager.java
5 5
import java.util.Collection;
6 6
import java.util.Collections;
7 7
import java.util.List;
8
import java.util.Map;
8 9
import java.util.Objects;
10
import org.apache.commons.collections4.map.LRUMap;
9 11
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
10 12
import org.apache.commons.lang3.StringUtils;
11 13
import org.apache.commons.lang3.mutable.Mutable;
......
41 43
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
42 44
import org.gvsig.tools.dynobject.DynClass;
43 45
import org.gvsig.tools.dynobject.DynField;
44
import org.gvsig.tools.exception.BaseException;
45 46
import org.gvsig.tools.resourcesstorage.FilesResourcesStorage;
46 47
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
48
import org.gvsig.tools.util.CachedValue;
47 49
import org.gvsig.tools.util.FileTools;
48 50
import org.gvsig.tools.util.HasAFile;
49
import org.gvsig.tools.visitor.VisitCanceledException;
50
import org.gvsig.tools.visitor.Visitor;
51 51
import org.slf4j.Logger;
52 52
import org.slf4j.LoggerFactory;
53 53

  
......
66 66
    private static final String CONFIG_NAME_BASEFOLDER = "BASEFOLDER";
67 67
    private static final String CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH = "ALTERNATIVE_RESOURCES_PATH";
68 68
    
69
    private static abstract class CachedValue<T> {
69
    private class CachedConfigValue extends CachedValue<String> {
70 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;
71
        private final String name;
72
      
73
        public CachedConfigValue(String name, String value) {
74
          this.setValue(value);
75
          this.name = name;
77 76
        }
78
        
79
        protected abstract void reload();
80

  
81
        public boolean isExpired() {
82
            long now = System.currentTimeMillis();
83
            return now - lastAccess > millis;
77
      
78
        public CachedConfigValue(String name, String value, long expireTime) {
79
          this.name = name;
80
          this.setValue(value);
81
          this.setExpireTime(expireTime);
84 82
        }
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

  
83
      
105 84
        @Override
106 85
        protected void reload() {
107
            String s = DefaultDatabaseWorkspaceManager.this.get(CONFIG_NAME_BASEFOLDER);
86
            String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(name);
108 87
            if( StringUtils.isBlank(s) ) {
109
                this.value = null;
88
                this.setValue(null);
110 89
            } else {
111
                this.value = new File(s);
90
                this.setValue(s);
112 91
            }
113 92
        }
114

  
115
        @Override
116
        public void set(File value) {
117
            DefaultDatabaseWorkspaceManager.this.set(CONFIG_NAME_BASEFOLDER, value.toString());
118
            super.set(value);
119
        }
120 93
    }
121 94
    
122 95
    private final DataServerExplorerParameters serverParameters;
123 96
    private Boolean existsConfiguration = null;
124 97
    private StoresRepository storesRepository;
125 98
    private Mutable<File> alternativeResourcesFolder = null;
126
    private BaseFolder baseFolder = null;
99
    private CachedValue<File> baseFolder;
100
    private Map<String,CachedConfigValue> cachedConfigValues;
127 101
    
128 102
    public DefaultDatabaseWorkspaceManager(DataServerExplorerParameters serverParameters) {
129 103
        this.serverParameters = serverParameters;
130
        this.baseFolder = new BaseFolder();
104
        this.baseFolder = new CachedValue() {
105
          @Override
106
          protected void reload() {
107
            String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(CONFIG_NAME_BASEFOLDER);
108
            if( StringUtils.isBlank(s) ) {
109
                this.setValue(null);
110
            } else {
111
                this.setValue(new File(s));
112
            }
113
          }
114
          
115
        };
116
        this.baseFolder.setExpireTime(20000); // 20seg
117
        this.cachedConfigValues = new LRUMap<>(20, 20);
131 118
    }
132 119

  
133 120
    @Override
......
337 324
            if( !this.existsConfiguration() ) {
338 325
                return null;
339 326
            }
327
            if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
328
              return this.baseFolder.get().toString();
329
            }
330
            CachedConfigValue cachedValue = this.cachedConfigValues.get(name);
331
            if( cachedValue != null ) {
332
              return cachedValue.get();
333
            }
334
            String value = this.getConfigValue(name);
335
            this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000));
336
            return value;
337
        } catch (Exception ex) {
338
            LOGGER.warn("Can't read configuration value '"+name+"'", ex);
339
            return null;
340
        }
341
    }
342
    
343
    private String getConfigValue(String name) {
344
        try {
345
            if( !this.existsConfiguration() ) {
346
                return null;
347
            }
340 348
            FeatureStore store = this.getTable(TABLE_CONFIGURATION);
341 349
            if( store == null ) {
342 350
                return null;
......
389 397
                store.update(efeature);
390 398
            }
391 399
            store.finishEditing();
400
            if( this.cachedConfigValues.containsKey(name) ) {
401
              this.cachedConfigValues.get(name).set(value);
402
            }
392 403
            return true;
393 404
        } catch (Exception ex) {
394 405
            LOGGER.warn("Can't write configuration value '"+name+"'", ex);
......
428 439
    @Override
429 440
    public void setBaseFolder(File baseFolder) {
430 441
        this.baseFolder.set(baseFolder);
442
        this.set(CONFIG_NAME_BASEFOLDER, baseFolder.toString());
431 443
    }
432 444

  
433 445
    private DataStoreParameters replaceInFilesToUseBaseFolder(DataStoreParameters parameters) {
......
595 607
    public boolean exists(String name) {
596 608
        try {
597 609
            if (this.existsConfiguration()) {
598
                FeatureStore store = this.getTable(TABLE_CONFIGURATION);
599
                if (store != null) {
600
                    ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
601
                    String filter = builder.eq(
602
                            builder.column(FIELD_CONFIGURATION_NAME),
603
                            builder.constant(name)
604
                    ).toString();
605
                    Feature feature = store.findFirst(filter);
606
                    if (feature != null) {
607
                        return true;
608
                    }
610
              if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
611
                return true;
612
              }
613
              if( this.cachedConfigValues.containsKey(name) ) {
614
                return true;
615
              }
616
              FeatureStore store = this.getTable(TABLE_CONFIGURATION);
617
              if (store != null) {
618
                  ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
619
                  String filter = builder.eq(
620
                          builder.column(FIELD_CONFIGURATION_NAME),
621
                          builder.constant(name)
622
                  ).toString();
623
                  Feature feature = store.findFirst(filter);
624
                  if (feature != null) {
625
                      String value = feature.getString(FIELD_CONFIGURATION_VALUE);
626
                      value = ExpressionUtils.evaluateDynamicText(this, value);
627
                      this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000));
628
                      return true;
629
                  }
609 630
                }
610 631
            }
611 632
            return super.exists(name);

Also available in: Unified diff