Strabon

changeset 1544:3932604c130d

Fixed bug #85 (Modified connection properties do not take effect in Endpoint)
- PROBLEM: StrabonBeanWrapper.init() creates new Strabon connection only if Strabon is null
- SOLUTION:
- Endpoint:StrabonBeanWrapper class:
- new data member flag <strabonConnectionDetailsHaveBeenModified>, used to control init()
- setConnectionDetails() calculates the correct value of <strabonConnectionDetailsHaveBeenModified>
after it validates and sanitizes parameter values. The flag is set only when connection parameters
are modified, for example googlemapskey modification does not set the flag.
- constructor applies business rules by using the group setter setConnectionDetails()
- init() creates new Strabon connection only if the flag <strabonConnectionDetailsHaveBeenModified>
is set. In this case it also closes the existing Strabon instance before creating a new one and
then clears the flag <strabonConnectionDetailsHaveBeenModified>.
-Runtime:StrabonDBEngine enum:
- enumerates the available database engines for Strabon along with default port
- used in StrabonBeanWrapper.setConnectionDetails() validation and
in StrabonBeanWrapper.init() to remove unnecessary checks.
author Theofilos Ioannidis <tioannid@yahoo.com>
date Wed Oct 25 12:56:48 2017 +0300 (2017-10-25)
parents 5945172a87c0
children 88f931e3a407
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Tue Oct 24 10:27:26 2017 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Wed Oct 25 12:56:48 2017 +0300
     1.3 @@ -32,6 +32,7 @@
     1.4  import eu.earthobservatory.runtime.generaldb.InvalidDatasetFormatFault;
     1.5  import eu.earthobservatory.runtime.generaldb.Strabon;
     1.6  import eu.earthobservatory.utils.Format;
     1.7 +import eu.earthobservatory.utils.StrabonDBEngine;
     1.8  
     1.9  public class StrabonBeanWrapper implements org.springframework.beans.factory.DisposableBean {
    1.10  
    1.11 @@ -49,25 +50,17 @@
    1.12      private int maxLimit;
    1.13      private boolean loadFromFile;
    1.14      private String prefixes;
    1.15 -
    1.16      private Strabon strabon = null;
    1.17 -
    1.18      private String gChartString = " ";
    1.19 -
    1.20      private boolean checkForLockTable;
    1.21      private List<StrabonBeanWrapperConfiguration> entries;
    1.22 +    private boolean strabonConnectionDetailsHaveBeenModified = false;
    1.23  
    1.24      public StrabonBeanWrapper(String databaseName, String user, String password,
    1.25              int port, String serverName, boolean checkForLockTable, String dbBackend,
    1.26              String googlemapskey, int maxLimit, boolean loadFromFile, String prefixes, List<List<String>> args) {
    1.27 -        this.serverName = serverName;
    1.28 -        this.port = port;
    1.29 -        this.databaseName = databaseName;
    1.30 -        this.user = user;
    1.31 -        this.password = password;
    1.32 +        setConnectionDetails(databaseName, user, password, String.valueOf(port), serverName, dbBackend, googlemapskey);
    1.33          this.checkForLockTable = checkForLockTable;
    1.34 -        this.dbBackend = dbBackend;
    1.35 -        this.googlemapskey = googlemapskey;
    1.36          this.maxLimit = maxLimit;
    1.37          this.loadFromFile = loadFromFile;
    1.38          this.prefixes = prefixes;
    1.39 @@ -78,7 +71,6 @@
    1.40          while (entryit.hasNext()) {
    1.41              List<String> list = entryit.next();
    1.42              Iterator<String> it = list.iterator();
    1.43 -
    1.44              while (it.hasNext()) {
    1.45                  int items = 0;
    1.46                  //Header:label        
    1.47 @@ -123,33 +115,37 @@
    1.48                      StrabonBeanWrapperConfiguration entry = new StrabonBeanWrapperConfiguration(param3, param1, param4, param2, param5, param6);
    1.49                      this.entries.add(entry);
    1.50                  }
    1.51 -
    1.52              }
    1.53          }
    1.54 -
    1.55          init();
    1.56      }
    1.57  
    1.58      public boolean init() {
    1.59 -        if (this.strabon == null) {
    1.60 +        /* if the connection details have been modified
    1.61 +        ** we should close the current Strabon (if any)
    1.62 +        ** and create a new one
    1.63 +         */
    1.64 +        if (this.strabonConnectionDetailsHaveBeenModified) {
    1.65 +            this.closeConnection();
    1.66              try {
    1.67                  logger.warn("[StrabonEndpoint] Strabon not initialized yet.");
    1.68                  logger.warn("[StrabonEndpoint] Initializing Strabon.");
    1.69                  //logger.info("[StrabonEndpoint] Connection details:\n" + this.getDetails());
    1.70  
    1.71                  // initialize Strabon according to user preference
    1.72 -                if (Common.DBBACKEND_MONETDB.equalsIgnoreCase(dbBackend)) {
    1.73 -                    this.strabon = new eu.earthobservatory.runtime.monetdb.Strabon(databaseName, user, password, port, serverName, checkForLockTable);
    1.74 +                switch (StrabonDBEngine.getStrabonDBEngine(dbBackend)) {
    1.75 +                    case MonetDB : this.strabon = new eu.earthobservatory.runtime.monetdb.Strabon(databaseName, user, password, port, serverName, checkForLockTable);
    1.76 +                                   break;
    1.77 +                    case PostGIS : // use PostGIS as the default database backend
    1.78 +                    default :
    1.79 +                                    this.strabon = new eu.earthobservatory.runtime.postgis.Strabon(databaseName, user, password, port, serverName, checkForLockTable);
    1.80 +                }
    1.81  
    1.82 -                } else {
    1.83 -                    // check whether the user typed wrong database backend and report
    1.84 -                    if (!Common.DBBACKEND_POSTGIS.equalsIgnoreCase(dbBackend)) {
    1.85 -                        logger.warn("[StrabonEndpoint] Unknown database backend \"" + dbBackend + "\". Assuming PostGIS.");
    1.86 -                    }
    1.87 -
    1.88 -                    // use PostGIS as the default database backend
    1.89 -                    this.strabon = new eu.earthobservatory.runtime.postgis.Strabon(databaseName, user, password, port, serverName, checkForLockTable);
    1.90 -                }
    1.91 +                logger.info("[StrabonEndpoint] Created new connection with details:\n" + this.getDetails());
    1.92 +                /* we should clear the <strabonConnectionDetailsHaveBeenModified> flag
    1.93 +                ** since the connection to Strabon has been made
    1.94 +                 */
    1.95 +                this.strabonConnectionDetailsHaveBeenModified = false;
    1.96  
    1.97                  installSIGTERMHandler(this.strabon);
    1.98  
    1.99 @@ -158,7 +154,6 @@
   1.100                  return false;
   1.101              }
   1.102          }
   1.103 -
   1.104          return true;
   1.105      }
   1.106  
   1.107 @@ -199,6 +194,7 @@
   1.108  
   1.109      public void closeConnection() {
   1.110          if (strabon != null) {
   1.111 +            logger.info("[StrabonEndpoint] Closing existing connection with details:\n" + this.getDetails());
   1.112              strabon.close();
   1.113              strabon = null;
   1.114          }
   1.115 @@ -379,17 +375,36 @@
   1.116          return true;
   1.117      }
   1.118  
   1.119 -    public void setConnectionDetails(String dbname, String username, String password, String port, String hostname, String dbengine, String googlemapskey) {
   1.120 -        this.databaseName = dbname;
   1.121 -        this.user = username;
   1.122 +    public void setConnectionDetails(String databaseName, String user, String password, String port, String serverName, String dbBackend, String googlemapskey) {
   1.123 +        /* validate-sanitize certain Strabon connection properties
   1.124 +        ** dbBackend - must be one of the supported Db engines, or set it to PostGIS
   1.125 +        ** port - must be an integer, or set it to dbBackend's default port
   1.126 +         */
   1.127 +        if (StrabonDBEngine.getStrabonDBEngine(dbBackend) == null) {
   1.128 +            logger.warn("[StrabonEndpoint] Unknown database backend \"" + dbBackend + "\". Assuming PostGIS.");
   1.129 +            dbBackend = StrabonDBEngine.PostGIS.getName();
   1.130 +        }
   1.131 +        try {
   1.132 +            int tmp_port = Integer.parseInt(port);
   1.133 +        } catch (NumberFormatException e) {
   1.134 +            port = String.valueOf(StrabonDBEngine.getStrabonDBEngine(this.dbBackend).getDefaultPort());
   1.135 +        }
   1.136 +
   1.137 +        /* set the <strabonConnectionDetailsHaveBeenModified> flag if
   1.138 +        ** any of the Strabon connection properties have changed
   1.139 +         */
   1.140 +        this.strabonConnectionDetailsHaveBeenModified = !(dbBackend.equalsIgnoreCase(this.dbBackend)
   1.141 +                && serverName.equalsIgnoreCase(this.serverName)
   1.142 +                && port.equalsIgnoreCase(String.valueOf(this.port))
   1.143 +                && databaseName.equals(this.databaseName)
   1.144 +                && user.equalsIgnoreCase(this.user)
   1.145 +                && password.equals(this.password));
   1.146 +        this.dbBackend = dbBackend;
   1.147 +        this.serverName = serverName;
   1.148 +        this.port = Integer.parseInt(port);
   1.149 +        this.databaseName = databaseName;
   1.150 +        this.user = user;
   1.151          this.password = password;
   1.152 -        try {
   1.153 -            this.port = Integer.valueOf(port);
   1.154 -        } catch (NumberFormatException e) {
   1.155 -            this.port = 5432;
   1.156 -        }
   1.157 -        this.serverName = hostname;
   1.158 -        this.dbBackend = dbengine;
   1.159          this.googlemapskey = googlemapskey;
   1.160          this.checkForLockTable = true;
   1.161      }
   1.162 @@ -521,7 +536,6 @@
   1.163          return googlemapskey;
   1.164      }
   1.165  
   1.166 -    
   1.167      public String googleMapsAPIScriptSourceForJSP() {
   1.168          /* returns the correct script source string to use in JSP
   1.169             i.e. query.jsp