/[schmitzm]/branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java
ViewVC logotype

Diff of /branches/2.3.x/src/skrueger/geotools/io/DbServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1194 by alfonx, Mon Oct 25 08:50:00 2010 UTC revision 1195 by alfonx, Tue Nov 2 10:33:32 2010 UTC
# Line 2  package skrueger.geotools.io; Line 2  package skrueger.geotools.io;
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;
# Line 20  import schmitzm.swing.ManualInputOption. Line 24  import schmitzm.swing.ManualInputOption.
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
# Line 34  import schmitzm.swing.SelectionInputOpti Line 39  import schmitzm.swing.SelectionInputOpti
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    
# Line 66  public class DbServerSettings extends Se Line 73  public class DbServerSettings extends Se
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           *           *
# Line 169  public class DbServerSettings extends Se Line 178  public class DbServerSettings extends Se
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);
# Line 358  public class DbServerSettings extends Se Line 368  public class DbServerSettings extends Se
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    }

Legend:
Removed from v.1194  
changed lines
  Added in v.1195

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26