2 |
|
|
3 |
import java.awt.Component; |
import java.awt.Component; |
4 |
import java.net.MalformedURLException; |
import java.net.MalformedURLException; |
5 |
|
import java.sql.Connection; |
6 |
|
import java.sql.DriverManager; |
7 |
|
import java.sql.SQLException; |
8 |
import java.util.HashMap; |
import java.util.HashMap; |
9 |
import java.util.regex.Pattern; |
import java.util.regex.Pattern; |
10 |
|
|
11 |
import org.apache.commons.lang.ArrayUtils; |
import org.apache.commons.lang.ArrayUtils; |
12 |
import org.apache.commons.lang.StringUtils; |
import org.apache.commons.lang.StringUtils; |
13 |
|
import org.apache.log4j.Logger; |
14 |
import org.geotools.data.postgis.PostgisNGDataStoreFactory; |
import org.geotools.data.postgis.PostgisNGDataStoreFactory; |
15 |
import org.geotools.jdbc.JDBCDataStore; |
import org.geotools.jdbc.JDBCDataStore; |
16 |
import org.geotools.jdbc.JDBCDataStoreFactory; |
import org.geotools.jdbc.JDBCDataStoreFactory; |
24 |
import schmitzm.swing.MultipleOptionPane; |
import schmitzm.swing.MultipleOptionPane; |
25 |
import schmitzm.swing.SelectionInputOption; |
import schmitzm.swing.SelectionInputOption; |
26 |
import schmitzm.swing.SelectionInputOption.Combo; |
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 |
* This class describes all settings needed to connect to a WFS server. This |
39 |
*/ |
*/ |
40 |
public class DbServerSettings extends ServerSettings<Object, Object> { |
public class DbServerSettings extends ServerSettings<Object, Object> { |
41 |
|
|
42 |
|
Logger log = Logger.getLogger(DbServerSettings.class); |
43 |
|
|
44 |
public enum DbType { |
public enum DbType { |
45 |
postgis("postgresql"); |
postgis("postgresql"); |
46 |
|
|
73 |
public enum Key { |
public enum Key { |
74 |
} |
} |
75 |
|
|
76 |
|
private static Connection dbc; |
77 |
|
|
78 |
/** |
/** |
79 |
* Opens a GUI that asks the use define a DB connection. |
* Opens a GUI that asks the use define a DB connection. |
80 |
* |
* |
178 |
} |
} |
179 |
|
|
180 |
private String[] cachedTypeNames = null; |
private String[] cachedTypeNames = null; |
181 |
|
private String[] cachedGeometryTableNames; |
182 |
|
|
183 |
public DbServerSettings() { |
public DbServerSettings() { |
184 |
this(DbType.postgis); |
this(DbType.postgis); |
368 |
|
|
369 |
return s.toString(); |
return s.toString(); |
370 |
} |
} |
371 |
} |
|
372 |
|
/** |
373 |
|
* returns a valid Connection to our postgresql Database. Name of database, |
374 |
|
* username and password must be provided. |
375 |
|
* |
376 |
|
* @param database |
377 |
|
* name of the database |
378 |
|
* @param username |
379 |
|
* a valid username |
380 |
|
* @param password |
381 |
|
* valid password |
382 |
|
* @return database connection |
383 |
|
* @throws SQLException |
384 |
|
* @throws ClassNotFoundException |
385 |
|
*/ |
386 |
|
public Connection getDatabaseConnection() { |
387 |
|
|
388 |
|
try { |
389 |
|
|
390 |
|
if (dbc == null || dbc.isClosed()) { |
391 |
|
dbc = createNewDatabaseConnection(); |
392 |
|
} |
393 |
|
return dbc; |
394 |
|
} catch (Exception e) { |
395 |
|
return null; |
396 |
|
} |
397 |
|
} |
398 |
|
|
399 |
|
private Connection createNewDatabaseConnection() throws SQLException { |
400 |
|
try { |
401 |
|
Class.forName("org.postgresql.Driver"); |
402 |
|
} catch (ClassNotFoundException e) { |
403 |
|
throw new RuntimeException(e); |
404 |
|
} |
405 |
|
String url = "jdbc:postgresql://" + getHost() |
406 |
|
// + (":" + PropertyUtils.getDbPort()) |
407 |
|
+ ("/" + getDatabase()); |
408 |
|
|
409 |
|
String password = getPassword(); |
410 |
|
String username = getUsername(); |
411 |
|
|
412 |
|
// log.debug("DB URL: " + url + " u=" + username + " p=" |
413 |
|
// + password); |
414 |
|
|
415 |
|
return DriverManager.getConnection(url, username, password); |
416 |
|
} |
417 |
|
|
418 |
|
public String[] getDescribedTablesWithGeometry() { |
419 |
|
|
420 |
|
if (cachedGeometryTableNames == null) { |
421 |
|
|
422 |
|
Connection dc; |
423 |
|
try { |
424 |
|
dc = createNewDatabaseConnection(); |
425 |
|
try { |
426 |
|
|
427 |
|
cachedGeometryTableNames = PGUtil |
428 |
|
.getColumnsDescribedInGeometryColumnsTable(dc |
429 |
|
.createStatement()); |
430 |
|
|
431 |
|
} catch (SQLException e) { |
432 |
|
log.error(e, e); |
433 |
|
} finally { |
434 |
|
dc.close(); |
435 |
|
} |
436 |
|
} catch (SQLException e1) { |
437 |
|
log.error(e1, e1); |
438 |
|
return new String[0]; |
439 |
|
} |
440 |
|
} |
441 |
|
return cachedGeometryTableNames; |
442 |
|
} |
443 |
|
|
444 |
|
@Override |
445 |
|
protected void finalize() throws Throwable { |
446 |
|
super.finalize(); |
447 |
|
dispose(); |
448 |
|
} |
449 |
|
|
450 |
|
public void dispose() { |
451 |
|
try { |
452 |
|
if (dbc != null && !dbc.isClosed()) { |
453 |
|
dbc.close(); |
454 |
|
dbc = null; |
455 |
|
} |
456 |
|
} catch (SQLException e) { |
457 |
|
log.error("Error while disposing the database connection to " |
458 |
|
+ toString(), e); |
459 |
|
} |
460 |
|
} |
461 |
|
} |