Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / OnlineManagerImpl.java @ 9512

History | View | Annotate | Download (26.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.online.lib.impl;
25

    
26
import java.io.File;
27
import java.io.IOException;
28
import java.net.URL;
29
import java.nio.file.Files;
30
import java.nio.file.Path;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.HashMap;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Objects;
38
import org.apache.commons.lang3.StringUtils;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import org.gvsig.fmap.dal.DataServerExplorer;
42
import org.gvsig.fmap.dal.DataStore;
43
import org.gvsig.fmap.dal.DataStoreParameters;
44
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
45
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYID;
46
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION;
47
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY;
48
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.feature.FeatureType;
51
import org.gvsig.fmap.dal.store.h2.H2SpatialUtils;
52
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
53
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
54
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
55
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
56
import org.gvsig.online.lib.api.OnlineCodeGenerator;
57
import org.gvsig.online.lib.api.OnlineManager;
58
import org.gvsig.online.lib.api.OnlineProject;
59
import org.gvsig.online.lib.api.OnlineSite;
60
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester;
61
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy;
62
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopyDescriptor;
63
import org.gvsig.online.lib.api.workingcopy.WorkingArea;
64
import org.gvsig.online.lib.impl.workspace.OnlineWorkingcopyDescriptorImpl;
65
import org.gvsig.online.lib.impl.workspace.OnlineWorkspaceImpl;
66
import org.gvsig.online.lib.impl.workspace.StoreProperties;
67
import org.gvsig.online.lib.impl.workspace.WorkingAreaImpl;
68
import org.gvsig.online.lib.impl.workspace.tables.EntitiesTable;
69
import org.gvsig.online.lib.impl.workspace.tables.RemoteChangesTable;
70
import org.gvsig.online.lib.impl.workspace.tables.VarsTable;
71
import org.gvsig.online.lib.impl.workspace.tables.WorkspaceChangesTable;
72
import org.gvsig.tools.ToolsLocator;
73
import org.gvsig.tools.dispose.DisposeUtils;
74
import org.gvsig.tools.task.SimpleTaskStatus;
75
import org.gvsig.tools.task.UserCancelTaskException;
76
import org.gvsig.tools.util.HasAFile;
77
import org.gvsig.tools.util.ListBuilder;
78
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
80

    
81
@SuppressWarnings("UseSpecificCatch")
82
public class OnlineManagerImpl implements OnlineManager {
83
    
84
    private static final Logger LOGGER = LoggerFactory.getLogger(OnlineManagerImpl.class);
85

    
86
    public static final List<String> INTERNAL_WORKSPACE_TABLES = ListBuilder.create(
87
            EntitiesTable.TABLE_NAME,
88
            VarsTable.TABLE_NAME,
89
            RemoteChangesTable.TABLE_NAME,
90
            WorkspaceChangesTable.TABLE_NAME,
91
            DatabaseWorkspaceManager.TABLE_RESOURCES_NAME,
92
            DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME,
93
            DatabaseWorkspaceManager.TABLE_REPOSITORY_NAME
94
    );
95
    
96
    private OnlineUserIdentificationRequester userIdentificationRequester;
97
    private Map<String,OnlineWorkingcopyDescriptor> workspaces;
98

    
99
    public OnlineManagerImpl() {
100
        this.workspaces = new HashMap<>();
101
        this.userIdentificationRequester = null;
102
    }
103
    
104
    @Override
105
    public String getErrorMessage(int errcode) {
106
        return OnlineUtils.getErrorMessage(errcode);
107
    }
108

    
109
    @Override
110
    public void setUserIdentificationRequester(OnlineUserIdentificationRequester userIdentificationRequester) {
111
        this.userIdentificationRequester = userIdentificationRequester;
112
    }
113

    
114
    @Override
115
    public OnlineUserIdentificationRequester getUserIdentificationRequester() {
116
        return this.userIdentificationRequester;
117
    }
118

    
119
    @Override
120
    public OnlineWorkingcopyDescriptor getWorkingcopyDescriptor(String code) {
121
        OnlineWorkingcopyDescriptor descriptor = this.workspaces.get(code);
122
        if( descriptor!=null ) {
123
            DisposeUtils.bind(descriptor);
124
            return descriptor;
125
        }
126
        for (OnlineWorkingcopyDescriptor descriptor2 : this.workspaces.values()) {
127
            if( descriptor2==null ) {
128
                continue;
129
            }
130
            if( StringUtils.equalsIgnoreCase(descriptor2.getLabel(), code) ) {
131
                DisposeUtils.bind(descriptor2);
132
                return descriptor2;
133
            }
134
        }
135
        return null;
136
    }
137
    
138
    @Override
139
    public synchronized void clean() {
140
        for (OnlineWorkingcopyDescriptor workspace : workspaces.values()) {
141
            DisposeUtils.disposeQuietly(workspace);
142
        }
143
        this.workspaces = new HashMap<>();
144
        this.userIdentificationRequester = null;
145
    }
146
    
147
    public OnlineWorkingcopy openWorkingcopy(JDBCServerExplorer wsexplorer, String label) {
148
        // Abre la copia de trabajo cambiando su label y asignando un code nuevo.
149
        if (wsexplorer == null) {
150
            return null;
151
        }
152
        JDBCServerExplorerParameters params = null;
153
        try {
154
            OnlineWorkingcopy workspace;
155
            params = wsexplorer.getParameters();
156
            
157
            workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), label);
158
            workspace.setUserIdentificationRequester(this.userIdentificationRequester);
159
            this.registerWorkingcopy(workspace);
160
            dropExpiredCaches();
161
            return workspace;
162
        } catch (Exception ex) {
163
            LOGGER.debug("Can't get workspace from "+Objects.toString(params)+".", ex);
164
            return null;
165
        }
166
    }
167
    
168
    @Override
169
    public void registerWorkingcopy(OnlineWorkingcopy workspace) {
170
        OnlineWorkingcopyDescriptor descriptor = this.workspaces.get(workspace.getCode());
171
        if( descriptor == null ) {
172
            descriptor = new OnlineWorkingcopyDescriptorImpl(workspace);
173
            this.workspaces.put(workspace.getCode(), descriptor);
174
        } else {
175
            ((OnlineWorkingcopyDescriptorImpl)descriptor).setWorkspace(workspace);            
176
        }
177
    }
178

    
179
    @Override
180
    public void deregisterWorkingcopy(File dbfile) {
181
        Path dbpath = dbfile.toPath();
182
        List<String>wscodes = new ArrayList<>();
183
        for (Iterator<OnlineWorkingcopyDescriptor> iterator = this.workspaces.values().iterator(); iterator.hasNext();) {
184
            OnlineWorkingcopyDescriptorImpl descriptor = (OnlineWorkingcopyDescriptorImpl) iterator.next();
185
            JDBCServerExplorerParameters explorerParams = descriptor.getExplorerParameters();
186
            if( explorerParams instanceof HasAFile ) {
187
                Path p = ((HasAFile) explorerParams).getFile().toPath();
188
                try {
189
                    if( Files.isSameFile(dbpath, p) ) {
190
                        wscodes.add(descriptor.getCode());
191
                    }
192
                } catch (IOException ex) {
193
                    LOGGER.debug("Can't deregister workspace '"+dbfile.getAbsolutePath()+"'.",ex);
194
                }
195
            }
196
        }        
197
        for (String wscode : wscodes) {
198
            deregisterWorkingcopy(wscode);
199
        }
200
    }
201

    
202
    @Override
203
    public void deregisterWorkingcopy(OnlineWorkingcopy workspace) {
204
        this.deregisterWorkingcopy(workspace.getCode());
205
    }
206

    
207
    @Override
208
    public void deregisterWorkingcopy(String workspaceCode) {
209
        DisposeUtils.disposeQuietly(this.workspaces.get(workspaceCode));
210
        this.workspaces.remove(workspaceCode);
211
        dropExpiredCaches();
212
    }
213
    
214
    private void dropExpiredCaches() {
215
        for (Iterator<OnlineWorkingcopyDescriptor> iterator = this.workspaces.values().iterator(); iterator.hasNext();) {
216
            OnlineWorkingcopyDescriptorImpl value = (OnlineWorkingcopyDescriptorImpl) iterator.next();
217
            if(DisposeUtils.getReferences(value)<=0){
218
                iterator.remove();
219
            } else {
220
                value.dropExpiredCaches();
221
            }
222
        }
223
    }
224

    
225
    @Override
226
    public OnlineWorkingcopy getWorkingcopy(FeatureStore store) {
227
        try {
228
            OnlineWorkspaceImpl workspace;
229
            switch(StoreProperties.getOnlineMode(store)) {
230
                case StoreProperties.MODE_NOT_ONLINE:
231
                    return null;
232

    
233
                case StoreProperties.MODE_ONLINE:
234
                    String workspace_code = StoreProperties.getWorkspaceCode(store);
235
                    OnlineWorkingcopyDescriptor workspaceDescriptor = this.workspaces.get(workspace_code);
236
                    if( workspaceDescriptor!=null ) {
237
                        workspace = (OnlineWorkspaceImpl) workspaceDescriptor.getWorkspace();
238
                        if (workspace != null) {
239
                            if( workspace.isInStoreIgnoreChanges(store) ) {
240
                                return workspace;
241
                            }
242
                            String entity_name = StoreProperties.getEntityName(store);
243
                            EntitiesTable.EntityRow entity = (EntitiesTable.EntityRow) workspace.getWorkspaceEntityByName(entity_name);
244
                            if (entity == null) {
245
                                StoreProperties.setNotOnlineMode(store);
246
                                DisposeUtils.dispose(workspace);
247
                                return null;
248
                            }
249
                            if( workspace.isCorrupt(entity,store,0) ) {
250
//                                return null; 
251
                            }
252
//                            LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from property (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");        
253
                            return workspace;
254
                        }
255
                    }                
256
                case StoreProperties.MODE_UNKNOWN:
257
                default:
258
                    DataStoreParameters params = store.getParameters();
259
                    if(!(params instanceof JDBCStoreParameters)) {
260
                        StoreProperties.setNotOnlineMode(store);
261
                        return null;
262
                    }
263
                    String entity_name = ((JDBCStoreParameters) params).getTable();
264

    
265
                    if (INTERNAL_WORKSPACE_TABLES.contains(entity_name)  ) {
266
                        StoreProperties.setNotOnlineMode(store);
267
                        return null;
268
                    }
269

    
270
                    String storeUrl = ((JDBCStoreParameters)params).getUrl();
271
                    List<OnlineWorkingcopyDescriptor> descriptors = new ArrayList<>(this.workspaces.values());
272
                    for (OnlineWorkingcopyDescriptor value : descriptors) {
273
                        if( value == null ) {
274
                            continue;
275
                        }
276
                        JDBCServerExplorerParameters explorerParams = value.getExplorerParameters();    
277
                        if( explorerParams == null ) {
278
                            continue;
279
                        }
280
                        if (!StringUtils.equals(storeUrl, explorerParams.getUrl())) {
281
                            continue;
282
                        }
283
                        workspace = (OnlineWorkspaceImpl) value.getWorkspace();
284
                        if( workspace == null ) {
285
                            continue;
286
                        }
287
                        EntitiesTable.EntityRow entity = (EntitiesTable.EntityRow) workspace.getWorkspaceEntityByName(entity_name);
288
                        if (entity == null) {
289
                            StoreProperties.setNotOnlineMode(store);
290
                            DisposeUtils.dispose(workspace);
291
                            return null;
292
                        }
293
                        StoreProperties.set(store, workspace, entity_name);
294
                        if( workspace.isCorrupt(entity,store,0) ) {
295
//                                return null;
296
                        }
297
//                            LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");        
298
                        return workspace;
299
                    }
300

    
301
                    JDBCServerExplorer explorer = null;
302
                    try {
303
                        explorer = (JDBCServerExplorer)store.getExplorer();
304
                        if( !this.isWorkingcopy(explorer.getParameters()) ) {
305
                            StoreProperties.setNotOnlineMode(store);
306
                            return null;
307
                        }
308
                        workspace = (OnlineWorkspaceImpl) openWorkingcopy(explorer);
309
                    } catch (Exception e) {
310
                        workspace = null;
311
                    } finally {
312
                        DisposeUtils.disposeQuietly(explorer);
313
                    }
314
                    if (workspace == null) {
315
                        StoreProperties.setNotOnlineMode(store);
316
                        return null;
317
                    }
318
                    this.registerWorkingcopy(workspace);
319

    
320
                    EntitiesTable.EntityRow entity = (EntitiesTable.EntityRow) workspace.getWorkspaceEntityByName(entity_name);
321
                    if (entity == null) {
322
                        StoreProperties.setNotOnlineMode(store);
323
                        return null;
324
                    }
325
                    StoreProperties.set(store, workspace, entity_name);
326
                    return workspace;
327
            }
328
        } catch (Exception ex) {
329
            LOGGER.warn("Can't get '" +store.getName()+ "'store's workspace ", ex);
330
            StoreProperties.setUnknownOnlineMode(store);
331
            throw new RuntimeException("Can't get '" +store.getName()+ "'store's workspace ", ex);
332
        }
333
    }
334
    
335
    @Override
336
    public Map<String, OnlineWorkingcopyDescriptor> getWorkingcopy() {
337
        return Collections.unmodifiableMap(this.workspaces);
338
    }
339

    
340
    @Override
341
    public synchronized void restoreWorkingcopy(Map<String, OnlineWorkingcopyDescriptor> descriptors) {
342
        this.workspaces = new HashMap<>();
343
        if( descriptors != null ) {
344
            this.workspaces.putAll(descriptors);
345
        }
346
    }
347
    
348
    @Override
349
    public boolean isWorkingcopy(JDBCServerExplorerParameters params) {
350
        DataServerExplorer explorer = null;
351
        try {
352
            DataManager dataManager = DALLocator.getDataManager();
353
            explorer = dataManager.openServerExplorer(params.getProviderName(), params);
354
            DataStoreParameters x = explorer.get(VarsTable.TABLE_NAME);
355
            if(x==null) {
356
                return false;
357
            }
358
            return explorer.exists(x);
359
        } catch (Exception ex) {
360
            return false;
361
        } finally {
362
            DisposeUtils.disposeQuietly(explorer);
363
        }
364
    }
365
    
366
    @Override
367
    public boolean isWorkingcopy(File dbfile) {
368
        try {
369
            DataManager dataManager = DALLocator.getDataManager();
370
            JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME);
371
            ((HasAFile)params).setFile(dbfile);
372
            return isWorkingcopy(params);
373
        } catch (Exception ex) {
374
            return false;
375
        }
376
    }    
377
    
378
    
379
    @Override
380
    public int initWorkingcopy(File wsfile, OnlineProject project, String label, SimpleTaskStatus status) {
381
        int err = ERR_NO_ERROR;
382
        if (wsfile == null) {
383
            return ERR_DBFILE_IS_NULL;
384
        }
385
        if (status == null) {
386
            status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Online init_workingcopy");
387
            status.setAutoremove(true);
388
            status.add();
389
        } else {
390
            status.push();
391
        }
392
        DataManager dataManager = DALLocator.getDataManager();
393
        JDBCServerExplorer explorer = null;
394
        try {
395
            wsfile = new File(H2SpatialUtils.removeH2FileNameExtension(wsfile.getAbsolutePath()));
396
            if (StringUtils.isBlank(label)) {
397
                label = wsfile.getName();
398
            }
399
            err = ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER;
400
            JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME);
401
            ((HasAFile) explorerParams).setFile(wsfile);
402
            explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
403
                    explorerParams.getProviderName(),
404
                    explorerParams
405
            );
406
            return this.initWorkingcopy(explorer, project, label, status);
407
        } catch (Exception ex) {
408
            status.abort();
409
            LOGGER.warn("Can't init workspace in '" + wsfile.getAbsolutePath() + "'.", ex);
410
            status.message(ex.getMessage());
411
            return err;
412
        } finally {
413
            status.pop();
414
            DisposeUtils.disposeQuietly(explorer);
415
        }
416
    }
417

    
418
    @Override
419
    public int initWorkingcopy(JDBCServerExplorer wsexplorer, OnlineProject project, String label, SimpleTaskStatus status) {
420
        int err = ERR_NO_ERROR;
421
        if (wsexplorer == null) {
422
            return ERR_WSEXPLORER_IS_NULL;
423
        }
424
        if (StringUtils.isBlank(label)) {
425
            return ERR_LABEL_IS_NULL;
426
        }
427
        if (status == null) {
428
            status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Online init_workingcopy");
429
            status.setAutoremove(true);
430
            status.add();
431
        } else {
432
            status.push();
433
        }
434
        LOGGER.info("Initializing workspace "+Objects.toString(wsexplorer.getParameters().toJson()).replace('\n', ' ').replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"****\""));
435

    
436
        DataManager dataManager = DALLocator.getDataManager();
437
        OnlineWorkspaceImpl workspace = null;
438
        try {
439
            status.setTitle("Online init_workingcopy");
440
            
441
            DatabaseWorkspaceManager databaseWorkspaceManager = dataManager.createDatabaseWorkspaceManager(wsexplorer.getParameters());
442
            
443
            if(!databaseWorkspaceManager.existsTable(TABLE_CONFIGURATION)){
444
                databaseWorkspaceManager.createTable(TABLE_CONFIGURATION);
445
            }
446
            if(!databaseWorkspaceManager.existsTable(TABLE_RESOURCES)){
447
                databaseWorkspaceManager.createTable(TABLE_RESOURCES);
448
            }
449
            if(!databaseWorkspaceManager.existsTable(TABLE_REPOSITORY)){
450
                databaseWorkspaceManager.createTable(TABLE_REPOSITORY);
451
                databaseWorkspaceManager.set(CONFIG_NAME_STORESREPOSITORYID, "Online");
452
            }
453
            databaseWorkspaceManager.connect();
454

    
455
            
456
            FeatureType[] tables = new FeatureType[]{
457
                VarsTable.featureType(),
458
                EntitiesTable.featureType(),
459
                WorkspaceChangesTable.featureType(),
460
                RemoteChangesTable.featureType()
461
            };
462
            status.setRangeOfValues(0, tables.length);
463
            int step = 1;
464
            for (FeatureType table : tables) {
465
                status.message(table.getLabel());
466
                status.setCurValue(step++);
467
                err = ERR_CANT_CREATE_TABLE + step;
468
                JDBCNewStoreParameters table_params = wsexplorer.getAddParameters();
469
                table_params.setDefaultFeatureType(table.getEditable());
470
                String tableName = table.getTags().getString("ID");
471
                table_params.setTable(tableName);
472
                wsexplorer.add(wsexplorer.getProviderName(), table_params, false);
473
                
474
                JDBCStoreParameters openParams = wsexplorer.get(tableName);
475
                databaseWorkspaceManager.writeStoresRepositoryEntry(tableName, openParams);
476
                
477
            }
478
//            try {
479
//                wsexplorer.execute(OnlineUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex1"));
480
//                wsexplorer.execute(OnlineUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex2"));
481
//                wsexplorer.execute(OnlineUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex3"));
482
//                
483
//            } catch (Exception ex) {
484
//                LOGGER.warn("Can't create index in workspace", ex);
485
//            }
486
//            
487
            status.setTitle("vcsgis retrieving entities");
488
            workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), project, label);
489
            if (workspace.getUserIdentificationRequester() == null) {
490
                workspace.setUserIdentificationRequester(this.userIdentificationRequester);
491
            }
492
            workspace.initialize();
493
            LOGGER.info("Workspace initialized "+workspace.getCode());
494

    
495
            this.registerWorkingcopy(workspace);
496
            status.terminate();
497
            return ERR_NO_ERROR;
498
        } catch (UserCancelTaskException ex) {
499
            status.cancel();
500
            LOGGER.warn("Cancelled by user (" + getMessageLabel(wsexplorer) + ").", ex);
501
            status.message("Cancelled by user.");
502
            throw ex;
503
        } catch (Exception ex) {
504
            status.abort();
505
            LOGGER.warn("Can't init workspace in '" + getMessageLabel(wsexplorer) + "'.", ex);
506
            status.message(ex.getMessage());
507
            return err;
508
        } finally {
509
            status.pop();
510
            DisposeUtils.disposeQuietly(workspace);
511
        }
512
    }
513
    
514
    private String getMessageLabel(JDBCServerExplorer explorer) {
515
        return explorer.getParameters().getUrl();
516
    }
517

    
518
    @Override
519
    public OnlineWorkingcopy openWorkingcopy(File wsfile) {
520
        if (wsfile == null) {
521
            return null;
522
        }
523
        JDBCServerExplorer explorer = null;
524
        try {
525
            DataManager dataManager = DALLocator.getDataManager();
526
            JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME);
527
            ((HasAFile) explorerParams).setFile(wsfile);
528
            explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
529
                    explorerParams.getProviderName(),
530
                    explorerParams
531
            );
532
            OnlineWorkingcopy workspace = openWorkingcopy(explorer);
533
            return workspace;
534
        } catch (Exception ex) {
535
            return null;
536
        } finally {
537
            DisposeUtils.disposeQuietly(explorer);
538
        }
539
    }
540

    
541
    @Override
542
    public OnlineWorkingcopy openWorkingcopy(JDBCServerExplorer wsexplorer) {
543
        if (wsexplorer == null) {
544
            return null;
545
        }
546
        JDBCServerExplorerParameters params = null;
547
        try {
548
            OnlineWorkingcopy workspace;
549

    
550
            params = wsexplorer.getParameters();
551
            String url = params.getUrl();
552
            for (Iterator<OnlineWorkingcopyDescriptor> iterator = this.workspaces.values().iterator(); iterator.hasNext();) {
553
                OnlineWorkingcopyDescriptorImpl value = (OnlineWorkingcopyDescriptorImpl) iterator.next();
554
                if (DisposeUtils.getReferences(value)<=0) {
555
                    iterator.remove();
556
                    continue;
557
                }
558
                if (value.isWorkspaceInitialized() && StringUtils.equals(url, value.getExplorerParameters().getUrl())) {
559
                    workspace = value.getWorkspace();//FIXME: lo siguiente solo si es null
560
                    //workspace.getCode(); value.getCode();
561
                    if (workspace.getUserIdentificationRequester() == null) {
562
                        workspace.setUserIdentificationRequester(this.userIdentificationRequester);
563
                    }
564
                    dropExpiredCaches();
565
//                    LOGGER.debug("===: OPEN WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')");        
566
                    return workspace;
567
                }
568
            }
569

    
570
            workspace = new OnlineWorkspaceImpl(wsexplorer, this.getCodeGenerator(), null);
571
            workspace.setUserIdentificationRequester(this.userIdentificationRequester);
572
            this.registerWorkingcopy(workspace);
573
            dropExpiredCaches();
574
            return workspace;
575
        } catch (Exception ex) {
576
            LOGGER.debug("Can't get workspace from "+Objects.toString(params)+".", ex);
577
            return null;
578
        }
579
    }
580

    
581
    public OnlineWorkingcopy openWorkingcopy(File wsfile, String label) {
582
        if (wsfile == null) {
583
            return null;
584
        }
585
        JDBCServerExplorer explorer = null;
586
        try {
587
            DataManager dataManager = DALLocator.getDataManager();
588
            JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME);
589
            ((HasAFile) explorerParams).setFile(wsfile);
590
            explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
591
                    explorerParams.getProviderName(),
592
                    explorerParams
593
            );
594
            OnlineWorkingcopy workspace = openWorkingcopy(explorer, label);
595
            return workspace;
596
        } catch (Exception ex) {
597
            return null;
598
        } finally {
599
            DisposeUtils.disposeQuietly(explorer);
600
        }
601
    }
602
    
603
    @Override
604
    public OnlineSite connectSite(URL siteurl) throws IOException {
605
        OnlineSiteImpl site = new OnlineSiteImpl();
606
        if( siteurl!=null) {
607
            site.connect(siteurl);
608
        }
609
        return site;
610
    }
611

    
612
    @Override
613
    public WorkingArea createWorkingArea() {
614
        return new WorkingAreaImpl();
615
    }
616
    
617
    private OnlineCodeGenerator getCodeGenerator() {
618
        return new RandomCodeGenerator();
619
    }
620

    
621

    
622
}