Skip to content

Commit 77150d2

Browse files
committed
Make JDBCCOnfiguration.validateConfiguration public to allow checking 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.
1 parent 2b9135f commit 77150d2

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

documentation/en/user/source/configuration/diskquotas.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ In order to switch from the Berkeley DB to the freeform JDBC sources the :file:`
180180
181181
</gwcQuotaConfiguration>
182182
183-
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``.
183+
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``.
184184
185185
The connection pool can be either provided locally, in such case a DBCP based connection pool will be instantiated, or provided via JNDI.
186186
The JDNI configuration is as simple as follows:
@@ -212,6 +212,10 @@ The local connection pool can instead be configured by specifying the following:
212212
</connectionPool>
213213
</gwcJdbcConfiguration>
214214
215+
.. note::
216+
217+
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``.
218+
215219
Disk quota schema
216220
-----------------
217221

geowebcache/diskquota/jdbc/src/main/java/org/geowebcache/diskquota/jdbc/JDBCConfiguration.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
2424
import java.io.Serializable;
25+
import java.util.logging.Logger;
2526
import org.apache.commons.lang3.SerializationUtils;
27+
import org.geotools.util.logging.Logging;
2628
import org.geowebcache.GeoWebCacheEnvironment;
2729
import org.geowebcache.GeoWebCacheExtensions;
2830
import org.geowebcache.config.ConfigurationException;
@@ -34,6 +36,7 @@
3436
* @author Andrea Aime - GeoSolutions
3537
*/
3638
public class JDBCConfiguration implements Serializable {
39+
private static final Logger LOG = Logging.getLogger(JDBCConfiguration.class.getName());
3740

3841
String dialect;
3942

@@ -73,18 +76,32 @@ public static JDBCConfiguration load(InputStream is) throws ConfigurationExcepti
7376
return conf;
7477
}
7578

76-
private static void validateConfiguration(JDBCConfiguration conf)
77-
throws ConfigurationException {
79+
/**
80+
* Validate configuration, producing a configuration exception for common problems.
81+
*
82+
* <p>This method is used to validate {@link JDBCConfiguration#load(InputStream)} and is
83+
* available to validate user input. Errors for required parameters result in an exception,
84+
* while warnings are logged.
85+
*
86+
* <p>Checks required JDBC driver and jdbc URL parameters are present.
87+
*
88+
* <p>Checks H2 and Oracle validation query if provided.
89+
*
90+
* @param conf configuration
91+
* @throws ConfigurationException for incomplete or inconsistent configuration.
92+
*/
93+
public static void validateConfiguration(JDBCConfiguration conf) throws ConfigurationException {
7894
if (conf.getDialect() == null) {
7995
throw new ConfigurationException(
8096
"A dialect must be provided, possible values are H2, HSQL, Oracle, PostgresSQL");
8197
}
8298

8399
ConnectionPoolConfiguration cp = conf.getConnectionPool();
100+
String dialect = conf.getDialect();
84101
if (conf.getJNDISource() == null
85102
&& cp == null
86-
&& !"H2".equals(conf.getDialect())
87-
&& !"HSQL".equals(conf.getDialect())) {
103+
&& !"H2".equals(dialect)
104+
&& !"HSQL".equals(dialect)) {
88105
throw new ConfigurationException(
89106
"No data source provided, either configure JNDISource or connectionPool");
90107
}
@@ -96,6 +113,30 @@ private static void validateConfiguration(JDBCConfiguration conf)
96113
if (cp.getUrl() == null) {
97114
throw new ConfigurationException("No JDBC URL provided");
98115
}
116+
117+
String vq = cp.getValidationQuery();
118+
if (vq != null) {
119+
if ("H2".equalsIgnoreCase(dialect)) {
120+
if (!vq.equalsIgnoreCase("SELECT 1")) {
121+
throw new ConfigurationException(
122+
"H2 validation query required to be: SELECT 1");
123+
}
124+
} else if ("Oracle".equalsIgnoreCase(dialect)) {
125+
if (!vq.equalsIgnoreCase("SELECT 1 FROM DUAL")) {
126+
throw new ConfigurationException(
127+
"Oracle validation query required to be: SELECT 1 FROM DUAL");
128+
}
129+
} else {
130+
if (!vq.equalsIgnoreCase("SELECT 1")
131+
&& !vq.equalsIgnoreCase("SELECT 1 FROM DUAL")) {
132+
LOG.config(
133+
dialect
134+
+ " non standard validation query '"
135+
+ vq
136+
+ "', recommended either 'SELECT 1' or 'SELECT 1 FROM DUAL'.");
137+
}
138+
}
139+
}
99140
}
100141
}
101142

@@ -195,7 +236,7 @@ public String toString() {
195236

196237
/**
197238
* The connection pool configuration, used to build a local connection pool (with DBCP or other
198-
* connection pool library)
239+
* connection pool library).
199240
*
200241
* @author Andrea Aime - GeoSolutions
201242
*/

geowebcache/diskquota/jdbc/src/test/java/org/geowebcache/diskquota/jdbc/H2QuotaStoreTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.geowebcache.diskquota.jdbc;
22

33
import java.util.Properties;
4+
import org.junit.Test;
45

56
public class H2QuotaStoreTest extends JDBCQuotaStoreTest {
67

@@ -28,4 +29,7 @@ protected Properties createOfflineFixture() {
2829
protected String getFixtureId() {
2930
return "h2";
3031
}
32+
33+
@Test
34+
public void checkConnectionTest() {}
3135
}

geowebcache/diskquota/jdbc/src/test/java/org/geowebcache/diskquota/jdbc/JDBCConfigurationTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import org.geowebcache.GeoWebCacheEnvironment;
1111
import org.geowebcache.GeoWebCacheExtensions;
12+
import org.geowebcache.config.ConfigurationException;
1213
import org.geowebcache.diskquota.jdbc.JDBCConfiguration.ConnectionPoolConfiguration;
1314
import org.junit.After;
1415
import org.junit.Assert;
@@ -130,4 +131,51 @@ public void testRoundTripParametrizedConnectionPool() throws Exception {
130131
JDBCConfiguration config2 = config.load(file2);
131132
Assert.assertEquals(config2, config);
132133
}
134+
135+
@Test
136+
public void testValidationQueryValidation() throws Exception {
137+
JDBCConfiguration config = new JDBCConfiguration();
138+
ConnectionPoolConfiguration cp = new ConnectionPoolConfiguration();
139+
140+
// h2 testing
141+
config.setDialect("H2");
142+
cp.setDriver("org.h2.Driver");
143+
cp.setUrl("jdbc:h2:database");
144+
config.setConnectionPool(cp);
145+
146+
cp.setValidationQuery("select 1");
147+
JDBCConfiguration.validateConfiguration(config);
148+
149+
cp.setValidationQuery("select 1 from DUO");
150+
try {
151+
JDBCConfiguration.validateConfiguration(config);
152+
Assert.fail("select 1 required");
153+
} catch (ConfigurationException expected) {
154+
}
155+
156+
// oracle testing
157+
config.setDialect("Oracle");
158+
cp.setDriver("oracle.jdbc.driver.OracleDriver");
159+
cp.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
160+
161+
cp.setValidationQuery("select 1 from DUAL");
162+
JDBCConfiguration.validateConfiguration(config);
163+
164+
cp.setValidationQuery("select 1");
165+
try {
166+
JDBCConfiguration.validateConfiguration(config);
167+
Assert.fail("select 1 from DUO required");
168+
} catch (ConfigurationException expected) {
169+
}
170+
171+
// postgres
172+
config.setDialect("PostgreSQL");
173+
cp.setDriver("org.postgresql.Driver");
174+
cp.setUrl("jdbc:postgresql:gttest");
175+
176+
cp.setValidationQuery("select 1 from DUAL");
177+
JDBCConfiguration.validateConfiguration(config);
178+
cp.setValidationQuery("select 1");
179+
JDBCConfiguration.validateConfiguration(config);
180+
}
133181
}

0 commit comments

Comments
 (0)