Revision 47716 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.h2spatial/org.gvsig.h2spatial.h2gis132/org.gvsig.h2spatial.h2gis132.provider/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialUtils.java
H2SpatialUtils.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.store.h2; |
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 |
import java.sql.Connection; |
|
4 | 5 |
import java.sql.SQLException; |
5 | 6 |
import java.text.MessageFormat; |
7 |
import java.util.HashMap; |
|
8 |
import java.util.Map; |
|
9 |
import org.apache.commons.codec.binary.Hex; |
|
6 | 10 |
import org.apache.commons.io.FileUtils; |
7 | 11 |
import org.apache.commons.io.FilenameUtils; |
8 | 12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.fmap.dal.store.h2.H2SpatialHelper.H2SpatialConnectionProvider; |
|
9 | 14 |
import static org.gvsig.fmap.dal.store.h2.H2SpatialHelper.LOGGER; |
15 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
16 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
17 |
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider; |
|
10 | 18 |
import org.h2.tools.Server; |
11 | 19 |
import org.h2gis.functions.system.H2GISversion; |
12 | 20 |
|
... | ... | |
21 | 29 |
private static boolean startServer = true; |
22 | 30 |
private static boolean lastAllowOthers = true; |
23 | 31 |
private static String lastPort = "9123"; |
32 |
private static Map<String,Connection> globalConnections; |
|
33 |
|
|
24 | 34 |
|
25 | 35 |
private static Thread shutdownHook = new Thread("H2_shutdown_hook") { |
26 | 36 |
@Override |
... | ... | |
179 | 189 |
if (h2server == null) { |
180 | 190 |
LOGGER.info("The H2 server is already stopped."); |
181 | 191 |
} else { |
192 |
closeGlobalConnections(); |
|
182 | 193 |
LOGGER.info("Stopping the H2 server."); |
183 | 194 |
LOGGER.info(" port :" + h2server.getPort()); |
184 | 195 |
LOGGER.info(" URL :" + h2server.getURL()); |
... | ... | |
256 | 267 |
public static synchronized boolean is_server_started() { |
257 | 268 |
return h2server!=null; |
258 | 269 |
} |
270 |
|
|
271 |
public static synchronized void addGlobalConnection(H2SpatialConnectionProvider connectionProvider) throws SQLException { |
|
272 |
if( globalConnections==null ) { |
|
273 |
globalConnections = new HashMap<>(); |
|
274 |
} |
|
275 |
H2SpatialConnectionParameters connectionParameters = connectionProvider.getConnectionParameters(); |
|
276 |
if( !connectionParameters.getMaintainGlobalConnection() ) { |
|
277 |
return; |
|
278 |
} |
|
279 |
String connectionProviderKey = getConnectionProviderKey(connectionParameters); |
|
280 |
Connection x = globalConnections.get(connectionProviderKey); |
|
281 |
if( x != null ) { |
|
282 |
return; |
|
283 |
} |
|
284 |
x = connectionProvider.getConnection(); |
|
285 |
globalConnections.put(connectionProviderKey, x); |
|
286 |
} |
|
287 |
|
|
288 |
public static String getConnectionProviderKey(JDBCConnectionParameters connectionParameters) { |
|
289 |
String pass = Hex.encodeHexString((connectionParameters.getPassword()+"").getBytes()); |
|
290 |
// String pass = connectionParameters.getPassword(); |
|
291 |
return connectionParameters.getUrl() + ";user:"+connectionParameters.getUser()+"@"+pass; |
|
292 |
} |
|
293 |
|
|
294 |
public static synchronized void closeGlobalConnections() { |
|
295 |
if( globalConnections==null ) { |
|
296 |
return; |
|
297 |
} |
|
298 |
for (Connection conn : globalConnections.values()) { |
|
299 |
LOGGER.info("Clossing connection "+ JDBCUtils.getConnId(conn)); |
|
300 |
JDBCUtils.closeQuietly(conn); |
|
301 |
} |
|
302 |
globalConnections = null; |
|
303 |
} |
|
259 | 304 |
} |
Also available in: Unified diff