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 @ 44198
History | View | Annotate | Download (4.82 KB)
1 | 44189 | jjdelcerro | 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 | 44190 | jjdelcerro | import java.nio.file.StandardCopyOption; |
18 | 44189 | jjdelcerro | 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 | 44190 | jjdelcerro | import org.slf4j.LoggerFactory; |
24 | 44189 | jjdelcerro | |
25 | /**
|
||
26 | *
|
||
27 | * @author jjdelcerro
|
||
28 | */
|
||
29 | @SuppressWarnings("UseSpecificCatch") |
||
30 | class H2SpatialResource |
||
31 | extends AbstractDataResource
|
||
32 | implements DataResource
|
||
33 | { |
||
34 | |||
35 | 44190 | jjdelcerro | private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(H2SpatialResource.class); |
36 | |||
37 | 44189 | jjdelcerro | 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 | 44190 | jjdelcerro | private final String resourceName; |
44 | 44189 | jjdelcerro | |
45 | 44190 | jjdelcerro | public H2SpatialResource(File zipFile, String tableName, String resourceName) { |
46 | 44189 | jjdelcerro | this.zipFile = zipFile;
|
47 | this.tableName = tableName;
|
||
48 | 44190 | jjdelcerro | this.resourceName = resourceName;
|
49 | 44189 | jjdelcerro | 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 | 44190 | jjdelcerro | return new URL(this.zipfsuri.toString()+"!/"+this.tableName+"."+this.resourceName); |
59 | 44189 | jjdelcerro | } 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 | 44190 | jjdelcerro | Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName); |
78 | 44189 | jjdelcerro | if (tablePath == null) { |
79 | return false; |
||
80 | } |
||
81 | 44190 | jjdelcerro | if( Files.exists(tablePath) ) {
|
82 | return true; |
||
83 | } |
||
84 | return false; |
||
85 | 44189 | jjdelcerro | } catch (Exception ex) { |
86 | 44190 | jjdelcerro | LOGGER.warn("Can't access to the resource ("+Objects.toString(this.getURL())+").", ex); |
87 | 44189 | jjdelcerro | 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 | 44190 | jjdelcerro | Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName); |
102 | 44189 | jjdelcerro | if (tablePath == null) { |
103 | return null; |
||
104 | } |
||
105 | this.in = Files.newInputStream(tablePath);
|
||
106 | return this.in; |
||
107 | } catch (Exception ex) { |
||
108 | 44190 | jjdelcerro | LOGGER.warn("Can't create input stream ("+Objects.toString(this.getURL())+").", ex); |
109 | 44189 | jjdelcerro | return null; |
110 | } |
||
111 | } |
||
112 | |||
113 | @Override
|
||
114 | public OutputStream asOutputStream() throws IOException { |
||
115 | if (this.in != null || this.out != null) { |
||
116 | 44190 | jjdelcerro | throw new IllegalStateException("Resource is already open ("+Objects.toString(this.getURL())+")."); |
117 | 44189 | jjdelcerro | } |
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 | 44190 | jjdelcerro | Path tablePath = this.zipfs.getPath(this.tableName+"."+this.resourceName); |
133 | 44189 | jjdelcerro | pipe = new ByteArrayInputStream(this.out.toByteArray()); |
134 | 44190 | jjdelcerro | Files.copy(pipe, tablePath, StandardCopyOption.REPLACE_EXISTING); |
135 | 44189 | jjdelcerro | } catch (Exception ex) { |
136 | 44190 | jjdelcerro | LOGGER.warn("Can't write resource ("+Objects.toString(this.getURL())+").", ex); |
137 | 44189 | jjdelcerro | } 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 | 44190 | jjdelcerro | this.zipfs = null; |
146 | 44189 | jjdelcerro | } |
147 | } |
||
148 | |||
149 | } |