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 / VCSGisManagerImpl.java @ 2728
History | View | Annotate | Download (17.5 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright (c) 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.vcsgis.lib; |
24 |
|
25 |
import java.io.File; |
26 |
import java.net.URL; |
27 |
import java.util.Collections; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import java.util.UUID; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.DataManager; |
35 |
import org.gvsig.fmap.dal.DataServerExplorer; |
36 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
37 |
import org.gvsig.fmap.dal.DataStore; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
42 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.task.SimpleTaskStatus; |
47 |
import org.gvsig.tools.util.HasAFile; |
48 |
import org.gvsig.tools.util.ListBuilder; |
49 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
50 |
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb; |
51 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.DataRepoTable; |
52 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.EntitiesRepoTable; |
53 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.RevisionsRepoTable; |
54 |
import org.gvsig.vcsgis.lib.repository.remoteclient.VCSGisRepositoryClient; |
55 |
import org.gvsig.vcsgis.lib.server.VCSGisServerController; |
56 |
import org.gvsig.vcsgis.lib.server.VCSGisServerControllerImpl; |
57 |
import org.gvsig.vcsgis.lib.workspace.StoreProperties; |
58 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
59 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceDescriptor; |
60 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceDescriptorImpl; |
61 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceImpl; |
62 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable; |
63 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable.EntityRow; |
64 |
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable; |
65 |
import org.gvsig.vcsgis.lib.workspace.tables.VarsTable; |
66 |
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable; |
67 |
import org.slf4j.Logger; |
68 |
import org.slf4j.LoggerFactory; |
69 |
|
70 |
|
71 |
/**
|
72 |
* @author gvSIG Team
|
73 |
*
|
74 |
*/
|
75 |
@SuppressWarnings("UseSpecificCatch") |
76 |
public class VCSGisManagerImpl implements VCSGisManager { |
77 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisManagerImpl.class); |
78 |
|
79 |
|
80 |
|
81 |
public static final List<String> INTERNAL_TABLES = ListBuilder.create(WorkspaceChangesTable.TABLE_NAME, |
82 |
VarsTable.TABLE_NAME, |
83 |
EntitiesTable.TABLE_NAME, |
84 |
RemoteChangesTable.TABLE_NAME, |
85 |
RevisionsRepoTable.TABLE_NAME, |
86 |
DataRepoTable.TABLE_NAME, |
87 |
EntitiesRepoTable.TABLE_NAME, |
88 |
"gvsigd_resources",
|
89 |
"gvsigd_config"
|
90 |
); |
91 |
|
92 |
|
93 |
private Map<String,VCSGisWorkspaceDescriptor> workspaces; |
94 |
private VCSGisCodeGenerator codeGenerator;
|
95 |
|
96 |
public VCSGisManagerImpl() {
|
97 |
this.workspaces = new HashMap<>(); |
98 |
this.codeGenerator = new RandomCodeGenerator(); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public Map<String, VCSGisWorkspaceDescriptor> getWorkspaces() { |
103 |
return Collections.unmodifiableMap(this.workspaces); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void restoreWorkspaces(Map<String, VCSGisWorkspaceDescriptor> descriptors) { |
108 |
this.workspaces = new HashMap<>(); |
109 |
if( descriptors != null ) { |
110 |
this.workspaces.putAll(descriptors);
|
111 |
} |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public int initWorkspace(File wsfile, VCSGisRepository repo, String label, SimpleTaskStatus status) { |
116 |
int err = ERR_NO_ERROR;
|
117 |
if (wsfile == null) { |
118 |
return ERR_DBFILE_IS_NULL;
|
119 |
} |
120 |
if (status == null) { |
121 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
122 |
status.add(); |
123 |
} |
124 |
status.push(); |
125 |
DataManager dataManager = DALLocator.getDataManager(); |
126 |
try {
|
127 |
wsfile = removeH2FileExtension(wsfile); |
128 |
if (StringUtils.isBlank(label)) {
|
129 |
label = wsfile.getName(); |
130 |
} |
131 |
err = ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER; |
132 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
133 |
((HasAFile) explorerParams).setFile(wsfile); |
134 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
135 |
explorerParams.getProviderName(), |
136 |
explorerParams |
137 |
); |
138 |
return this.initWorkspace(explorer, repo, label, status); |
139 |
} catch (Exception ex) { |
140 |
status.abort(); |
141 |
LOGGER.warn("Can't init workspace in '" + wsfile.getAbsolutePath() + "'.", ex); |
142 |
status.message(ex.getMessage()); |
143 |
return err;
|
144 |
} finally {
|
145 |
status.pop(); |
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public int initWorkspace(JDBCServerExplorer wsexplorer, VCSGisRepository repository, String label, SimpleTaskStatus status) { |
151 |
int err = ERR_NO_ERROR;
|
152 |
if (wsexplorer == null) { |
153 |
return ERR_WSEXPLORER_IS_NULL;
|
154 |
} |
155 |
if (StringUtils.isBlank(label)) {
|
156 |
return ERR_LABEL_IS_NULL;
|
157 |
} |
158 |
if (status == null) { |
159 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
160 |
status.add(); |
161 |
} |
162 |
status.push(); |
163 |
DataManager dataManager = DALLocator.getDataManager(); |
164 |
try {
|
165 |
status.setTitle("vcsgis init_workspace");
|
166 |
FeatureType[] tables = new FeatureType[]{ |
167 |
VarsTable.featureType(), |
168 |
EntitiesTable.featureType(), |
169 |
WorkspaceChangesTable.featureType(), |
170 |
RemoteChangesTable.featureType() |
171 |
}; |
172 |
status.setRangeOfValues(0, tables.length);
|
173 |
int step = 1; |
174 |
for (FeatureType table : tables) {
|
175 |
status.message(table.getLabel()); |
176 |
status.setCurValue(step++); |
177 |
err = ERR_CANT_CREATE_TABLE + step; |
178 |
JDBCNewStoreParameters table_params = wsexplorer.getAddParameters(); |
179 |
table_params.setDefaultFeatureType(table.getEditable()); |
180 |
table_params.setTable(table.getTags().getString("ID"));
|
181 |
wsexplorer.add(wsexplorer.getProviderName(), table_params, false);
|
182 |
} |
183 |
status.setTitle("vcsgis retrieving entities");
|
184 |
VCSGisWorkspaceImpl workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator, repository, label); |
185 |
workspace.initialize(); |
186 |
|
187 |
this.registerWorkspace(workspace);
|
188 |
status.terminate(); |
189 |
return ERR_NO_ERROR;
|
190 |
} catch (Exception ex) { |
191 |
status.abort(); |
192 |
LOGGER.warn("Can't init workspace in '" + getMessageLabel(wsexplorer) + "'.", ex); |
193 |
status.message(ex.getMessage()); |
194 |
return err;
|
195 |
} finally {
|
196 |
status.pop(); |
197 |
} |
198 |
} |
199 |
|
200 |
private String getMessageLabel(JDBCServerExplorer explorer) { |
201 |
return explorer.getParameters().getUrl();
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public VCSGisWorkspace openWorkspace(File wsfile) { |
206 |
if (wsfile == null) { |
207 |
return null; |
208 |
} |
209 |
try {
|
210 |
DataManager dataManager = DALLocator.getDataManager(); |
211 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
212 |
((HasAFile) explorerParams).setFile(wsfile); |
213 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
214 |
explorerParams.getProviderName(), |
215 |
explorerParams |
216 |
); |
217 |
VCSGisWorkspace workspace = openWorkspace(explorer); |
218 |
return workspace;
|
219 |
} catch (Exception ex) { |
220 |
return null; |
221 |
} |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public VCSGisWorkspace openWorkspace(JDBCServerExplorer wsexplorer) {
|
226 |
if (wsexplorer == null) { |
227 |
return null; |
228 |
} |
229 |
try {
|
230 |
VCSGisWorkspace workspace; |
231 |
|
232 |
JDBCServerExplorerParameters params = wsexplorer.getParameters(); |
233 |
String url = params.getUrl();
|
234 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
235 |
if (value.isWorkspaceInitialized() && StringUtils.equals(url, value.getExplorerParameters().getUrl())) {
|
236 |
workspace = value.getWorkspace(); |
237 |
// LOGGER.debug("===: OPEN WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')");
|
238 |
return workspace;
|
239 |
} |
240 |
} |
241 |
|
242 |
workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator); |
243 |
|
244 |
this.registerWorkspace(workspace);
|
245 |
return workspace;
|
246 |
} catch (Exception ex) { |
247 |
return null; |
248 |
} |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public void registerWorkspace(VCSGisWorkspace workspace) { |
253 |
VCSGisWorkspaceDescriptorImpl descriptor = new VCSGisWorkspaceDescriptorImpl(workspace);
|
254 |
this.workspaces.put(descriptor.getCode(), descriptor);
|
255 |
DataManager dataManager = DALLocator.getDataManager(); |
256 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
257 |
if( !pool.contains(workspace.getLabel()) ) {
|
258 |
pool.add(workspace.getLabel(), workspace.getExplorerParameters()); |
259 |
} |
260 |
// LOGGER.debug("===: REGISTER WORKSPACE "+ hexId(workspace)+ " (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')");
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void deregisterWorkspace(VCSGisWorkspace workspace) { |
265 |
// VCSGisWorkspaceDescriptorImpl descriptor = new VCSGisWorkspaceDescriptorImpl(workspace);
|
266 |
// this.workspaces.remove(descriptor.getCode());
|
267 |
this.workspaces.remove(workspace.getCode());
|
268 |
} |
269 |
|
270 |
@Override
|
271 |
public VCSGisWorkspace getWorkspace(FeatureStore store) {
|
272 |
try {
|
273 |
VCSGisWorkspace workspace; |
274 |
switch(StoreProperties.getVCSMode(store)) {
|
275 |
case StoreProperties.MODE_NOT_VCS:
|
276 |
return null; |
277 |
|
278 |
case StoreProperties.MODE_VCS:
|
279 |
String workspace_code = StoreProperties.getWorkspaceCode(store);
|
280 |
VCSGisWorkspaceDescriptor workspaceDescriptor = this.workspaces.get(workspace_code);
|
281 |
if( workspaceDescriptor!=null ) { |
282 |
workspace = workspaceDescriptor.getWorkspace(); |
283 |
if (workspace != null) { |
284 |
String entity_name = StoreProperties.getEntityName(store);
|
285 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
286 |
if (entity == null) { |
287 |
StoreProperties.setNotVCSMode(store); |
288 |
return null; |
289 |
} |
290 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from property (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
291 |
return workspace;
|
292 |
} |
293 |
} |
294 |
// break;
|
295 |
|
296 |
case StoreProperties.MODE_UNKNOWN:
|
297 |
default:
|
298 |
DataStoreParameters params = store.getParameters(); |
299 |
if(!(params instanceof JDBCStoreParameters)) { |
300 |
StoreProperties.setNotVCSMode(store); |
301 |
return null; |
302 |
} |
303 |
String entity_name = ((JDBCStoreParameters) params).getTable();
|
304 |
|
305 |
if (INTERNAL_TABLES.contains(entity_name)) {
|
306 |
StoreProperties.setNotVCSMode(store); |
307 |
return null; |
308 |
} |
309 |
|
310 |
String storeUrl = ((JDBCStoreParameters)params).getUrl();
|
311 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
312 |
if (StringUtils.equals(storeUrl, value.getExplorerParameters().getUrl())) {
|
313 |
workspace = value.getWorkspace(); |
314 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
315 |
if (entity == null) { |
316 |
StoreProperties.setNotVCSMode(store); |
317 |
return null; |
318 |
} |
319 |
StoreProperties.set(store, workspace, entity_name); |
320 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
321 |
return workspace;
|
322 |
} |
323 |
|
324 |
} |
325 |
|
326 |
workspace = openWorkspace((JDBCServerExplorer)store.getExplorer()); |
327 |
if (workspace == null) { |
328 |
StoreProperties.setNotVCSMode(store); |
329 |
return null; |
330 |
} |
331 |
this.registerWorkspace(workspace);
|
332 |
|
333 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
334 |
if (entity == null) { |
335 |
StoreProperties.setNotVCSMode(store); |
336 |
return null; |
337 |
} |
338 |
StoreProperties.set(store, workspace, entity_name); |
339 |
return workspace;
|
340 |
} |
341 |
} catch (Exception ex) { |
342 |
LOGGER.warn("Can't get '" +store.getName()+ "'store's workspace ", ex); |
343 |
StoreProperties.setNotVCSMode(store); |
344 |
return null; |
345 |
} |
346 |
} |
347 |
|
348 |
@Override
|
349 |
public boolean isWorkspace(JDBCServerExplorerParameters params) { |
350 |
try {
|
351 |
DataManager dataManager = DALLocator.getDataManager(); |
352 |
DataServerExplorer explorer = dataManager.openServerExplorer(params.getProviderName(), params); |
353 |
DataStoreParameters x = explorer.get(VarsTable.TABLE_NAME); |
354 |
if(x==null) { |
355 |
return false; |
356 |
} |
357 |
return explorer.exists(x);
|
358 |
} catch (Exception ex) { |
359 |
return false; |
360 |
} |
361 |
} |
362 |
|
363 |
@Override
|
364 |
public boolean isWorkspace(File dbfile) { |
365 |
try {
|
366 |
DataManager dataManager = DALLocator.getDataManager(); |
367 |
JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
368 |
((HasAFile)params).setFile(dbfile); |
369 |
return isWorkspace(params);
|
370 |
} catch (Exception ex) { |
371 |
return false; |
372 |
} |
373 |
} |
374 |
|
375 |
private File removeH2FileExtension(File f) { |
376 |
if (f == null) { |
377 |
return null; |
378 |
} |
379 |
String pathname = f.getAbsolutePath();
|
380 |
String lpathname = pathname.toLowerCase();
|
381 |
for (String ext : new String[]{".mv.db", ".mv", ".trace.db", ".trace"}) { |
382 |
if (lpathname.endsWith(ext)) {
|
383 |
pathname = pathname.substring(0, pathname.length() - ext.length());
|
384 |
return new File(pathname); |
385 |
} |
386 |
} |
387 |
return f;
|
388 |
} |
389 |
|
390 |
public static String hexId(Object obj) { |
391 |
if( obj==null ) { |
392 |
return "NULL"; |
393 |
} |
394 |
return Integer.toHexString(obj.hashCode()).toUpperCase(); |
395 |
} |
396 |
|
397 |
@Override
|
398 |
public VCSGisWorkspaceDescriptor getWorkspaceDescriptor(String code) { |
399 |
return this.workspaces.get(code); |
400 |
} |
401 |
|
402 |
@Override
|
403 |
public int initRepository(JDBCServerExplorerParameters repositoryParameters, SimpleTaskStatus status) { |
404 |
return VCSGisRepositoryLocaldb.create(repositoryParameters, this.codeGenerator, status); |
405 |
} |
406 |
|
407 |
@Override
|
408 |
public VCSGisRepository openRepository(JDBCServerExplorerParameters repositoryParameters) {
|
409 |
return new VCSGisRepositoryLocaldb(repositoryParameters, codeGenerator); |
410 |
} |
411 |
|
412 |
@Override
|
413 |
public VCSGisRepository openRepository(URL repository) { |
414 |
return new VCSGisRepositoryClient(repository); |
415 |
} |
416 |
|
417 |
@Override
|
418 |
public VCSGisServerController createServerController(VCSGisRepository repository) {
|
419 |
return new VCSGisServerControllerImpl(repository); |
420 |
} |
421 |
|
422 |
@Override
|
423 |
public void setCodeGenerator(VCSGisCodeGenerator generator) { |
424 |
this.codeGenerator = generator;
|
425 |
} |
426 |
|
427 |
@Override
|
428 |
public VCSGisCodeGenerator getCodeGenerator() {
|
429 |
return this.codeGenerator; |
430 |
} |
431 |
|
432 |
} |