Statistics
| Revision:

svn-gvsig-desktop / 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 @ 44190

History | View | Annotate | Download (4.82 KB)

1
package org.gvsig.fmap.dal.store.h2;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.File;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.net.MalformedURLException;
10
import java.net.URI;
11
import java.net.URISyntaxException;
12
import java.net.URL;
13
import java.nio.file.FileSystem;
14
import java.nio.file.FileSystems;
15
import java.nio.file.Files;
16
import java.nio.file.Path;
17
import java.nio.file.StandardCopyOption;
18
import java.util.Collections;
19
import java.util.Objects;
20
import org.apache.commons.io.IOUtils;
21
import org.gvsig.fmap.dal.AbstractDataResource;
22
import org.gvsig.fmap.dal.DataServerExplorer.DataResource;
23
import org.slf4j.LoggerFactory;
24

    
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29
@SuppressWarnings("UseSpecificCatch")
30
class H2SpatialResource 
31
        extends AbstractDataResource
32
        implements DataResource 
33
    {
34
    
35
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(H2SpatialResource.class);
36
    
37
    private final File zipFile;
38
    private final String tableName;
39
    private InputStream in;
40
    private ByteArrayOutputStream out;
41
    private FileSystem zipfs;
42
    private final URI zipfsuri;
43
    private final String resourceName;
44

    
45
    public H2SpatialResource(File zipFile, String tableName, String resourceName) {
46
        this.zipFile = zipFile;
47
        this.tableName = tableName;
48
        this.resourceName = resourceName;
49
        try {
50
            this.zipfsuri = new URI("jar:" + this.zipFile.toURI().toString());
51
        } catch (URISyntaxException ex) {
52
            throw new IllegalArgumentException("The zip file '" + Objects.toString(zipFile) + "' is not a valid file name.", ex);
53
        }
54
    }
55

    
56
    public URL getURL() {
57
        try {
58
            return new URL(this.zipfsuri.toString()+"!/"+this.tableName+"."+this.resourceName);
59
        } catch (MalformedURLException ex) {
60
            return null;
61
        }
62
    }
63

    
64
    private void openzipfs(boolean create) throws IOException {
65
        if (this.zipfs == null) {
66
            this.zipfs = FileSystems.newFileSystem(this.zipfsuri, Collections.singletonMap("create", create ? "true" : "false"));
67
        }
68
    }
69

    
70
    @Override
71
    public boolean exists() {
72
        if (!this.zipFile.exists()) {
73
            return false;
74
        }
75
        try {
76
            this.openzipfs(false);
77
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
78
            if (tablePath == null) {
79
                return false;
80
            }
81
            if( Files.exists(tablePath) ) {
82
                return true;
83
            }
84
            return false;
85
        } catch (Exception ex) {
86
            LOGGER.warn("Can't access to the resource ("+Objects.toString(this.getURL())+").", ex);
87
            return false;
88
        }
89
    }
90

    
91
    @Override
92
    public InputStream asInputStream() throws IOException {
93
        if (!this.zipFile.exists()) {
94
            return null;
95
        }
96
        if (this.in != null || this.out != null) {
97
            throw new IllegalStateException("Resource is already open (" + this.zipFile.toString() + "!" + this.tableName + ")");
98
        }
99
        try {
100
            this.openzipfs(false);
101
            Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
102
            if (tablePath == null) {
103
                return null;
104
            }
105
            this.in = Files.newInputStream(tablePath);
106
            return this.in;
107
        } catch (Exception ex) {
108
            LOGGER.warn("Can't create input stream ("+Objects.toString(this.getURL())+").", ex);
109
            return null;
110
        }
111
    }
112

    
113
    @Override
114
    public OutputStream asOutputStream() throws IOException {
115
        if (this.in != null || this.out != null) {
116
            throw new IllegalStateException("Resource is already open ("+Objects.toString(this.getURL())+").");
117
        }
118
        this.out = new ByteArrayOutputStream();
119
        return this.out;
120
    }
121

    
122
    @Override
123
    public void close() {
124
        if (this.in != null) {
125
            IOUtils.closeQuietly(this.in);
126
            this.in = null;
127
        }
128
        if (this.out != null) {
129
            ByteArrayInputStream pipe = null;
130
            try {
131
                this.openzipfs(true);
132
                Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName);
133
                pipe = new ByteArrayInputStream(this.out.toByteArray());
134
                Files.copy(pipe, tablePath, StandardCopyOption.REPLACE_EXISTING);
135
            } catch (Exception ex) {
136
                LOGGER.warn("Can't write resource ("+Objects.toString(this.getURL())+").", ex);
137
            } finally {
138
                IOUtils.closeQuietly(pipe);
139
                IOUtils.closeQuietly(this.out);
140
                this.out = null;
141
            }
142
        }
143
        if (this.zipfs != null) {
144
            IOUtils.closeQuietly(this.zipfs);
145
            this.zipfs = null;
146
        }
147
    }
148
    
149
}