Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / workspace / VCSGisWorkspaceDescriptorImpl.java @ 3947

History | View | Annotate | Download (6.54 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, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

    
23
package org.gvsig.vcsgis.lib.workspace;
24

    
25
import java.util.Date;
26
import org.gvsig.fmap.dal.DALLocator;
27
import org.gvsig.fmap.dal.DataManager;
28
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
29
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dispose.DisposeUtils;
32
import org.gvsig.tools.dispose.impl.AbstractDisposable;
33
import org.gvsig.tools.dynobject.DynStruct;
34
import org.gvsig.tools.exception.BaseException;
35
import org.gvsig.tools.persistence.PersistenceManager;
36
import org.gvsig.tools.persistence.PersistentState;
37
import org.gvsig.tools.persistence.exception.PersistenceException;
38
import org.gvsig.vcsgis.lib.VCSGisLocator;
39
import org.gvsig.vcsgis.lib.VCSGisManager;
40

    
41
/**
42
 *
43
 * @author gvSIG Team
44
 */
45
@SuppressWarnings("UseSpecificCatch")
46
public class VCSGisWorkspaceDescriptorImpl extends AbstractDisposable implements VCSGisWorkspaceDescriptor 
47
    {
48

    
49
    private String label;
50
    private String code;
51
    private JDBCServerExplorerParameters explorerParameters;
52

    
53
    private JDBCServerExplorer explorer;
54
    private VCSGisWorkspace workspace;
55
    private long lastUse;
56

    
57
    public VCSGisWorkspaceDescriptorImpl() {
58
        
59
    }
60

    
61
    public VCSGisWorkspaceDescriptorImpl(VCSGisWorkspace workspace) {
62
        setWorkspace(workspace);
63
    }
64

    
65
    @Override
66
    public String getCode() {
67
        return this.code;
68
    }
69
    
70
    @Override
71
    public boolean isWorkspaceInitialized() {
72
        return this.workspace != null;
73
    }
74

    
75
    @Override
76
    public VCSGisWorkspace getWorkspace() {
77
        if( this.workspace == null ) {
78
            VCSGisManager manager = VCSGisLocator.getVCSGisManager();
79
            this.workspace = manager.openWorkspace(this.getExplorer());
80
        }
81
        lastUse = new Date().getTime();
82
        DisposeUtils.bind(this.workspace);
83
        return this.workspace;
84
    }
85

    
86
    @Override
87
    public String getLabel() {
88
        return this.label;
89
    }
90

    
91
    @Override
92
    public VCSGisWorkspaceDescriptor getValue() {
93
        return this;
94
    }
95

    
96
    @Override
97
    public JDBCServerExplorer getExplorer() {
98
        if (this.explorer == null) {
99
            if (this.workspace == null) {
100
                try {
101
                    DataManager manager = DALLocator.getDataManager();
102
                    this.explorer = (JDBCServerExplorer) manager.openServerExplorer(
103
                            this.explorerParameters.getProviderName(),
104
                            this.explorerParameters
105
                    );
106
                } catch (Exception ex) {
107
                    throw new RuntimeException(ex);
108
                }
109
            } else {
110
                this.explorer = this.workspace.getExplorer();
111
            }
112
//            DisposeUtils.bind(this.explorer);
113
        }
114
        DisposeUtils.bind(this.explorer);
115
        return this.explorer;
116
    }
117

    
118
    @Override
119
    public JDBCServerExplorerParameters getExplorerParameters() {
120
        return this.explorerParameters;
121
    }
122

    
123
    @Override
124
    public void saveToState(PersistentState state) throws PersistenceException {
125
        state.set("label", this.label);
126
        state.set("code", this.code);
127
        state.set("explorerParameters", this.explorerParameters);
128
    }
129

    
130
    @Override
131
    public void loadFromState(PersistentState state) throws PersistenceException {
132
        this.code = state.getString("code");
133
        this.label = state.getString("label");
134
        this.explorerParameters = (JDBCServerExplorerParameters) state.get("explorerParameters");
135
    }
136

    
137
    public static void selfRegister() {
138
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
139

    
140
        if (manager.getDefinition("VCSGisWorkspaceDescriptor")== null) {
141
            DynStruct definition = manager.addDefinition(VCSGisWorkspaceDescriptorImpl.class,
142
                    "VCSGisWorkspaceDescriptor",
143
                    "VCSGisWorkspaceDescriptor persistent definition",
144
                    null,
145
                    null
146
            );
147
            definition.addDynFieldString("code");
148
            definition.addDynFieldString("label");
149
            definition.addDynFieldObject("explorerParameters")
150
                .setClassOfValue(JDBCServerExplorerParameters.class)
151
                .setMandatory(true);
152
        }
153
        
154
    }
155

    
156
    @Override
157
    public String toString() {
158
        return this.getLabel();
159
    }
160

    
161
    /**
162
     * Only for testing purposes
163
     * @param workspace
164
     */
165
    public void setWorkspace(VCSGisWorkspace workspace) {
166
        if(workspace == null){
167
            throw new IllegalArgumentException("workspace can't be null.");
168
        }
169
        DisposeUtils.disposeQuietly(this.workspace);
170
        DisposeUtils.disposeQuietly(this.explorer);
171
        this.workspace = workspace;
172
        DisposeUtils.bind(this.workspace);
173
        this.explorerParameters = workspace.getExplorerParameters();
174
        this.explorer = null;
175
        this.label = workspace.getLabel();
176
        this.code = workspace.getCode();
177
        lastUse = new Date().getTime();
178
    }
179

    
180
    @Override
181
    protected void doDispose() throws BaseException {
182
        DisposeUtils.disposeQuietly(this.workspace);
183
        DisposeUtils.disposeQuietly(this.explorer);
184
        this.explorerParameters = null;
185
        this.explorer = null;
186
        this.workspace = null;
187
//        this.label = null; //Don't null for debugging purposses
188
        this.code = null;
189

    
190
    }
191
    
192
    public void dropExpiredCaches() {
193
        long now = new Date().getTime();
194
        if (now > this.lastUse+60000) {
195
            dropCaches();
196
        }
197
        
198
    }
199
    
200
    public void dropCaches() {
201
        DisposeUtils.disposeQuietly(this.workspace);
202
        DisposeUtils.disposeQuietly(this.explorer);
203
        this.workspace = null;
204
        this.explorer = null;
205
    }
206
    
207
}