Skip to content

Commit

Permalink
JGRP-2063 JDBC_PING: allow supplying dataSource object directly inste…
Browse files Browse the repository at this point in the history
…ad of String-based properties
  • Loading branch information
rhusar committed Feb 7, 2017
1 parent 45160dd commit a1ee71f
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/org/jgroups/protocols/JDBC_PING.java
Expand Up @@ -41,7 +41,7 @@ public class JDBC_PING extends FILE_PING {
@Property(description = "The JDBC connection username", writable = false) @Property(description = "The JDBC connection username", writable = false)
protected String connection_username; protected String connection_username;


@Property(description = "The JDBC connection password", writable = false,exposeAsManagedAttribute=false) @Property(description = "The JDBC connection password", writable = false, exposeAsManagedAttribute=false)
protected String connection_password; protected String connection_password;


@Property(description = "The JDBC connection driver name", writable = false) @Property(description = "The JDBC connection driver name", writable = false)
Expand Down Expand Up @@ -84,21 +84,29 @@ public class JDBC_PING extends FILE_PING {


/* --------------------------------------------- Fields ------------------------------------------------------ */ /* --------------------------------------------- Fields ------------------------------------------------------ */


protected DataSource dataSourceFromJNDI; protected DataSource dataSource;




@Override protected void createRootDir() { @Override protected void createRootDir() {
; // do *not* create root file system (don't remove !) ; // do *not* create root file system (don't remove !)
} }


public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override @Override
public void init() throws Exception { public void init() throws Exception {
super.init(); super.init();
verifyconfigurationParameters(); verifyConfigurationParameters();
if (stringIsEmpty(datasource_jndi_name)) // If dataSource is already set, skip loading driver or JNDI lookup
loadDriver(); if (dataSource == null) {
else if (stringIsEmpty(datasource_jndi_name)) {
dataSourceFromJNDI = getDataSourceFromJNDI(datasource_jndi_name.trim()); loadDriver();
} else {
dataSource = getDataSourceFromJNDI(datasource_jndi_name.trim());
}
}
attemptSchemaInitialization(); attemptSchemaInitialization();
} }


Expand Down Expand Up @@ -276,12 +284,12 @@ protected void loadDriver() {
Class.forName(connection_driver); Class.forName(connection_driver);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new IllegalArgumentException("JDBC Driver required for JDBC_PING " throw new IllegalArgumentException("JDBC Driver required for JDBC_PING "
+ "protocol could not be loaded: '" + connection_driver + "'"); + " protocol could not be loaded: '" + connection_driver + "'");
} }
} }


protected Connection getConnection() { protected Connection getConnection() {
if (dataSourceFromJNDI == null) { if (dataSource == null) {
Connection connection; Connection connection;
try { try {
connection = DriverManager.getConnection(connection_url, connection_username, connection_password); connection = DriverManager.getConnection(connection_url, connection_username, connection_password);
Expand All @@ -296,7 +304,7 @@ protected Connection getConnection() {
} }
else { else {
try { try {
return dataSourceFromJNDI.getConnection(); return dataSource.getConnection();
} catch (SQLException e) { } catch (SQLException e) {
log.error(Util.getMessage("CouldNotOpenConnectionToDatabase"), e); log.error(Util.getMessage("CouldNotOpenConnectionToDatabase"), e);
return null; return null;
Expand Down Expand Up @@ -394,19 +402,22 @@ protected DataSource getDataSourceFromJNDI(String name) {
} }
} }


protected void verifyconfigurationParameters() { protected void verifyConfigurationParameters() {
if (stringIsEmpty(this.connection_url) || // Skip if datasource is already provided via integration code (e.g. WildFly)
stringIsEmpty(this.connection_driver) || if (dataSource == null) {
stringIsEmpty(this.connection_username) ) { if (stringIsEmpty(this.connection_url) ||
if (stringIsEmpty(this.datasource_jndi_name)) { stringIsEmpty(this.connection_driver) ||
throw new IllegalArgumentException("Either the 4 configuration properties starting with 'connection_' or the datasource_jndi_name must be set"); stringIsEmpty(this.connection_username)) {
if (stringIsEmpty(this.datasource_jndi_name)) {
throw new IllegalArgumentException("Either the 4 configuration properties starting with 'connection_' or the datasource_jndi_name must be set");
}
} }
} if (stringNotEmpty(this.connection_url) ||
if (stringNotEmpty(this.connection_url) || stringNotEmpty(this.connection_driver) ||
stringNotEmpty(this.connection_driver) || stringNotEmpty(this.connection_username)) {
stringNotEmpty(this.connection_username) ) { if (stringNotEmpty(this.datasource_jndi_name)) {
if (stringNotEmpty(this.datasource_jndi_name)) { throw new IllegalArgumentException("When using the 'datasource_jndi_name' configuration property, all properties starting with 'connection_' must not be set");
throw new IllegalArgumentException("When using the 'datasource_jndi_name' configuration property, all properties starting with 'connection_' must not be set"); }
} }
} }
if (stringIsEmpty(this.insert_single_sql)) { if (stringIsEmpty(this.insert_single_sql)) {
Expand All @@ -420,11 +431,11 @@ protected void verifyconfigurationParameters() {
} }
} }


private static final boolean stringIsEmpty(final String value) { private static boolean stringIsEmpty(final String value) {
return value == null || value.trim().isEmpty(); return value == null || value.trim().isEmpty();
} }


private static final boolean stringNotEmpty(final String value) { private static boolean stringNotEmpty(final String value) {
return !stringIsEmpty(value); return !stringIsEmpty(value);
} }


Expand Down

0 comments on commit a1ee71f

Please sign in to comment.