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 / H2SpatialHelper.java @ 44346
History | View | Annotate | Download (12.1 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2016 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.fmap.dal.store.h2; |
23 |
|
24 |
import java.sql.Connection; |
25 |
import java.sql.SQLException; |
26 |
import java.text.MessageFormat; |
27 |
import org.apache.commons.dbcp.BasicDataSource; |
28 |
import org.apache.commons.io.FilenameUtils; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
31 |
import org.gvsig.fmap.dal.exception.InitializeException; |
32 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
33 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
34 |
import org.gvsig.fmap.dal.store.h2.operations.H2SpatialOperationsFactory; |
35 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
36 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
37 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
38 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
39 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
41 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCServerExplorerBase; |
45 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase; |
46 |
import org.h2.tools.Server; |
47 |
import org.h2gis.ext.H2GISExtension; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
@SuppressWarnings("UseSpecificCatch") |
52 |
public class H2SpatialHelper extends JDBCHelperBase { |
53 |
|
54 |
static final Logger LOGGER = LoggerFactory.getLogger(H2SpatialHelper.class); |
55 |
|
56 |
public static final String H2SPATIAL_JDBC_DRIVER = "org.h2.Driver"; |
57 |
|
58 |
public static String getConnectionURL(H2SpatialConnectionParameters params) { |
59 |
String connectionURL;
|
60 |
String dbfilename = params.getFile().getAbsolutePath().replace("\\","/"); |
61 |
if( dbfilename!=null && dbfilename.endsWith(".mv.db") ) { |
62 |
dbfilename = dbfilename.substring(0, dbfilename.length()-6); |
63 |
} |
64 |
if( StringUtils.isEmpty(params.getHost()) ) {
|
65 |
// Asumimos que es una conexion directa sobre el filesystem
|
66 |
if( StringUtils.equalsIgnoreCase(FilenameUtils.getExtension(params.getFile().getName()),"zip") ) { |
67 |
connectionURL = MessageFormat.format(
|
68 |
"jdbc:h2:zip:{0}!/{1};MODE=PostgreSQL",
|
69 |
dbfilename, |
70 |
params.getDBName() |
71 |
); |
72 |
} else {
|
73 |
connectionURL = MessageFormat.format(
|
74 |
"jdbc:h2:file:{0};MODE=PostgreSQL",
|
75 |
dbfilename |
76 |
); |
77 |
} |
78 |
} else if( params.getPort() == null ) { |
79 |
connectionURL = MessageFormat.format(
|
80 |
"jdbc:h2:tcp://{0}/{1};MODE=PostgreSQL",
|
81 |
params.getHost(), |
82 |
dbfilename |
83 |
); |
84 |
} else {
|
85 |
connectionURL = MessageFormat.format(
|
86 |
"jdbc:h2:tcp://{0}:{1,number,#######}/{2};MODE=PostgreSQL",
|
87 |
params.getHost(), |
88 |
params.getPort().intValue(), |
89 |
dbfilename |
90 |
); |
91 |
} |
92 |
LOGGER.debug("connectionURL: {}", connectionURL);
|
93 |
return connectionURL;
|
94 |
} |
95 |
|
96 |
private static class ConnectionProvider { |
97 |
|
98 |
private static boolean needRegisterDriver = true; |
99 |
|
100 |
private BasicDataSource dataSource = null; |
101 |
|
102 |
private final H2SpatialConnectionParameters connectionParameters; |
103 |
|
104 |
private static Server server = null; |
105 |
private static boolean startServer = true; |
106 |
|
107 |
public ConnectionProvider(H2SpatialConnectionParameters connectionParameters) {
|
108 |
this.connectionParameters = connectionParameters;
|
109 |
} |
110 |
|
111 |
private void startServer() { |
112 |
|
113 |
if( startServer && server == null ) { |
114 |
String port = "9123"; |
115 |
try {
|
116 |
Server theServer; |
117 |
String s = System.getProperty("H2Port"); |
118 |
if( s!=null ) { |
119 |
try {
|
120 |
int n = Integer.parseInt(s); |
121 |
port = String.valueOf(n);
|
122 |
} catch(Throwable th) { |
123 |
// Ignore port number, use default.
|
124 |
} |
125 |
} |
126 |
if( System.getProperty("H2AllowOthers")!=null ) { |
127 |
theServer = Server.createTcpServer("-tcpPort", port, "-tcpAllowOthers", "-ifExists"); |
128 |
} else {
|
129 |
theServer = Server.createTcpServer("-tcpPort", port, "-ifExists"); |
130 |
} |
131 |
theServer.start(); |
132 |
server = theServer; |
133 |
LOGGER.info("H2 Server started" );
|
134 |
LOGGER.info(" port :"+ server.getPort());
|
135 |
LOGGER.info(" URL :"+ server.getURL());
|
136 |
LOGGER.info(" status:"+ server.getStatus());
|
137 |
} catch (SQLException ex) { |
138 |
LOGGER.warn("H2 Server not started",ex);
|
139 |
} |
140 |
// Tanto si consigue lanzar el server como si no, no lo vuelve a intentar
|
141 |
startServer = false;
|
142 |
} |
143 |
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public String toString() { |
148 |
StringBuilder builder = new StringBuilder(); |
149 |
builder.append(" url=").append(connectionParameters.getUrl());
|
150 |
builder.append(" driver name=").append(connectionParameters.getJDBCDriverClassName());
|
151 |
builder.append(" user=").append(connectionParameters.getUser());
|
152 |
return builder.toString();
|
153 |
} |
154 |
|
155 |
public Connection getConnection() throws SQLException { |
156 |
if (this.dataSource == null) { |
157 |
this.dataSource = this.createDataSource(); |
158 |
} |
159 |
Connection conn = this.dataSource.getConnection(); |
160 |
try {
|
161 |
conn.createStatement().execute("SELECT TOP 1 SRID FROM SPATIAL_REF_SYS");
|
162 |
} catch(SQLException ex) { |
163 |
H2GISExtension.load(conn); |
164 |
} |
165 |
return conn;
|
166 |
} |
167 |
|
168 |
private BasicDataSource createDataSource() throws SQLException { |
169 |
if (!this.isRegistered()) { |
170 |
this.registerDriver();
|
171 |
} |
172 |
startServer(); |
173 |
H2SpatialConnectionParameters params = connectionParameters; |
174 |
|
175 |
BasicDataSource ds = new BasicDataSource();
|
176 |
ds.setDriverClassName(params.getJDBCDriverClassName()); |
177 |
if( !StringUtils.isEmpty(params.getUser()) ) {
|
178 |
ds.setUsername(params.getUser()); |
179 |
} |
180 |
if( !StringUtils.isEmpty(params.getPassword()) ) {
|
181 |
ds.setPassword(params.getPassword()); |
182 |
} |
183 |
ds.setUrl(params.getUrl()); |
184 |
|
185 |
ds.setMaxWait(60L * 1000); |
186 |
return ds;
|
187 |
} |
188 |
|
189 |
private boolean isRegistered() { |
190 |
return needRegisterDriver;
|
191 |
} |
192 |
|
193 |
public void registerDriver() throws SQLException { |
194 |
String className = this.connectionParameters.getJDBCDriverClassName(); |
195 |
if (className == null) { |
196 |
return;
|
197 |
} |
198 |
try {
|
199 |
Class theClass = Class.forName(className); |
200 |
if (theClass == null) { |
201 |
throw new JDBCDriverClassNotFoundException(H2SpatialLibrary.NAME, className); |
202 |
} |
203 |
} catch (Exception e) { |
204 |
throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
205 |
} |
206 |
needRegisterDriver = false;
|
207 |
} |
208 |
|
209 |
} |
210 |
|
211 |
private ConnectionProvider connectionProvider = null; |
212 |
|
213 |
public H2SpatialHelper(JDBCConnectionParameters connectionParameters) {
|
214 |
super(connectionParameters);
|
215 |
this.srssolver = new SRSSolverBase(this); |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public synchronized Connection getConnection() throws AccessResourceException { |
220 |
try {
|
221 |
if (this.connectionProvider == null) { |
222 |
this.connectionProvider = new ConnectionProvider(this.getConnectionParameters()); |
223 |
} |
224 |
Connection connection = this.connectionProvider.getConnection(); |
225 |
if( LOGGER.isDebugEnabled() ) {
|
226 |
LOGGER.debug("getConnection: connection = "+connection.hashCode()+ connectionProvider.toString());
|
227 |
} |
228 |
return connection;
|
229 |
} catch (SQLException ex) { |
230 |
throw new AccessResourceException(H2SpatialLibrary.NAME, ex); |
231 |
} |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void closeConnection(Connection connection) { |
236 |
LOGGER.debug("closeConnection: connection = "+connection.hashCode());
|
237 |
super.closeConnection(connection);
|
238 |
} |
239 |
|
240 |
@Override
|
241 |
public H2SpatialConnectionParameters getConnectionParameters() {
|
242 |
return (H2SpatialConnectionParameters) super.getConnectionParameters(); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public String getConnectionURL() { |
247 |
return getConnectionURL(this.getConnectionParameters()); |
248 |
} |
249 |
|
250 |
@Override
|
251 |
protected String getResourceType() { |
252 |
return H2SpatialLibrary.NAME;
|
253 |
} |
254 |
|
255 |
@Override
|
256 |
public String getProviderName() { |
257 |
return H2SpatialLibrary.NAME;
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
public JDBCSQLBuilderBase createSQLBuilder() {
|
262 |
return new H2SpatialSQLBuilder(this); |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public OperationsFactory getOperations() {
|
267 |
if (this.operationsFactory == null) { |
268 |
this.operationsFactory = new H2SpatialOperationsFactory(this); |
269 |
} |
270 |
return operationsFactory;
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
public GeometrySupportType getGeometrySupportType() {
|
275 |
return GeometrySupportType.WKB;
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public boolean hasSpatialFunctions() { |
280 |
return true; |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
285 |
return true; |
286 |
} |
287 |
|
288 |
@Override
|
289 |
public String getQuoteForIdentifiers() { |
290 |
return "\""; |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public boolean allowAutomaticValues() { |
295 |
return true; |
296 |
} |
297 |
|
298 |
@Override
|
299 |
public boolean supportOffsetInSelect() { |
300 |
return true; |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public String getQuoteForStrings() { |
305 |
return "'"; |
306 |
} |
307 |
|
308 |
@Override
|
309 |
public String getSourceId(JDBCStoreParameters parameters) { |
310 |
return parameters.getDBName() + "." + |
311 |
parameters.getSchema()+ "." +
|
312 |
parameters.getTable(); |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public JDBCNewStoreParameters createNewStoreParameters() {
|
317 |
return new H2SpatialNewStoreParameters(); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public JDBCStoreParameters createOpenStoreParameters() {
|
322 |
return new H2SpatialStoreParameters(); |
323 |
} |
324 |
|
325 |
@Override
|
326 |
public JDBCServerExplorerParameters createServerExplorerParameters() {
|
327 |
return new H2SpatialExplorerParameters(); |
328 |
} |
329 |
|
330 |
@Override
|
331 |
public JDBCServerExplorer createServerExplorer(
|
332 |
JDBCServerExplorerParameters parameters, |
333 |
DataServerExplorerProviderServices providerServices |
334 |
) throws InitializeException {
|
335 |
|
336 |
JDBCServerExplorer explorer = new JDBCServerExplorerBase(
|
337 |
parameters, |
338 |
providerServices, |
339 |
this
|
340 |
); |
341 |
this.initialize(explorer, parameters, null); |
342 |
return explorer;
|
343 |
} |
344 |
} |