svn-gvsig-desktop / 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 @ 46414
History | View | Annotate | Download (8.47 KB)
1 |
package org.gvsig.fmap.dal.store.h2; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.sql.SQLException; |
5 |
import java.text.MessageFormat; |
6 |
import org.apache.commons.io.FilenameUtils; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import static org.gvsig.fmap.dal.store.h2.H2SpatialHelper.LOGGER; |
9 |
import org.h2.tools.Server; |
10 |
import org.h2gis.functions.system.H2GISversion; |
11 |
|
12 |
/**
|
13 |
*
|
14 |
* @author gvSIG Team
|
15 |
*/
|
16 |
public class H2SpatialUtils { |
17 |
|
18 |
private static boolean enable_server = true; |
19 |
private static Server h2server = null; |
20 |
private static boolean startServer = true; |
21 |
private static boolean lastAllowOthers = true; |
22 |
private static String lastPort = "9123"; |
23 |
|
24 |
public static File[] getH2Files(File f) { |
25 |
if( f==null ) { |
26 |
return null; |
27 |
} |
28 |
String name = removeH2FileNameExtension(f.getName());
|
29 |
File[] files = new File[62]; |
30 |
files[0] = new File(f.getParentFile(), name+".mv.db"); |
31 |
files[1] = new File(f.getParentFile(), name+".trace.db"); |
32 |
for (int i = 2; i < files.length; i++) { |
33 |
files[i] = new File(f.getParentFile(), name+".mv.db."+(i-1)+".part"); |
34 |
} |
35 |
return files;
|
36 |
} |
37 |
|
38 |
public static File normalizeH2File(File f) { |
39 |
if( f==null ) { |
40 |
return null; |
41 |
} |
42 |
f = new File(f.getParentFile(), normalizeH2FileName(f.getName())); |
43 |
return f;
|
44 |
} |
45 |
|
46 |
public static String normalizeH2FileName(String name) { |
47 |
String s = removeH2FileNameExtension(name);
|
48 |
if( s == null ) { |
49 |
return null; |
50 |
} |
51 |
return s+".mv.db"; |
52 |
} |
53 |
|
54 |
public static String removeH2FileNameExtension(String name) { |
55 |
if( StringUtils.isBlank(name) ) {
|
56 |
return null; |
57 |
} |
58 |
if( name.endsWith(".mv.db") ) { |
59 |
int l = name.length();
|
60 |
return name.substring(0, l-6); |
61 |
} |
62 |
if( name.endsWith(".trace.db") ) { |
63 |
int l = name.length();
|
64 |
return name.substring(0, l-9); |
65 |
} |
66 |
return FilenameUtils.removeExtension(name);
|
67 |
} |
68 |
|
69 |
public static File getLocalFile(H2SpatialConnectionParameters params) { |
70 |
String host = params.getHost();
|
71 |
if( !StringUtils.isEmpty(host) ) {
|
72 |
host = host.toLowerCase().trim(); |
73 |
if( !(host.equals("localhost") || host.equals("127.0.0.1")) ) { |
74 |
return null; |
75 |
} |
76 |
} |
77 |
File f = params.getFile();
|
78 |
return normalizeH2File(f);
|
79 |
} |
80 |
|
81 |
public static String getConnectionURL(H2SpatialConnectionParameters params) { |
82 |
String connectionURL;
|
83 |
String dbfilename = params.getFile().getAbsolutePath().replace("\\","/"); |
84 |
if( dbfilename!=null ) { |
85 |
dbfilename = H2SpatialUtils.removeH2FileNameExtension(dbfilename); |
86 |
} |
87 |
StringBuilder commonParameters = new StringBuilder(); |
88 |
commonParameters.append(";MODE=PostgreSQL");
|
89 |
commonParameters.append(";SCHEMA=PUBLIC");
|
90 |
commonParameters.append(";ALLOW_LITERALS=ALL");
|
91 |
int splitSize = params.getSplitSize();
|
92 |
|
93 |
if( StringUtils.isEmpty(params.getHost()) ) {
|
94 |
// Asumimos que es una conexion directa sobre el filesystem
|
95 |
if( StringUtils.equalsIgnoreCase(FilenameUtils.getExtension(params.getFile().getName()),"zip") ) { |
96 |
connectionURL = MessageFormat.format(
|
97 |
"jdbc:h2:zip:{0}!/{1}"+commonParameters.toString(),
|
98 |
dbfilename, |
99 |
params.getDBName() |
100 |
); |
101 |
} else if( splitSize>0 ) { |
102 |
connectionURL = MessageFormat.format(
|
103 |
"jdbc:h2:split:{1,number,##}:{0}"+commonParameters.toString(),
|
104 |
dbfilename, |
105 |
splitSize |
106 |
); |
107 |
} else {
|
108 |
connectionURL = MessageFormat.format(
|
109 |
"jdbc:h2:file:{0}"+commonParameters.toString(),
|
110 |
dbfilename |
111 |
); |
112 |
} |
113 |
} else if( params.getPort() == null ) { |
114 |
if( splitSize>0 ) { |
115 |
connectionURL = MessageFormat.format(
|
116 |
"jdbc:h2:tcp://{0}/split:{2,number,##}:/{1}"+commonParameters.toString(),
|
117 |
params.getHost(), |
118 |
dbfilename, |
119 |
splitSize |
120 |
); |
121 |
} else {
|
122 |
connectionURL = MessageFormat.format(
|
123 |
"jdbc:h2:tcp://{0}/{1}"+commonParameters.toString(),
|
124 |
params.getHost(), |
125 |
dbfilename |
126 |
); |
127 |
} |
128 |
} else if( splitSize>0 ) { |
129 |
connectionURL = MessageFormat.format("jdbc:h2:tcp://{0}:{1,number,#######}/split:{3,number,##}:/{2}"+commonParameters.toString(), |
130 |
params.getHost(), |
131 |
(int) params.getPort(),
|
132 |
dbfilename, |
133 |
splitSize |
134 |
); |
135 |
} else {
|
136 |
connectionURL = MessageFormat.format("jdbc:h2:tcp://{0}:{1,number,#######}/{2}"+commonParameters.toString(), |
137 |
params.getHost(), |
138 |
(int) params.getPort(),
|
139 |
dbfilename |
140 |
); |
141 |
} |
142 |
LOGGER.debug("connectionURL: {}", connectionURL);
|
143 |
return connectionURL;
|
144 |
} |
145 |
|
146 |
public static void server_stop() { |
147 |
if (h2server == null) { |
148 |
LOGGER.info("The H2 server is already stopped.");
|
149 |
} else {
|
150 |
LOGGER.info("Stopping the H2 server.");
|
151 |
LOGGER.info(" port :" + h2server.getPort());
|
152 |
LOGGER.info(" URL :" + h2server.getURL());
|
153 |
LOGGER.info(" shutdown server...");
|
154 |
try {
|
155 |
h2server.shutdown(); |
156 |
} catch (Throwable th) { |
157 |
LOGGER.warn("Problems shutdown the H2 server.", th);
|
158 |
} |
159 |
LOGGER.info(" Stoping server...");
|
160 |
try {
|
161 |
h2server.stop(); |
162 |
} catch (Throwable th) { |
163 |
LOGGER.warn("Problems stopping the H2 server.", th);
|
164 |
} |
165 |
LOGGER.info(" status:" + h2server.getStatus());
|
166 |
h2server = null;
|
167 |
LOGGER.info("H2 Server stopped");
|
168 |
} |
169 |
startServer = true;
|
170 |
} |
171 |
|
172 |
public static void server_start() { |
173 |
server_start(lastPort, lastAllowOthers); |
174 |
} |
175 |
|
176 |
public static void server_start(String port, Boolean allowOthers) { |
177 |
if( !enable_server ) {
|
178 |
return;
|
179 |
} |
180 |
if( startServer && h2server == null ) { |
181 |
if( StringUtils.isBlank(port) ) {
|
182 |
port = lastPort; |
183 |
} |
184 |
if( allowOthers==null ) { |
185 |
allowOthers = lastAllowOthers; |
186 |
} |
187 |
try {
|
188 |
Server theH2Server; |
189 |
if( allowOthers) {
|
190 |
theH2Server = Server.createTcpServer("-tcpPort", port, "-ifExists", "-tcpAllowOthers"); |
191 |
} else {
|
192 |
theH2Server = Server.createTcpServer("-tcpPort", port, "-ifExists"); |
193 |
} |
194 |
theH2Server.start(); |
195 |
h2server = theH2Server; |
196 |
LOGGER.info("H2 Server started" );
|
197 |
LOGGER.info(" Engine version : h2 "+ org.h2.engine.Constants.getFullVersion()+", h2gis "+H2GISversion.geth2gisVersion()); |
198 |
LOGGER.info(" Connection url : jdbc:h2:"+h2server.getURL()+"/ABSOLUTE_DATABASE_PATH;MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL"); |
199 |
LOGGER.info(" status:"+ h2server.getStatus());
|
200 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
201 |
@Override
|
202 |
public void run() { |
203 |
server_stop(); |
204 |
} |
205 |
}); |
206 |
lastPort = port; |
207 |
lastAllowOthers = allowOthers; |
208 |
} catch (SQLException ex) { |
209 |
LOGGER.warn("H2 Server not started",ex);
|
210 |
} |
211 |
// Tanto si consigue lanzar el server como si no, no lo vuelve a intentar
|
212 |
startServer = false;
|
213 |
} |
214 |
} |
215 |
|
216 |
public static void set_enable_server(boolean enable) { |
217 |
enable_server = enable; |
218 |
} |
219 |
|
220 |
public static void set_server_port(int port) { |
221 |
lastPort = Integer.toString(port);
|
222 |
} |
223 |
|
224 |
public static boolean is_enable_server() { |
225 |
return enable_server;
|
226 |
} |
227 |
|
228 |
public static boolean is_server_started() { |
229 |
return h2server!=null; |
230 |
} |
231 |
} |