/[schmitzm]/branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java
ViewVC logotype

Diff of /branches/2.4.x/src/skrueger/geotools/io/GtDbServerSettings.java

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

revision 1164 by alfonx, Mon Oct 25 07:25:26 2010 UTC revision 1291 by alfonx, Mon Nov 22 01:18:13 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 javax.management.RuntimeErrorException;
12    
13  import org.apache.commons.lang.ArrayUtils;  import org.apache.commons.lang.ArrayUtils;
14  import org.apache.commons.lang.StringUtils;  import org.apache.commons.lang.StringUtils;
15    import org.apache.log4j.Logger;
16  import org.geotools.data.postgis.PostgisNGDataStoreFactory;  import org.geotools.data.postgis.PostgisNGDataStoreFactory;
17  import org.geotools.jdbc.JDBCDataStore;  import org.geotools.jdbc.JDBCDataStore;
18  import org.geotools.jdbc.JDBCDataStoreFactory;  import org.geotools.jdbc.JDBCDataStoreFactory;
# Line 20  import schmitzm.swing.ManualInputOption. Line 26  import schmitzm.swing.ManualInputOption.
26  import schmitzm.swing.MultipleOptionPane;  import schmitzm.swing.MultipleOptionPane;
27  import schmitzm.swing.SelectionInputOption;  import schmitzm.swing.SelectionInputOption;
28  import schmitzm.swing.SelectionInputOption.Combo;  import schmitzm.swing.SelectionInputOption.Combo;
29    import skrueger.db.PGUtil;
30    
31  /**  /**
32   * 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 41  import schmitzm.swing.SelectionInputOpti
41   */   */
42  public class DbServerSettings extends ServerSettings<Object, Object> {  public class DbServerSettings extends ServerSettings<Object, Object> {
43    
44            Logger log = Logger.getLogger(DbServerSettings.class);
45    
46          public enum DbType {          public enum DbType {
47                  postgis("postgresql");                  postgis("postgresql");
48    
# Line 66  public class DbServerSettings extends Se Line 75  public class DbServerSettings extends Se
75          public enum Key {          public enum Key {
76          }          }
77    
78            private static Connection dbc;
79    
80          /**          /**
81           * Opens a GUI that asks the use define a DB connection.           * Opens a GUI that asks the use define a DB connection.
82           *           *
# Line 139  public class DbServerSettings extends Se Line 150  public class DbServerSettings extends Se
150           *         .properties line. @see #parsePropertiesString           *         .properties line. @see #parsePropertiesString
151           * @throws MalformedURLException           * @throws MalformedURLException
152           */           */
153          public static DbServerSettings parsePropertiesString(String propString)          @Override
154            public DbServerSettings parsePropertiesString(String propString)
155                          throws MalformedURLException {                          throws MalformedURLException {
156    
157                  if (propString == null || propString.isEmpty())                  if (propString == null || propString.isEmpty())
# Line 169  public class DbServerSettings extends Se Line 181  public class DbServerSettings extends Se
181          }          }
182    
183          private String[] cachedTypeNames = null;          private String[] cachedTypeNames = null;
184            private String[] cachedGeometryTableNames;
185    
186          public DbServerSettings() {          public DbServerSettings() {
187                    this(DbType.postgis);
188                    
189          }          }
190    
191          public DbServerSettings(DbType dbType) {          public DbServerSettings(DbType dbType) {
192                  setDbType(dbType);                  setDbType(dbType);
193                  setHost("localhost");                  setHost("localhost");
194                  setSchema("public");                  setSchema("public");
195                    put(JDBCDataStoreFactory.PK_METADATA_TABLE.key, null);
196            }
197    
198            public DbServerSettings(String propertiesString) {
199                    try {
200                            parsePropertiesString(propertiesString);
201                    } catch (MalformedURLException e) {
202                            throw new RuntimeException(e);
203                    }
204          }          }
205    
206          public String[] getCachedTypeNames() {          public String[] getCachedTypeNames() {
# Line 312  public class DbServerSettings extends Se Line 336  public class DbServerSettings extends Se
336    
337                  serialized.append(getTitle());                  serialized.append(getTitle());
338                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
339                    
340                  serialized.append(getHost());                  serialized.append(getHost());
341                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
342    
# Line 356  public class DbServerSettings extends Se Line 380  public class DbServerSettings extends Se
380    
381                  return s.toString();                  return s.toString();
382          }          }
383  }  
384            /**
385             * returns a valid Connection to our postgresql Database. Name of database,
386             * username and password must be provided.
387             *
388             * @param database
389             *            name of the database
390             * @param username
391             *            a valid username
392             * @param password
393             *            valid password
394             * @return database connection
395             * @throws SQLException
396             * @throws ClassNotFoundException
397             */
398            public Connection getDatabaseConnection() {
399    
400                    try {
401    
402                            if (dbc == null || dbc.isClosed()) {
403                                    dbc = createNewDatabaseConnection();
404                            }
405                            return dbc;
406                    } catch (Exception e) {
407                            return null;
408                    }
409            }
410    
411            private Connection createNewDatabaseConnection() throws SQLException {
412                    try {
413                            Class.forName("org.postgresql.Driver");
414                    } catch (ClassNotFoundException e) {
415                            throw new RuntimeException(e);
416                    }
417                    String url = "jdbc:postgresql://" + getHost()
418                    // + (":" + PropertyUtils.getDbPort())
419                                    + ("/" + getDatabase());
420    
421                    String password = getPassword();
422                    String username = getUsername();
423    
424                    // log.debug("DB URL: " + url + " u=" + username + " p="
425                    // + password);
426    
427                    return DriverManager.getConnection(url, username, password);
428            }
429    
430            public String[] getDescribedTablesWithGeometry() {
431    
432                    if (cachedGeometryTableNames == null) {
433    
434                            Connection dc;
435                            try {
436                                    dc = createNewDatabaseConnection();
437                                    try {
438    
439                                            cachedGeometryTableNames = PGUtil
440                                                            .getColumnsDescribedInGeometryColumnsTable(dc
441                                                                            .createStatement());
442    
443                                    } catch (SQLException e) {
444                                            log.error(e, e);
445                                    } finally {
446                                            dc.close();
447                                    }
448                            } catch (SQLException e1) {
449                                    log.error(e1, e1);
450                                    return new String[0];
451                            }
452                    }
453                    return cachedGeometryTableNames;
454            }
455    
456            @Override
457            protected void finalize() throws Throwable {
458                    super.finalize();
459                    dispose();
460            }
461    
462            public void dispose() {
463                    try {
464                            if (dbc != null && !dbc.isClosed()) {
465                                    dbc.close();
466                                    dbc = null;
467                            }
468                    } catch (SQLException e) {
469                            log.error("Error while disposing the database connection to "
470                                            + toString(), e);
471                    }
472            }
473    }

Legend:
Removed from v.1164  
changed lines
  Added in v.1291

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26