1 |
package skrueger.geotools.io; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.net.MalformedURLException; |
5 |
import java.sql.Connection; |
6 |
import java.sql.DriverManager; |
7 |
import java.sql.SQLException; |
8 |
import java.util.HashMap; |
9 |
import java.util.regex.Pattern; |
10 |
|
11 |
import org.apache.commons.lang.ArrayUtils; |
12 |
import org.apache.commons.lang.StringUtils; |
13 |
import org.apache.log4j.Logger; |
14 |
import org.geotools.data.postgis.PostgisNGDataStoreFactory; |
15 |
import org.geotools.jdbc.JDBCDataStore; |
16 |
import org.geotools.jdbc.JDBCDataStoreFactory; |
17 |
import org.jfree.util.Log; |
18 |
|
19 |
import schmitzm.swing.BooleanInputOption; |
20 |
import schmitzm.swing.ManualInputOption; |
21 |
import schmitzm.swing.ManualInputOption.Integer; |
22 |
import schmitzm.swing.ManualInputOption.PasswordViewable; |
23 |
import schmitzm.swing.ManualInputOption.Text; |
24 |
import schmitzm.swing.MultipleOptionPane; |
25 |
import schmitzm.swing.SelectionInputOption; |
26 |
import schmitzm.swing.SelectionInputOption.Combo; |
27 |
import skrueger.db.PGUtil; |
28 |
|
29 |
/** |
30 |
* This class describes all settings needed to connect to a WFS server. This |
31 |
* class extends a {@link HashMap} and contains two groups of keys:<br/> |
32 |
* The first group of keys, are all keys provided by Geotools to create a WFS |
33 |
* datastore, like: {@link JDBCDataStore#SCHEMA} .<br/> |
34 |
* The second group are additional keys defined in the enum |
35 |
* {@link GtDbServerSettings.Key}.<br/> |
36 |
* This class can serialize all important parameters needed to define the |
37 |
* connection into a {@link String} with {@link #toPropertiesString()} and |
38 |
* re-import the String with {@link #parsePropertiesString(String)}. |
39 |
*/ |
40 |
public class GtDbServerSettings extends AbstractGTServerSettings<Object, Object> { |
41 |
|
42 |
Logger log = Logger.getLogger(GtDbServerSettings.class); |
43 |
|
44 |
public enum DbType { |
45 |
postgis("postgresql"); |
46 |
|
47 |
private final String protocolString; |
48 |
|
49 |
DbType(String protocolString) { |
50 |
this.protocolString = protocolString; |
51 |
} |
52 |
|
53 |
public String getProtocolString() { |
54 |
return protocolString; |
55 |
}; |
56 |
} |
57 |
|
58 |
/** |
59 |
* params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis"); |
60 |
* params.put(JDBCDataStoreFactory.HOST.key, host); // the name or ip |
61 |
* params.put(JDBCDataStoreFactory.PORT.key, port); // the port that |
62 |
* |
63 |
* params.put(JDBCDataStoreFactory.DATABASE.key, database); // the |
64 |
* |
65 |
* // name params.put(JDBCDataStoreFactory.USER.key, username); // the user |
66 |
* to params.put(JDBCDataStoreFactory.PASSWD.key, password); // the |
67 |
* |
68 |
* params.put(JDBCDataStoreFactory.SCHEMA, schema); |
69 |
* |
70 |
* params.put(JDBCDataStoreFactory.EXPOSE_PK.key, true); * |
71 |
*/ |
72 |
|
73 |
public enum Key { |
74 |
} |
75 |
|
76 |
private static Connection dbc; |
77 |
|
78 |
/** |
79 |
* Opens a GUI that asks the use define a DB connection. |
80 |
* |
81 |
* @param dbServer |
82 |
* <code>null</code> to create a new instance, or an instance to |
83 |
* edit. |
84 |
* @return <code>null</code> if the user cancelled the creation of a new |
85 |
* {@link GtDbServerSettings}, otherwise the edited instance. |
86 |
*/ |
87 |
public static GtDbServerSettings createOrEdit(Component owner, |
88 |
GtDbServerSettings dbServer) { |
89 |
boolean newCreated = false; |
90 |
|
91 |
if (dbServer == null) { |
92 |
newCreated = true; |
93 |
dbServer = new GtDbServerSettings(DbType.postgis); |
94 |
} |
95 |
|
96 |
Combo<DbType> dpTypeInput = new SelectionInputOption.Combo<DbType>( |
97 |
"Database type", true, DbType.values(), ArrayUtils.indexOf( |
98 |
DbType.values(), dbServer.getDbType()), DbType.values()); |
99 |
|
100 |
Text hostInput = new ManualInputOption.Text("Hostname", true, |
101 |
dbServer.getHost()); |
102 |
|
103 |
Integer portInput = new ManualInputOption.Integer("Port", true, |
104 |
dbServer.getPort()); |
105 |
|
106 |
Text databaseInput = new ManualInputOption.Text("Database", true, |
107 |
dbServer.getDatabase()); |
108 |
|
109 |
Text schemaInput = new ManualInputOption.Text("Schema", true, |
110 |
dbServer.getSchema()); |
111 |
|
112 |
Text userInput = new ManualInputOption.Text("Username", true, |
113 |
dbServer.getUsername()); |
114 |
|
115 |
PasswordViewable passwdInput = new ManualInputOption.PasswordViewable( |
116 |
"Password", true, dbServer.getPassword()); |
117 |
|
118 |
BooleanInputOption exposePkInput = new BooleanInputOption( |
119 |
"Expose primary keys", dbServer.getExposePrimaryKey()); |
120 |
|
121 |
Object[] input = MultipleOptionPane.showMultipleInputDialog(owner, |
122 |
"DB Connection paramters", dpTypeInput, hostInput, portInput, |
123 |
databaseInput, schemaInput, userInput, passwdInput, |
124 |
exposePkInput); |
125 |
|
126 |
if (input == null) { |
127 |
if (newCreated) |
128 |
return null; |
129 |
else |
130 |
return dbServer; |
131 |
} else { |
132 |
dbServer.setDbType((DbType) input[0]); |
133 |
dbServer.setHost((String) input[1]); |
134 |
dbServer.setPort((java.lang.Integer) input[2]); |
135 |
dbServer.setDatabase((String) input[3]); |
136 |
dbServer.setSchema((String) input[4]); |
137 |
dbServer.setUsername((String) input[5]); |
138 |
dbServer.setPassword(String.valueOf((char[]) input[6])); |
139 |
dbServer.setExposePrimaryKey((Boolean) input[7]); |
140 |
} |
141 |
|
142 |
return dbServer; |
143 |
|
144 |
} |
145 |
|
146 |
/** |
147 |
* @return transforms the settings to a String that can be stored in a |
148 |
* .properties line. @see #parsePropertiesString |
149 |
* @throws MalformedURLException |
150 |
*/ |
151 |
@Override |
152 |
public boolean parsePropertiesString(String propString) { |
153 |
|
154 |
if (propString == null || propString.isEmpty()) |
155 |
return false; |
156 |
try { |
157 |
|
158 |
String[] split = propString.split(Pattern.quote(DELIMITER)); |
159 |
|
160 |
int i = 0; |
161 |
setDbType(DbType.valueOf(split[i++])); |
162 |
// DbServerSettings dbServer = new DbServerSettings( |
163 |
// DbType.valueOf(split[i++]) |
164 |
// ); |
165 |
|
166 |
setTitle(split[i++]); |
167 |
setHost(split[i++]); |
168 |
setPort(java.lang.Integer.valueOf(split[i++])); |
169 |
setUsername(StringUtils.stripToNull(split[i++])); |
170 |
setPassword(split[i++]); |
171 |
setDatabase(split[i++]); |
172 |
setExposePrimaryKey(Boolean.valueOf(split[i++])); |
173 |
setSchema(stringOrNull(split[i++])); |
174 |
|
175 |
return true; |
176 |
} catch (Exception e) { |
177 |
Log.warn("couldn't parse " + propString, e); |
178 |
return false; |
179 |
} |
180 |
} |
181 |
|
182 |
private String[] cachedTypeNames = null; |
183 |
private String[] cachedGeometryTableNames; |
184 |
|
185 |
public GtDbServerSettings() { |
186 |
this(DbType.postgis); |
187 |
|
188 |
} |
189 |
|
190 |
public GtDbServerSettings(DbType dbType) { |
191 |
setDbType(dbType); |
192 |
setHost("localhost"); |
193 |
setSchema("public"); |
194 |
put(JDBCDataStoreFactory.PK_METADATA_TABLE.key, null); |
195 |
} |
196 |
|
197 |
public GtDbServerSettings(String propertiesString) { |
198 |
parsePropertiesString(propertiesString); |
199 |
} |
200 |
|
201 |
public String[] getCachedTypeNames() { |
202 |
return cachedTypeNames; |
203 |
} |
204 |
|
205 |
public String getDatabase() { |
206 |
return (String) get(JDBCDataStoreFactory.DATABASE.key); |
207 |
} |
208 |
|
209 |
public DbType getDbType() { |
210 |
String dbt = (String) get(PostgisNGDataStoreFactory.DBTYPE.key); |
211 |
if (dbt != null) |
212 |
return DbType.valueOf(dbt); |
213 |
return null; |
214 |
} |
215 |
|
216 |
public Boolean getExposePrimaryKey() { |
217 |
Boolean expk = (Boolean) get(JDBCDataStoreFactory.EXPOSE_PK.key); |
218 |
if (expk == null) |
219 |
return (Boolean) JDBCDataStoreFactory.EXPOSE_PK.sample; |
220 |
return expk; |
221 |
} |
222 |
|
223 |
public String getHost() { |
224 |
return (String) get(JDBCDataStoreFactory.HOST.key); |
225 |
} |
226 |
|
227 |
public String getPassword() { |
228 |
return (String) get(JDBCDataStoreFactory.PASSWD.key); |
229 |
} |
230 |
|
231 |
public java.lang.Integer getPort() { |
232 |
java.lang.Integer port = (java.lang.Integer) get(JDBCDataStoreFactory.PORT.key); |
233 |
if (port == null) |
234 |
return (java.lang.Integer) JDBCDataStoreFactory.PORT.sample; |
235 |
return port; |
236 |
} |
237 |
|
238 |
public String getSchema() { |
239 |
return (String) get(JDBCDataStoreFactory.SCHEMA.key); |
240 |
} |
241 |
|
242 |
public String getUsername() { |
243 |
return (String) get(JDBCDataStoreFactory.USER.key); |
244 |
} |
245 |
|
246 |
/** |
247 |
* @return <code>true</code>, if all parameters look OK, without actually |
248 |
* opening any connection. |
249 |
*/ |
250 |
public boolean isWellDefined() { |
251 |
if (getDbType() == null) |
252 |
return false; |
253 |
|
254 |
if (getHost() == null) |
255 |
return false; |
256 |
|
257 |
if (getUsername() == null) |
258 |
return false; |
259 |
|
260 |
if (getPassword() == null) |
261 |
return false; |
262 |
|
263 |
if (getPort() == null) |
264 |
return false; |
265 |
|
266 |
if (getSchema() == null) |
267 |
return false; |
268 |
|
269 |
return true; |
270 |
} |
271 |
|
272 |
public void setCachedTypeNames(String[] cachedTypeNames) { |
273 |
this.cachedTypeNames = cachedTypeNames; |
274 |
} |
275 |
|
276 |
public void setDatabase(String db) { |
277 |
put(JDBCDataStoreFactory.DATABASE.key, db); |
278 |
} |
279 |
|
280 |
public void setDbType(DbType dbType) { |
281 |
put(PostgisNGDataStoreFactory.DBTYPE.key, dbType.toString()); |
282 |
|
283 |
if (dbType == DbType.postgis) { |
284 |
if (getPort() == null) { |
285 |
// For a PostGIS DBMS automatically set the port to 5432 |
286 |
setPort(5432); |
287 |
} |
288 |
if (getUsername() == null) { |
289 |
// For a PostGIS DBMS automatically set the user to postgres |
290 |
setUsername("postgres"); |
291 |
} |
292 |
|
293 |
} |
294 |
|
295 |
} |
296 |
|
297 |
public void setExposePrimaryKey(Boolean exportPk) { |
298 |
put(JDBCDataStoreFactory.EXPOSE_PK.key, exportPk); |
299 |
} |
300 |
|
301 |
public void setHost(String host) { |
302 |
put(JDBCDataStoreFactory.HOST.key, host); |
303 |
} |
304 |
|
305 |
public void setPassword(String password) { |
306 |
put(JDBCDataStoreFactory.PASSWD.key, password); |
307 |
} |
308 |
|
309 |
public void setPort(java.lang.Integer port) { |
310 |
put(JDBCDataStoreFactory.PORT.key, port); |
311 |
} |
312 |
|
313 |
public void setSchema(String schema) { |
314 |
put(JDBCDataStoreFactory.SCHEMA.key, schema); |
315 |
} |
316 |
|
317 |
public void setUsername(String username) { |
318 |
put(JDBCDataStoreFactory.USER.key, username); |
319 |
} |
320 |
|
321 |
/** |
322 |
* @return transforms the settings to a String that can be stored in a |
323 |
* .properties line. @see #parsePropertiesString |
324 |
*/ |
325 |
public String toPropertiesString() { |
326 |
|
327 |
StringBuffer serialized = new StringBuffer(100); |
328 |
|
329 |
serialized.append(getDbType().toString()); |
330 |
serialized.append(DELIMITER); |
331 |
|
332 |
serialized.append(getTitle()); |
333 |
serialized.append(DELIMITER); |
334 |
|
335 |
serialized.append(getHost()); |
336 |
serialized.append(DELIMITER); |
337 |
|
338 |
serialized.append(getPort().toString()); |
339 |
serialized.append(DELIMITER); |
340 |
|
341 |
serialized.append(getUsername()); |
342 |
serialized.append(DELIMITER); |
343 |
|
344 |
serialized.append(getPassword()); |
345 |
serialized.append(DELIMITER); |
346 |
|
347 |
serialized.append(getDatabase()); |
348 |
serialized.append(DELIMITER); |
349 |
|
350 |
serialized.append(getExposePrimaryKey().toString()); |
351 |
serialized.append(DELIMITER); |
352 |
|
353 |
serialized.append(getSchema().toString()); |
354 |
// serialized.append(DELIMITER); |
355 |
|
356 |
return serialized.toString(); |
357 |
} |
358 |
|
359 |
@Override |
360 |
public String toString() { |
361 |
|
362 |
StringBuffer s = new StringBuffer(); |
363 |
|
364 |
if (getDbType() != null) { |
365 |
s.append(getDbType().getProtocolString() + "://"); |
366 |
} |
367 |
|
368 |
s.append(getHost()); |
369 |
|
370 |
if (getPort() != null && getPort() != 5432) { |
371 |
s.append(":" + getPort()); |
372 |
} |
373 |
|
374 |
s.append("/" + getDatabase()); |
375 |
|
376 |
return s.toString(); |
377 |
} |
378 |
|
379 |
/** |
380 |
* returns a valid Connection to our postgresql Database. Name of database, |
381 |
* username and password must be provided. |
382 |
* |
383 |
* @param database |
384 |
* name of the database |
385 |
* @param username |
386 |
* a valid username |
387 |
* @param password |
388 |
* valid password |
389 |
* @return database connection |
390 |
* @throws SQLException |
391 |
* @throws ClassNotFoundException |
392 |
*/ |
393 |
public Connection getDatabaseConnection() { |
394 |
|
395 |
try { |
396 |
|
397 |
if (dbc == null || dbc.isClosed()) { |
398 |
dbc = createNewDatabaseConnection(); |
399 |
} |
400 |
return dbc; |
401 |
} catch (Exception e) { |
402 |
return null; |
403 |
} |
404 |
} |
405 |
|
406 |
private Connection createNewDatabaseConnection() throws SQLException { |
407 |
try { |
408 |
Class.forName("org.postgresql.Driver"); |
409 |
} catch (ClassNotFoundException e) { |
410 |
throw new RuntimeException(e); |
411 |
} |
412 |
String url = "jdbc:postgresql://" + getHost() |
413 |
// + (":" + PropertyUtils.getDbPort()) |
414 |
+ ("/" + getDatabase()); |
415 |
|
416 |
String password = getPassword(); |
417 |
String username = getUsername(); |
418 |
|
419 |
// log.debug("DB URL: " + url + " u=" + username + " p=" |
420 |
// + password); |
421 |
|
422 |
return DriverManager.getConnection(url, username, password); |
423 |
} |
424 |
|
425 |
public String[] getDescribedTablesWithGeometry() { |
426 |
|
427 |
if (cachedGeometryTableNames == null) { |
428 |
|
429 |
Connection dc; |
430 |
try { |
431 |
dc = createNewDatabaseConnection(); |
432 |
try { |
433 |
|
434 |
cachedGeometryTableNames = PGUtil |
435 |
.getColumnsDescribedInGeometryColumnsTable(dc |
436 |
.createStatement()); |
437 |
|
438 |
} catch (SQLException e) { |
439 |
log.error(e, e); |
440 |
} finally { |
441 |
dc.close(); |
442 |
} |
443 |
} catch (SQLException e1) { |
444 |
log.error(e1, e1); |
445 |
return new String[0]; |
446 |
} |
447 |
} |
448 |
return cachedGeometryTableNames; |
449 |
} |
450 |
|
451 |
@Override |
452 |
protected void finalize() throws Throwable { |
453 |
super.finalize(); |
454 |
dispose(); |
455 |
} |
456 |
|
457 |
public void dispose() { |
458 |
try { |
459 |
if (dbc != null && !dbc.isClosed()) { |
460 |
dbc.close(); |
461 |
dbc = null; |
462 |
} |
463 |
} catch (SQLException e) { |
464 |
log.error("Error while disposing the database connection to " |
465 |
+ toString(), e); |
466 |
} |
467 |
} |
468 |
} |