Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / serverexplorer / filesystem / impl / DefaultFilesystemServerExplorer.java @ 44259

History | View | Annotate | Download (21 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.serverexplorer.filesystem.impl;
24

    
25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.FileOutputStream;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.net.MalformedURLException;
31
import java.net.URL;
32
import java.util.ArrayList;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Set;
37
import java.util.logging.Level;
38
import java.util.logging.Logger;
39
import org.apache.commons.io.FilenameUtils;
40
import org.apache.commons.io.IOUtils;
41
import org.gvsig.fmap.dal.AbstractDataResource;
42

    
43
import org.gvsig.fmap.dal.DALFileLocator;
44
import org.gvsig.fmap.dal.DALLocator;
45
import org.gvsig.fmap.dal.DataManager;
46
import org.gvsig.fmap.dal.DataStore;
47
import org.gvsig.fmap.dal.DataStoreParameters;
48
import org.gvsig.fmap.dal.NewDataStoreParameters;
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.exception.FileNotFoundException;
51
import org.gvsig.fmap.dal.exception.InitializeException;
52
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
53
import org.gvsig.fmap.dal.exception.RemoveException;
54
import org.gvsig.fmap.dal.exception.ServerExplorerAddException;
55
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
56
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter;
57
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
58
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
59
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
60
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
61
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
62
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
63
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer;
64
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
65
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
66
import org.gvsig.fmap.dal.spi.FileMultiResource;
67
import org.gvsig.tools.exception.BaseException;
68
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
69

    
70
@SuppressWarnings("UseSpecificCatch")
71
public class DefaultFilesystemServerExplorer extends AbstractDataServerExplorer
72
        implements FilesystemServerExplorerProviderServices,
73
        FilesystemFileFilter {
74

    
75
    private class FileResource 
76
            extends AbstractDataResource
77
            implements DataResource 
78
        {
79

    
80
        private final File f;
81
        private FileInputStream in;
82
        private FileOutputStream out;
83
        
84
        public FileResource(File f) {
85
            this.f = f;
86
        }
87

    
88
        public URL getURL() {
89
            try {
90
                return this.f.toURI().toURL();
91
            } catch (MalformedURLException ex) {
92
                return null;
93
            }
94
        }
95
        
96
        public File getFile() {
97
            return this.f;
98
        }
99
        
100
        @Override
101
        public boolean exists() {
102
            return this.f.exists();
103
        }
104

    
105
        @Override
106
        public InputStream asInputStream() throws java.io.FileNotFoundException {
107
            if( this.out != null || this.in!=null ) {
108
                throw new IllegalStateException("Input already open");
109
            }
110
            this.in = new FileInputStream(this.f);
111
            return this.in;
112
        }
113

    
114
        @Override
115
        public OutputStream asOutputStream() throws java.io.FileNotFoundException {
116
            if( this.out != null || this.in!=null ) {
117
                throw new IllegalStateException("Input already open");
118
            }
119
            this.out = new FileOutputStream(this.f);
120
            return this.out;
121
        }
122

    
123
        @Override
124
        public void close() {
125
            IOUtils.closeQuietly(out);
126
            IOUtils.closeQuietly(in);
127
            this.in = null;
128
            this.out = null;
129
        }
130
    }
131

    
132
    private File root;
133
    private File current; // Current path
134
    private List<FilesystemServerExplorerProvider> serverProviders;
135

    
136
    public DefaultFilesystemServerExplorer(
137
            FilesystemServerExplorerParameters parameters,
138
            DataServerExplorerProviderServices services)
139
            throws InitializeException {
140
        super(parameters, services);
141
        if (this.getFSParameters().getRoot() != null) {
142
            this.root = new File(this.getFSParameters().getRoot());
143
        }
144
        if (this.getFSParameters().getInitialpath() != null) {
145
            this.current = new File(this.getFSParameters().getInitialpath());
146
        }
147
        if (this.root == null && this.current == null) {
148
                        // throw new InitializeException(this.getName(),
149
            // new IllegalArgumentException());
150
        } else if (this.current == null) {
151
            this.current = new File(this.getFSParameters().getRoot());
152
        }
153
    }
154

    
155
    private FilesystemServerExplorerParameters getFSParameters() {
156
        return (FilesystemServerExplorerParameters) this.getParameters();
157
    }
158

    
159
    @Override
160
    protected void doDispose() throws BaseException {
161
        this.root = null;
162
        super.doDispose();
163
    }
164

    
165
    @Override
166
    public List list(int mode) throws DataException {
167
        if (this.current == null) {
168
            throw new IllegalStateException();
169
        }
170
        if (!this.current.exists()) {
171
            throw new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current);
172
        }
173

    
174
        if (!this.current.isDirectory()) {
175
            throw new IllegalArgumentException(this.getProviderName()
176
                    + ": Path not a directory '" + this.current + "'");
177
        }
178

    
179
        List files = new ArrayList(); // return files
180
        List providers = this.getProviders(mode);
181
        String allfiles[] = this.current.list();
182

    
183
        for (String f : allfiles) {
184
            File file = new File(this.root, f);
185
            Iterator providersit = providers.iterator();
186
            while (providersit.hasNext()) {
187
                FilesystemServerExplorerProvider provider = (FilesystemServerExplorerProvider) providersit
188
                        .next();
189
                if (provider.accept(file)) {
190
                    DataStoreParameters dsp = this.createStoreParameters(file);
191
                    if (dsp != null) {
192
                        files.add(dsp);
193
                    }
194
                    break;
195
                }
196
            }
197
        }
198
        return files;
199
    }
200

    
201
    public List list() throws DataException {
202
        if (this.current == null) {
203
            throw new IllegalStateException(); // FIXME
204
        }
205
        if (!this.current.exists()) {
206
            throw new FileNotFoundException(this.current);
207
        }
208

    
209
        if (!this.current.isDirectory()) {
210
            throw new IllegalArgumentException(this.getProviderName()
211
                    + ": Path not a directory '" + this.current + "'");
212
        }
213

    
214
        String files[] = this.current.list();
215
        int i;
216
        File theFile;
217
        ArrayList list = new ArrayList();
218
        DataStoreParameters dsp;
219

    
220
        for (i = 0; i < files.length; i++) {
221
            theFile = new File(this.root, files[i]);
222
            dsp = this.createStoreParameters(theFile);
223
            if (dsp != null) {
224
                list.add(dsp);
225
            }
226
        }
227
        return list;
228
    }
229

    
230
    public void setCurrentPath(File path) throws FileNotFoundException {
231
        // FIXME Comprobar si es un directorio existente
232
        if (!path.exists()) {
233
            throw new FileNotFoundException(path);
234
        }
235
        if (!path.isDirectory()) {
236
            throw new IllegalArgumentException(path.getPath()
237
                    + " is not a directory");
238
        }
239
        if (!isFromRoot(path)) {
240
            throw new IllegalArgumentException(path.getPath()
241
                    + " is not from root");
242

    
243
        }
244

    
245
        this.current = path;
246
    }
247

    
248
    public File getCurrentPath() {
249
        return this.current;
250
    }
251

    
252
    public File getRoot() {
253
        return this.root;
254
    }
255

    
256
    public void remove(DataStoreParameters dsp) throws RemoveException {
257
        String providerName = dsp.getDataStoreName();
258
        try {
259
            this.checkIsMine(dsp);
260
            FilesystemServerExplorerProvider provider = this
261
                    .getProvider(providerName);
262

    
263
            provider.remove(dsp);
264
        } catch (DataException e) {
265
            throw new RemoveException(this.getProviderName(), e);
266
        }
267
    }
268

    
269
    public boolean add(String providerName, NewDataStoreParameters ndsp,
270
            boolean overwrite) throws DataException {
271

    
272
        try {
273
            this.checkIsMine(ndsp);
274
            FilesystemServerExplorerProvider provider = this
275
                    .getProvider(providerName);
276

    
277
            ndsp.validate();
278
            provider.create(ndsp, overwrite);
279
            return true;
280
        } catch (DataException | ValidateDataParametersException e) {
281
            throw new ServerExplorerAddException(this.getProviderName(), e);
282
        }
283
    }
284

    
285
    public boolean canAdd() {
286
        return this.root.canWrite();
287
    }
288

    
289
    public String getProviderName() {
290
        return FilesystemServerExplorer.NAME;
291
    }
292

    
293
    public NewDataStoreParameters getAddParameters(String storeName)
294
            throws DataException {
295
        FilesystemServerExplorerProvider provider = this.getProvider(storeName);
296
        if (provider == null) {
297
            throw new IllegalArgumentException(
298
                    "Not registered in this explorer"); // FIXME
299
        }
300

    
301
        NewDataStoreParameters nParams = provider.getCreateParameters();
302
        // nParams.setAttribute("path", this.getCurrentPath().getPath());
303
        return nParams;
304
    }
305

    
306
    public boolean canAdd(String storeName) throws DataException {
307
        if (storeName == null) {
308
            return false;// CanAdd with genericFilter
309
        }
310
        FilesystemServerExplorerProvider provider = this.getProvider(storeName);
311
        if (provider == null) {
312
            throw new IllegalArgumentException(
313
                    "Not registered in this explorer"); // FIXME
314
        }
315

    
316
        return provider.canCreate();
317

    
318
    }
319

    
320
        // ==========================================
321
    private FilesystemServerExplorerProvider getProvider(String storeName)
322
            throws InitializeException, ProviderNotRegisteredException {
323
        Iterator providers = getProviders(FilesystemServerExplorer.MODE_ALL).iterator();
324
        FilesystemServerExplorerProvider provider;
325
        while (providers.hasNext()) {
326
            provider = (FilesystemServerExplorerProvider) providers.next();
327
            if (provider.getDataStoreProviderName().equals(storeName)) {
328
                return provider;
329
            }
330
        }
331
        return null;
332
    }
333

    
334
    private DataManagerProviderServices getManager() {
335
        return (DataManagerProviderServices) DALLocator.getDataManager();
336
    }
337

    
338
    public DataStoreParameters createStoreParameters(File file, String providerName) throws DataException {
339

    
340
        return this.getParametersFor(file, providerName, true);
341
    }
342

    
343
    public DataStoreParameters createStoreParameters(File file) throws DataException {
344

    
345
        return this.getParametersFor(file, null, true);
346
    }
347

    
348
    public DataStoreParameters getParametersFor(File file, String providerName, boolean checksExist) throws DataException {
349

    
350
        if (checksExist) {
351
            if (!file.exists()) {
352
                return null;
353
            }
354
            if (!file.isFile()) {
355
                return null;
356
            }
357
            if (!file.canRead()) {
358
                return null;
359
            }
360
            if (file.isHidden()) { // XXX ???
361
                return null;
362
            }
363
        }
364
        if (providerName == null) {
365
            providerName = this.getProviderName(file);
366
        }
367
        if (providerName != null) {
368
            DataStoreParameters params = this.getManager()
369
                    .createStoreParameters(providerName);
370
            ((FilesystemStoreParameters) params).setFile(file);
371
            return params;
372

    
373
        }
374
        return null;
375
    }
376

    
377
    public List<String> getProviderNameList(File file) {
378
        Iterator filters = getFilters();
379
        List list = new ArrayList();
380
        while (filters.hasNext()) {
381
            FilesystemFileFilter filter = (FilesystemFileFilter) filters.next();
382
            if (filter.accept(file)) {
383
                list.add(filter.getDataStoreProviderName());
384
            }
385
        }
386
        return list;
387
    }
388

    
389
    @Override
390
    public String getProviderName(File file) {
391
        Iterator filters = getFilters();
392
        while (filters.hasNext()) {
393
            FilesystemFileFilter filter = (FilesystemFileFilter) filters.next();
394
            if (filter.accept(file)) {
395
                return filter.getDataStoreProviderName();
396
            }
397
        }
398
        return null;
399
    }
400

    
401
    public List getDataStoreProviderNames() {
402
        Set names = new HashSet();
403

    
404
        Iterator filters = getFilters();
405
        while (filters.hasNext()) {
406
            FilesystemFileFilter filter = (FilesystemFileFilter) filters.next();
407
            names.add(filter.getDataStoreProviderName());
408
        }
409
        return new ArrayList(names);
410
    }
411

    
412
    private void checkIsMine(DataStoreParameters dsp)
413
            throws IllegalArgumentException, DataException {
414
        if (!(dsp instanceof FilesystemStoreParameters)) {
415
            throw new IllegalArgumentException("not instance of FilesystemStoreParameters");
416
        }
417
        Iterator filters = getFilters();
418
        File file = ((FilesystemStoreParameters) dsp).getFile();
419
        if (!this.isFromRoot(file)) {
420
            throw new IllegalArgumentException("worng explorer");
421
        }
422
        FilesystemFileFilter filter;
423
        while (filters.hasNext()) {
424
            filter = (FilesystemFileFilter) filters.next();
425
            if (dsp.getDataStoreName()
426
                    .equals(filter.getDataStoreProviderName())) {
427
                return;
428
            }
429
        }
430
        throw new IllegalArgumentException("worng explorer");
431
    }
432

    
433
    private boolean isFromRoot(File file) {
434
        if (this.root == null) {
435
            return true;
436
        }
437
        return file.getAbsolutePath().startsWith(this.root.getAbsolutePath());
438
    }
439

    
440
    public List<FilesystemServerExplorerProvider> getProviders() {
441
        if (this.serverProviders == null) {
442
            Iterator iter = DALFileLocator.getFilesystemServerExplorerManager()
443
                    .getRegisteredProviders();
444
            this.serverProviders = new ArrayList();
445
            Extension ext;
446
            FilesystemServerExplorerProvider provider;
447
            FilesystemServerExplorerProviderFactory factory;
448
            while (iter.hasNext()) {
449
                ext = (Extension) iter.next();
450
                try {
451
                    factory = (FilesystemServerExplorerProviderFactory) ext.create();
452
                } catch (Exception e) {
453
                    throw new RuntimeException(e);// FIXME !!!
454
                }
455
                try {
456
                    provider=factory.createProvider();
457
                } catch (InitializeException e) {
458
                    throw new RuntimeException(e);// FIXME !!!
459
                }
460
                provider.initialize(this);
461
                this.serverProviders.add(provider);
462
            }
463
        }
464
        return this.serverProviders;
465
    }
466

    
467
    public List getProviders(int mode) {
468
        List<FilesystemServerExplorerProvider> providers = new ArrayList();
469
        List<FilesystemServerExplorerProvider> allProviders = getProviders();
470
        for (FilesystemServerExplorerProvider provider:allProviders){
471
            if (provider.isMode(mode)) {
472
                provider.initialize(this);
473
                providers.add(provider);
474
            }
475
        }
476
        return providers;
477
    }
478

    
479
    public Iterator getFilters(int mode) {
480
        return this.getProviders(mode).iterator();
481
    }
482

    
483
    public Iterator<FilesystemServerExplorerProvider> getFilters() {
484
        return this.getProviders().iterator();
485
    }
486

    
487
    public FilesystemFileFilter getFilter(int mode, final String description) {
488

    
489
        final List filters = new ArrayList();
490
        Iterator it = this.getFilters(mode);
491
        while (it.hasNext()) {
492
            filters.add(it.next());
493
        }
494
        FilesystemFileFilter filter = new FilesystemFileFilter() {
495

    
496
            public boolean accept(File f) {
497
                if (f.isDirectory()) {
498
                    return true;
499
                }
500
                for (int i = 0; i < filters.size(); i++) {
501
                    if (((FilesystemFileFilter) filters.get(i)).accept(f)) {
502
                        return true;
503
                    }
504
                }
505
                return false;
506
            }
507

    
508
            public String getDescription() {
509
                return description;
510
            }
511

    
512
            public String getDataStoreProviderName() {
513
                return null;
514
            }
515
        };
516
        return filter;
517
    }
518

    
519
    public FilesystemFileFilter getGenericFilter() {
520
        // FIXME: Este metodo, getGenericFilter, no tengo claro que deba existir (jjdc)
521
        return this;
522
    }
523

    
524
    public String getDataStoreProviderName() {
525
        return null;
526
    }
527

    
528
    public String getDescription() {
529
        return "All supporteds";
530
    }
531

    
532
    public boolean accept(File pathname) {
533
        try {
534
            return this.createStoreParameters(pathname) != null;
535
        } catch (DataException e) {
536
            throw new RuntimeException(e); // FIXME excpetion??
537
        }
538
    }
539

    
540
    public DataStore open(File file) throws DataException,
541
            ValidateDataParametersException {
542
        DataManager manager = DALLocator.getDataManager();
543
        String providerName = this.getProviderName(file);
544

    
545
        DataStoreParameters params = this.getAddParameters(file);
546
        return manager.openStore(providerName, params);
547
    }
548

    
549
    public NewDataStoreParameters getAddParameters(File file)
550
            throws DataException {
551
        DataStoreParameters params = this.getParametersFor(file, null, false);
552
        NewDataStoreParameters newParams = this.getAddParameters(params
553
                .getDataStoreName());
554
        ((FilesystemStoreParameters) newParams)
555
                .setFile(((FilesystemStoreParameters) params).getFile());
556
        return newParams;
557
    }
558

    
559
    @Deprecated
560
    @Override
561
    public File getResourcePath(DataStore dataStore, String resourceName) throws DataException {
562
        FileResource resource = (FileResource) this.getResource(dataStore, resourceName);
563
        if( resource==null ) {
564
            return null;
565
        }
566
        return resource.getFile();
567
    }
568

    
569
    @Override
570
    public DataResource getResource(DataStore dataStore, String resourceName) throws DataException {
571
        FilesystemServerExplorerProvider provider = this.getProvider(dataStore.getProviderName());
572
        if (provider == null) {
573
            return null;
574
        }
575
        String rootPathName = provider.getResourceRootPathName(dataStore);
576
        if (rootPathName == null) {
577
            return null;
578
        }
579
        if( !dataStore.getChildren().isEmpty() ) {
580
            FileMultiResource res = new FileMultiResource(
581
                    new File(rootPathName),
582
                    dataStore.getName(), 
583
                    resourceName
584
            );
585
            return res;
586
        }
587
        File f = new File(FilenameUtils.getFullPathNoEndSeparator(rootPathName),resourceName);
588
        if( f.exists() ) {
589
            return new FileResource(f);
590
        }
591
        return new FileResource(new File(rootPathName + "." + resourceName));
592
    }
593
    
594
    @Override
595
    public DataStoreParameters get(String name) throws DataException {
596
        File theFile = new File(this.current,name);
597
        DataStoreParameters dsp = this.createStoreParameters(theFile);
598
        return dsp;
599
    }
600
}