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

View differences:

DefaultFilesystemServerExplorer.java
23 23
package org.gvsig.fmap.dal.serverexplorer.filesystem.impl;
24 24

  
25 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 26
import java.util.ArrayList;
33 27
import java.util.HashSet;
34 28
import java.util.Iterator;
35 29
import java.util.List;
36 30
import java.util.Set;
37
import java.util.logging.Level;
38
import java.util.logging.Logger;
39 31
import org.apache.commons.io.FilenameUtils;
40
import org.apache.commons.io.IOUtils;
41
import org.gvsig.fmap.dal.AbstractDataResource;
42 32

  
43 33
import org.gvsig.fmap.dal.DALFileLocator;
44 34
import org.gvsig.fmap.dal.DALLocator;
......
66 56
import org.gvsig.fmap.dal.spi.FileMultiResource;
67 57
import org.gvsig.tools.exception.BaseException;
68 58
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
59
import org.gvsig.tools.util.ResourcesStorage;
60
import org.gvsig.tools.util.SimpleFileResource;
69 61

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

  
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 67
    private File root;
133 68
    private File current; // Current path
134 69
    private List<FilesystemServerExplorerProvider> serverProviders;
......
559 494
    @Deprecated
560 495
    @Override
561 496
    public File getResourcePath(DataStore dataStore, String resourceName) throws DataException {
562
        FileResource resource = (FileResource) this.getResource(dataStore, resourceName);
497
        SimpleFileResource resource = (SimpleFileResource) this.getResource(dataStore, resourceName);
563 498
        if( resource==null ) {
564 499
            return null;
565 500
        }
......
567 502
    }
568 503

  
569 504
    @Override
570
    public DataResource getResource(DataStore dataStore, String resourceName) throws DataException {
505
    public org.gvsig.tools.util.ResourcesStorage.Resource getResource(DataStore dataStore, String resourceName) throws DataException {
571 506
        FilesystemServerExplorerProvider provider = this.getProvider(dataStore.getProviderName());
572 507
        if (provider == null) {
573 508
            return null;
......
586 521
        }
587 522
        File f = new File(FilenameUtils.getFullPathNoEndSeparator(rootPathName),resourceName);
588 523
        if( f.exists() ) {
589
            return new FileResource(f);
524
            return new SimpleFileResource(f);
590 525
        }
591
        return new FileResource(new File(rootPathName + "." + resourceName));
526
        return new SimpleFileResource(new File(rootPathName + "." + resourceName));
592 527
    }
528

  
529
    @Override
530
    public List<ResourcesStorage.Resource> getResources(DataStore dataStore, String resourceName) throws DataException {
531
        FilesystemServerExplorerProvider provider = this.getProvider(dataStore.getProviderName());
532
        if (provider == null) {
533
            return null;
534
        }
535
        String rootPathName = provider.getResourceRootPathName(dataStore);
536
        if (rootPathName == null) {
537
            return null;
538
        }
539
        List<ResourcesStorage.Resource>ress = new ArrayList<>();
540
        int n = 0;
541
        while(true) {
542
            String multiresourceName;
543
            if( n==0 ) {
544
                multiresourceName = resourceName;
545
            } else {
546
                multiresourceName = String.valueOf(n)+"."+resourceName;
547
            }
548
            File f = new File(rootPathName + "." + multiresourceName);
549
            if( !f.exists() ) {
550
                break;
551
            }
552
            ress.add(new SimpleFileResource(f));
553
            n++;
554
        }
555
        if( ress.isEmpty() ) {
556
            return null;
557
        }
558
        return ress;
559
    }
593 560
    
561
    
594 562
    @Override
595 563
    public DataStoreParameters get(String name) throws DataException {
596 564
        File theFile = new File(this.current,name);

Also available in: Unified diff