Revision 44189 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/H2SpatialExplorer.java
H2SpatialExplorer.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.store.h2; |
2 | 2 |
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.ByteArrayOutputStream; |
|
5 | 3 |
import java.io.File; |
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.io.OutputStream; |
|
9 |
import java.net.URI; |
|
10 |
import java.nio.file.FileSystem; |
|
11 |
import java.nio.file.FileSystems; |
|
12 |
import java.nio.file.Files; |
|
13 |
import java.nio.file.Path; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.Map; |
|
16 | 4 |
import org.apache.commons.io.FilenameUtils; |
17 |
import org.apache.commons.io.IOUtils; |
|
18 | 5 |
import org.gvsig.fmap.dal.DataStore; |
19 | 6 |
import org.gvsig.fmap.dal.exception.DataException; |
20 | 7 |
import org.gvsig.fmap.dal.exception.InitializeException; |
... | ... | |
29 | 16 |
*/ |
30 | 17 |
@SuppressWarnings("UseSpecificCatch") |
31 | 18 |
public class H2SpatialExplorer extends JDBCServerExplorerBase { |
32 |
|
|
33 |
private class H2Resource implements DataResource { |
|
34 |
|
|
35 |
private final File zipFile; |
|
36 |
private final String tableName; |
|
37 |
private InputStream in; |
|
38 |
private ByteArrayOutputStream out; |
|
39 |
private FileSystem fs; |
|
40 | 19 |
|
41 |
public H2Resource(File zipFile, String tableName) { |
|
42 |
this.zipFile = zipFile; |
|
43 |
this.tableName = tableName; |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public boolean exists() { |
|
48 |
if( !this.zipFile.exists() ) { |
|
49 |
return false; |
|
50 |
} |
|
51 |
try { |
|
52 |
Map<String, String> attributes = new HashMap<>(); |
|
53 |
attributes.put("create", "false"); |
|
54 |
URI zipURI = new URI("zip:"+this.zipFile.toURI().toString()); |
|
55 |
this.fs = FileSystems.newFileSystem(zipURI, attributes); |
|
56 |
Path tablePath = fs.getPath(this.tableName); |
|
57 |
if( tablePath==null ) { |
|
58 |
IOUtils.closeQuietly(fs); |
|
59 |
this.fs = null; |
|
60 |
return false; |
|
61 |
} |
|
62 |
return true; |
|
63 |
|
|
64 |
} catch(Exception ex) { |
|
65 |
IOUtils.closeQuietly(fs); |
|
66 |
this.fs = null; |
|
67 |
return false; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public InputStream asInputStream() throws IOException { |
|
73 |
if( !this.zipFile.exists()) { |
|
74 |
return null; |
|
75 |
} |
|
76 |
if( this.in!=null || this.out!=null ) { |
|
77 |
throw new IllegalStateException("Resource is already open ("+this.zipFile.toString()+"!"+this.tableName+")"); |
|
78 |
} |
|
79 |
try { |
|
80 |
Map<String, String> attributes = new HashMap<>(); |
|
81 |
attributes.put("create", "false"); |
|
82 |
URI zipURI = new URI("zip:"+this.zipFile.toURI().toString()); |
|
83 |
this.fs = FileSystems.newFileSystem(zipURI, attributes); |
|
84 |
Path tablePath = fs.getPath(this.tableName); |
|
85 |
if( tablePath==null ) { |
|
86 |
IOUtils.closeQuietly(fs); |
|
87 |
this.fs = null; |
|
88 |
return null; |
|
89 |
} |
|
90 |
this.in = Files.newInputStream(tablePath); |
|
91 |
return this.in; |
|
92 |
|
|
93 |
} catch(Exception ex) { |
|
94 |
IOUtils.closeQuietly(fs); |
|
95 |
this.fs = null; |
|
96 |
return null; |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public OutputStream asOutputStream() throws IOException { |
|
102 |
if( !this.zipFile.exists() ) { |
|
103 |
return null; |
|
104 |
} |
|
105 |
if( this.in!=null || this.out!=null ) { |
|
106 |
throw new IllegalStateException("Resource is already open ("+this.zipFile.toString()+"!"+this.tableName+")"); |
|
107 |
} |
|
108 |
this.out = new ByteArrayOutputStream(); |
|
109 |
return this.out; |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public void close() { |
|
114 |
if( this.in!=null ) { |
|
115 |
IOUtils.closeQuietly(this.in); |
|
116 |
this.in = null; |
|
117 |
} |
|
118 |
if(this.out != null ) { |
|
119 |
try { |
|
120 |
Map<String, String> attributes = new HashMap<>(); |
|
121 |
attributes.put("create", "true"); |
|
122 |
URI zipURI = new URI("zip:"+this.zipFile.toURI().toString()); |
|
123 |
this.fs = FileSystems.newFileSystem(zipURI, attributes); |
|
124 |
Path tablePath = fs.getPath(this.tableName); |
|
125 |
ByteArrayInputStream lin = new ByteArrayInputStream(this.out.toByteArray()); |
|
126 |
Files.copy(lin, tablePath); |
|
127 |
|
|
128 |
} catch(Exception ex) { |
|
129 |
IOUtils.closeQuietly(this.out); |
|
130 |
this.out = null; |
|
131 |
} |
|
132 |
} |
|
133 |
if( this.fs!=null ) { |
|
134 |
IOUtils.closeQuietly(this.fs); |
|
135 |
this.fs = null; |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 | 20 |
public H2SpatialExplorer(JDBCServerExplorerParameters parameters, DataServerExplorerProviderServices services, JDBCHelper helper) throws InitializeException { |
141 | 21 |
super(parameters, services, helper); |
142 | 22 |
} |
... | ... | |
151 | 31 |
String zipPath = this.getParameters().getFile().getAbsolutePath(); |
152 | 32 |
zipPath = FilenameUtils.removeExtension(zipPath); |
153 | 33 |
zipPath = zipPath + "." + resourceName; |
154 |
H2Resource resource = new H2Resource(new File(zipPath), dataStore.getName());
|
|
34 |
H2SpatialResource resource = new H2SpatialResource(new File(zipPath), dataStore.getName());
|
|
155 | 35 |
return resource; |
156 | 36 |
} |
157 | 37 |
|
Also available in: Unified diff