Skip to content

Commit

Permalink
Make JDBCCOnfiguration.validateConfiguration public to allow checking…
Browse files Browse the repository at this point in the history
… of validation query

This method is used by GWC to check jdbc configuration files when loaded. Making the method public allows it to also be used by the GeoServer form.
  • Loading branch information
jodygarnett committed Oct 28, 2024
1 parent 2ade999 commit 5b73090
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
6 changes: 5 additions & 1 deletion documentation/en/user/source/configuration/diskquotas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ In order to switch from the Berkeley DB to the freeform JDBC sources the :file:`
</gwcQuotaConfiguration>
In this case a separate file, :file:`geowebcache-diskquota-jdbc.xml` will contain the configuration for the chosen database containing the chosen DBMS dialect, at the time of writing the possible values are ``H2``, ``Oracle``, ``PostgreSQL``.
In this case a separate file, :file:`geowebcache-diskquota-jdbc.xml` will contain the configuration for the chosen database containing the chosen DBMS dialect, at the time of writing the possible values are ``HSQL``, ``H2``, ``Oracle``, ``PostgreSQL``.
The connection pool can be either provided locally, in such case a DBCP based connection pool will be instantiated, or provided via JNDI.
The JDNI configuration is as simple as follows:
Expand Down Expand Up @@ -212,6 +212,10 @@ The local connection pool can instead be configured by specifying the following:
</connectionPool>
</gwcJdbcConfiguration>
.. note::
The `validationQuery` parameter is optional. Any supplied value is restricted based on dialect: `H2` requires ``SELECT 1``, and `Oracle` uses ``SELECT 1 FROM DUAL``. Remaining dialects are recommendation to use ``SELECT 1``.
Disk quota schema
-----------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Logger;
import org.apache.commons.lang3.SerializationUtils;
import org.geotools.util.logging.Logging;
import org.geowebcache.GeoWebCacheEnvironment;
import org.geowebcache.GeoWebCacheExtensions;
import org.geowebcache.config.ConfigurationException;
Expand All @@ -34,6 +36,7 @@
* @author Andrea Aime - GeoSolutions
*/
public class JDBCConfiguration implements Serializable {
private static final Logger LOG = Logging.getLogger(JDBCConfiguration.class.getName());

String dialect;

Expand Down Expand Up @@ -73,18 +76,32 @@ public static JDBCConfiguration load(InputStream is) throws ConfigurationExcepti
return conf;
}

private static void validateConfiguration(JDBCConfiguration conf)
throws ConfigurationException {
/**
* Validate configuration, producing a configuration exception for common problems.
*
* <p>This method is used to validate {@link JDBCConfiguration#load(InputStream)} and is
* available to validate user input. Errors for required parameters result in an exception,
* while warnings are logged.
*
* <p>Checks required JDBC driver and jdbc URL parameters are present.
*
* <p>Checks H2 and Oracle validation query if provided.
*
* @param conf configuration
* @throws ConfigurationException for incomplete or inconsistent configuration.
*/
public static void validateConfiguration(JDBCConfiguration conf) throws ConfigurationException {
if (conf.getDialect() == null) {
throw new ConfigurationException(
"A dialect must be provided, possible values are H2, HSQL, Oracle, PostgresSQL");
}

ConnectionPoolConfiguration cp = conf.getConnectionPool();
String dialect = conf.getDialect();
if (conf.getJNDISource() == null
&& cp == null
&& !"H2".equals(conf.getDialect())
&& !"HSQL".equals(conf.getDialect())) {
&& !"H2".equals(dialect)
&& !"HSQL".equals(dialect)) {
throw new ConfigurationException(
"No data source provided, either configure JNDISource or connectionPool");
}
Expand All @@ -96,6 +113,30 @@ private static void validateConfiguration(JDBCConfiguration conf)
if (cp.getUrl() == null) {
throw new ConfigurationException("No JDBC URL provided");
}

String vq = cp.getValidationQuery();
if (vq != null) {
if ("H2".equalsIgnoreCase(dialect)) {
if (!vq.equalsIgnoreCase("SELECT 1")) {
throw new ConfigurationException(
"H2 validation query required to be: SELECT 1");
}
} else if ("Oracle".equalsIgnoreCase(dialect)) {
if (!vq.equalsIgnoreCase("SELECT 1 FROM DUAL")) {
throw new ConfigurationException(
"Oracle validation query required to be: SELECT 1 FROM DUAL");
}
} else {
if (!vq.equalsIgnoreCase("SELECT 1")
&& !vq.equalsIgnoreCase("SELECT 1 FROM DUAL")) {
LOG.config(
dialect
+ " non standard validation query '"
+ vq
+ "', recommended either 'SELECT 1' or 'SELECT 1 FROM DUAL'.");
}
}
}
}
}

Expand Down Expand Up @@ -195,7 +236,7 @@ public String toString() {

/**
* The connection pool configuration, used to build a local connection pool (with DBCP or other
* connection pool library)
* connection pool library).
*
* @author Andrea Aime - GeoSolutions
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.geowebcache.diskquota.jdbc;

import java.util.Properties;
import org.junit.Test;

public class H2QuotaStoreTest extends JDBCQuotaStoreTest {

Expand Down Expand Up @@ -28,4 +29,7 @@ protected Properties createOfflineFixture() {
protected String getFixtureId() {
return "h2";
}

@Test
public void checkConnectionTest() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import org.geowebcache.GeoWebCacheEnvironment;
import org.geowebcache.GeoWebCacheExtensions;
import org.geowebcache.config.ConfigurationException;
import org.geowebcache.diskquota.jdbc.JDBCConfiguration.ConnectionPoolConfiguration;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -130,4 +131,51 @@ public void testRoundTripParametrizedConnectionPool() throws Exception {
JDBCConfiguration config2 = config.load(file2);
Assert.assertEquals(config2, config);
}

@Test
public void testValidationQueryValidation() throws Exception {
JDBCConfiguration config = new JDBCConfiguration();
ConnectionPoolConfiguration cp = new ConnectionPoolConfiguration();

// h2 testing
config.setDialect("H2");
cp.setDriver("org.h2.Driver");
cp.setUrl("jdbc:h2:database");
config.setConnectionPool(cp);

cp.setValidationQuery("select 1");
JDBCConfiguration.validateConfiguration(config);

cp.setValidationQuery("select 1 from DUO");
try {
JDBCConfiguration.validateConfiguration(config);
Assert.fail("select 1 required");
} catch (ConfigurationException expected) {
}

// oracle testing
config.setDialect("Oracle");
cp.setDriver("oracle.jdbc.driver.OracleDriver");
cp.setUrl("jdbc:oracle:thin:@localhost:1521:xe");

cp.setValidationQuery("select 1 from DUAL");
JDBCConfiguration.validateConfiguration(config);

cp.setValidationQuery("select 1");
try {
JDBCConfiguration.validateConfiguration(config);
Assert.fail("select 1 from DUO required");
} catch (ConfigurationException expected) {
}

// postgres
config.setDialect("PostgreSQL");
cp.setDriver("org.postgresql.Driver");
cp.setUrl("jdbc:postgresql:gttest");

cp.setValidationQuery("select 1 from DUAL");
JDBCConfiguration.validateConfiguration(config);
cp.setValidationQuery("select 1");
JDBCConfiguration.validateConfiguration(config);
}
}

0 comments on commit 5b73090

Please sign in to comment.