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 @ 44189
History | View | Annotate | Download (4.11 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.util.Collections; |
18 |
import java.util.Objects; |
19 |
import java.util.logging.Level; |
20 |
import java.util.logging.Logger; |
21 |
import org.apache.commons.io.IOUtils; |
22 |
import org.gvsig.fmap.dal.AbstractDataResource; |
23 |
import org.gvsig.fmap.dal.DataServerExplorer.DataResource; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
@SuppressWarnings("UseSpecificCatch") |
30 |
class H2SpatialResource |
31 |
extends AbstractDataResource
|
32 |
implements DataResource
|
33 |
{ |
34 |
|
35 |
private final File zipFile; |
36 |
private final String tableName; |
37 |
private InputStream in; |
38 |
private ByteArrayOutputStream out; |
39 |
private FileSystem zipfs;
|
40 |
private final URI zipfsuri; |
41 |
|
42 |
public H2SpatialResource(File zipFile, String tableName) { |
43 |
this.zipFile = zipFile;
|
44 |
this.tableName = tableName;
|
45 |
try {
|
46 |
this.zipfsuri = new URI("jar:" + this.zipFile.toURI().toString()); |
47 |
} catch (URISyntaxException ex) { |
48 |
throw new IllegalArgumentException("The zip file '" + Objects.toString(zipFile) + "' is not a valid file name.", ex); |
49 |
} |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public URL getURL() { |
54 |
try {
|
55 |
return new URL(this.zipfsuri.toString()+"!"+this.tableName); |
56 |
} catch (MalformedURLException ex) { |
57 |
return null; |
58 |
} |
59 |
} |
60 |
|
61 |
private void openzipfs(boolean create) throws IOException { |
62 |
if (this.zipfs == null) { |
63 |
this.zipfs = FileSystems.newFileSystem(this.zipfsuri, Collections.singletonMap("create", create ? "true" : "false")); |
64 |
} |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public boolean exists() { |
69 |
if (!this.zipFile.exists()) { |
70 |
return false; |
71 |
} |
72 |
try {
|
73 |
this.openzipfs(false); |
74 |
Path tablePath = this.zipfs.getPath(this.tableName); |
75 |
if (tablePath == null) { |
76 |
return false; |
77 |
} |
78 |
return true; |
79 |
} catch (Exception ex) { |
80 |
return false; |
81 |
} |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public InputStream asInputStream() throws IOException { |
86 |
if (!this.zipFile.exists()) { |
87 |
return null; |
88 |
} |
89 |
if (this.in != null || this.out != null) { |
90 |
throw new IllegalStateException("Resource is already open (" + this.zipFile.toString() + "!" + this.tableName + ")"); |
91 |
} |
92 |
try {
|
93 |
this.openzipfs(false); |
94 |
Path tablePath = this.zipfs.getPath(this.tableName); |
95 |
if (tablePath == null) { |
96 |
return null; |
97 |
} |
98 |
this.in = Files.newInputStream(tablePath);
|
99 |
return this.in; |
100 |
} catch (Exception ex) { |
101 |
return null; |
102 |
} |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public OutputStream asOutputStream() throws IOException { |
107 |
if (this.in != null || this.out != null) { |
108 |
throw new IllegalStateException("Resource is already open (" + this.zipFile.toString() + "!" + this.tableName + ")"); |
109 |
} |
110 |
this.out = new ByteArrayOutputStream(); |
111 |
return this.out; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void close() { |
116 |
if (this.in != null) { |
117 |
IOUtils.closeQuietly(this.in);
|
118 |
this.in = null; |
119 |
} |
120 |
if (this.out != null) { |
121 |
ByteArrayInputStream pipe = null; |
122 |
try {
|
123 |
this.openzipfs(true); |
124 |
Path tablePath = this.zipfs.getPath(this.tableName); |
125 |
pipe = new ByteArrayInputStream(this.out.toByteArray()); |
126 |
Files.copy(pipe, tablePath); |
127 |
} catch (Exception ex) { |
128 |
} finally {
|
129 |
IOUtils.closeQuietly(pipe); |
130 |
IOUtils.closeQuietly(this.out);
|
131 |
this.out = null; |
132 |
} |
133 |
} |
134 |
if (this.zipfs != null) { |
135 |
IOUtils.closeQuietly(this.zipfs);
|
136 |
} |
137 |
} |
138 |
|
139 |
} |