Revision 44190 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialResource.java

View differences:

H2SpatialResource.java
14 14
import java.nio.file.FileSystems;
15 15
import java.nio.file.Files;
16 16
import java.nio.file.Path;
17
import java.nio.file.StandardCopyOption;
17 18
import java.util.Collections;
18 19
import java.util.Objects;
19
import java.util.logging.Level;
20
import java.util.logging.Logger;
21 20
import org.apache.commons.io.IOUtils;
22 21
import org.gvsig.fmap.dal.AbstractDataResource;
23 22
import org.gvsig.fmap.dal.DataServerExplorer.DataResource;
23
import org.slf4j.LoggerFactory;
24 24

  
25 25
/**
26 26
 *
......
32 32
        implements DataResource 
33 33
    {
34 34
    
35
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(H2SpatialResource.class);
36
    
35 37
    private final File zipFile;
36 38
    private final String tableName;
37 39
    private InputStream in;
38 40
    private ByteArrayOutputStream out;
39 41
    private FileSystem zipfs;
40 42
    private final URI zipfsuri;
43
    private final String resourceName;
41 44

  
42
    public H2SpatialResource(File zipFile, String tableName) {
45
    public H2SpatialResource(File zipFile, String tableName, String resourceName) {
43 46
        this.zipFile = zipFile;
44 47
        this.tableName = tableName;
48
        this.resourceName = resourceName;
45 49
        try {
46 50
            this.zipfsuri = new URI("jar:" + this.zipFile.toURI().toString());
47 51
        } catch (URISyntaxException ex) {
......
49 53
        }
50 54
    }
51 55

  
52
    @Override
53 56
    public URL getURL() {
54 57
        try {
55
            return new URL(this.zipfsuri.toString()+"!"+this.tableName);
58
            return new URL(this.zipfsuri.toString()+"!/"+this.tableName+"."+this.resourceName);
56 59
        } catch (MalformedURLException ex) {
57 60
            return null;
58 61
        }
......
71 74
        }
72 75
        try {
73 76
            this.openzipfs(false);
74
            Path tablePath = this.zipfs.getPath(this.tableName);
77
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
75 78
            if (tablePath == null) {
76 79
                return false;
77 80
            }
78
            return true;
81
            if( Files.exists(tablePath) ) {
82
                return true;
83
            }
84
            return false;
79 85
        } catch (Exception ex) {
86
            LOGGER.warn("Can't access to the resource ("+Objects.toString(this.getURL())+").", ex);
80 87
            return false;
81 88
        }
82 89
    }
......
91 98
        }
92 99
        try {
93 100
            this.openzipfs(false);
94
            Path tablePath = this.zipfs.getPath(this.tableName);
101
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
95 102
            if (tablePath == null) {
96 103
                return null;
97 104
            }
98 105
            this.in = Files.newInputStream(tablePath);
99 106
            return this.in;
100 107
        } catch (Exception ex) {
108
            LOGGER.warn("Can't create input stream ("+Objects.toString(this.getURL())+").", ex);
101 109
            return null;
102 110
        }
103 111
    }
......
105 113
    @Override
106 114
    public OutputStream asOutputStream() throws IOException {
107 115
        if (this.in != null || this.out != null) {
108
            throw new IllegalStateException("Resource is already open (" + this.zipFile.toString() + "!" + this.tableName + ")");
116
            throw new IllegalStateException("Resource is already open ("+Objects.toString(this.getURL())+").");
109 117
        }
110 118
        this.out = new ByteArrayOutputStream();
111 119
        return this.out;
......
121 129
            ByteArrayInputStream pipe = null;
122 130
            try {
123 131
                this.openzipfs(true);
124
                Path tablePath = this.zipfs.getPath(this.tableName);
132
                Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
125 133
                pipe = new ByteArrayInputStream(this.out.toByteArray());
126
                Files.copy(pipe, tablePath);
134
                Files.copy(pipe, tablePath, StandardCopyOption.REPLACE_EXISTING);
127 135
            } catch (Exception ex) {
136
                LOGGER.warn("Can't write resource ("+Objects.toString(this.getURL())+").", ex);
128 137
            } finally {
129 138
                IOUtils.closeQuietly(pipe);
130 139
                IOUtils.closeQuietly(this.out);
......
133 142
        }
134 143
        if (this.zipfs != null) {
135 144
            IOUtils.closeQuietly(this.zipfs);
145
            this.zipfs = null;
136 146
        }
137 147
    }
138 148
    

Also available in: Unified diff