Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Refactor datasource property defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
awisniew90 committed Sep 12, 2019
1 parent 4eab5bb commit 9b20996
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
@BoosterCoordinates(AbstractBoosterConfig.BOOSTERS_GROUP_ID + ":jdbc")
public class JDBCBoosterConfig extends AbstractBoosterConfig {

public static final String DB2_DEFAULT_PORT_NUMBER = "50000";
public static final String MYSQL_DEFAULT_PORT_NUMBER = "3306";
public static final String POSTGRESQL_DEFAULT_PORT_NUMBER = "5432";

public static String DERBY_DRIVER_CLASS_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
public static String DB2_DRIVER_CLASS_NAME = "com.ibm.db2.jcc.DB2Driver";
public static String MYSQL_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
public static String POSTGRESQL_DRIVER_CLASS_NAME = "org.postgresql.Driver";

public static String DERBY_DRIVER_NAME = "derby";
public static String DB2_DRIVER_NAME = "db2";
public static String MYSQL_DRIVER_NAME = "mysql";
public static String POSTGRESQL_DRIVER_NAME = "postgresql";

public static String DERBY_GROUP_ID = "org.apache.derby";
public static String DERBY_ARTIFACT_ID = "derby";
public static String DB2_GROUP_ID = "com.ibm.db2.jcc";
Expand All @@ -51,13 +52,16 @@ public class JDBCBoosterConfig extends AbstractBoosterConfig {
public static String DRIVER_NAME = "driverName";
public static String DRIVER_JAR = "driverJar";

BoostLoggerI logger;
protected Properties boostConfigProperties;
private String dependency;
private Map<String, String> driverInfo;

public JDBCBoosterConfig(BoosterConfigParams params, BoostLoggerI logger) throws BoostException {
super(params.getProjectDependencies().get(getCoordinates(JDBCBoosterConfig.class)));

this.logger = logger;

Map<String, String> projectDependencies = params.getProjectDependencies();
this.boostConfigProperties = params.getBoostProperties();

Expand All @@ -67,38 +71,38 @@ public JDBCBoosterConfig(BoosterConfigParams params, BoostLoggerI logger) throws
String version = projectDependencies.get(DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID);
dependency = DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID + ":" + version;

driverInfo.put(DRIVER_NAME, DERBY_ARTIFACT_ID);
driverInfo.put(DRIVER_NAME, DERBY_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, DERBY_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, DERBY_ARTIFACT_ID + "-" + version + ".jar");

} else if (projectDependencies.containsKey(DB2_GROUP_ID + ":" + DB2_ARTIFACT_ID)) {
String version = projectDependencies.get(DB2_GROUP_ID + ":" + DB2_ARTIFACT_ID);
dependency = DB2_GROUP_ID + ":" + DB2_ARTIFACT_ID + ":" + version;

driverInfo.put(DRIVER_NAME, DB2_ARTIFACT_ID);
driverInfo.put(DRIVER_NAME, DB2_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, DB2_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, DB2_ARTIFACT_ID + "-" + version + ".jar");

} else if (projectDependencies.containsKey(MYSQL_GROUP_ID + ":" + MYSQL_ARTIFACT_ID)) {
String version = projectDependencies.get(MYSQL_GROUP_ID + ":" + MYSQL_ARTIFACT_ID);
dependency = MYSQL_GROUP_ID + ":" + MYSQL_ARTIFACT_ID + ":" + version;

driverInfo.put(DRIVER_NAME, MYSQL_ARTIFACT_ID);
driverInfo.put(DRIVER_NAME, MYSQL_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, MYSQL_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, MYSQL_ARTIFACT_ID + "-" + version + ".jar");

} else if (projectDependencies.containsKey(POSTGRESQL_GROUP_ID + ":" + POSTGRESQL_ARTIFACT_ID)) {
String version = projectDependencies.get(POSTGRESQL_GROUP_ID + ":" + POSTGRESQL_ARTIFACT_ID);
dependency = POSTGRESQL_GROUP_ID + ":" + POSTGRESQL_ARTIFACT_ID + ":" + version;

driverInfo.put(DRIVER_NAME, POSTGRESQL_ARTIFACT_ID);
driverInfo.put(DRIVER_NAME, POSTGRESQL_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, POSTGRESQL_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, POSTGRESQL_ARTIFACT_ID + "-" + version + ".jar");

} else {
dependency = DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID + ":" + DERBY_DEFAULT_VERSION;

driverInfo.put(DRIVER_NAME, DERBY_ARTIFACT_ID);
driverInfo.put(DRIVER_NAME, DERBY_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, DERBY_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, DERBY_ARTIFACT_ID + "-" + DERBY_DEFAULT_VERSION + ".jar");
}
Expand All @@ -116,29 +120,41 @@ public Properties getDatasourceProperties() {
}
}

// TODO: Are defaults adding any value here? Should we just eliminate
// them?
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_URL)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_DATABASE_NAME)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_SERVER_NAME)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_PORT_NUMBER)) {

// No db connection properties have been specified. Set defaults.
if (dependency.contains(DERBY_GROUP_ID)) {
// Verify correct property configuration
if (dependency.contains(DERBY_GROUP_ID)) {
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_DATABASE_NAME)) {
datasourceProperties.put(BoostProperties.DATASOURCE_DATABASE_NAME, DERBY_DB);
}
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_CREATE_DATABASE)) {
datasourceProperties.put(BoostProperties.DATASOURCE_CREATE_DATABASE, "create");
}
} else {
// Check connection properties
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_URL)) {
// No URL was specified. Check if individual properties were
// set.
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_DATABASE_NAME)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_SERVER_NAME)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_PORT_NUMBER)) {

logger.warn("No DB connection properties were provided for the " + driverInfo.get(DRIVER_NAME)
+ "database. " + " The " + BoostProperties.DATASOURCE_URL
+ " property will need to be set at runtime.");

datasourceProperties.put(BoostProperties.DATASOURCE_URL, "");
}
}

} else if (dependency.contains(DB2_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:db2://localhost:" + DB2_DEFAULT_PORT_NUMBER);
// Check authentication properties
if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_USER)
&& !datasourceProperties.containsKey(BoostProperties.DATASOURCE_PASSWORD)) {

} else if (dependency.contains(MYSQL_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:mysql://localhost:" + MYSQL_DEFAULT_PORT_NUMBER);
logger.warn("No authentication properties were provided for the " + driverInfo.get(DRIVER_NAME)
+ "database. " + " The " + BoostProperties.DATASOURCE_USER + " and "
+ BoostProperties.DATASOURCE_PASSWORD + " properties will need to be set at runtime.");

} else if (dependency.contains(POSTGRESQL_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:postgresql://localhost:" + POSTGRESQL_DEFAULT_PORT_NUMBER);
datasourceProperties.put(BoostProperties.DATASOURCE_USER, "");
datasourceProperties.put(BoostProperties.DATASOURCE_PASSWORD, "");
}
}
return datasourceProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,13 @@ public void addDataSource(Map<String, String> driverInfo, Properties datasourceP
String datasourcePropertiesElement = null;

String driverName = driverInfo.get(JDBCBoosterConfig.DRIVER_NAME);
if (driverName.equals(JDBCBoosterConfig.DERBY_ARTIFACT_ID)) {
if (driverName.equals(JDBCBoosterConfig.DERBY_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_DERBY_EMBEDDED;
} else if (driverName.equals(JDBCBoosterConfig.DB2_ARTIFACT_ID)) {
} else if (driverName.equals(JDBCBoosterConfig.DB2_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_DB2_JCC;
} else if (driverName.equals(JDBCBoosterConfig.MYSQL_ARTIFACT_ID)) {
} else if (driverName.equals(JDBCBoosterConfig.MYSQL_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES;
} else if (driverName.equals(JDBCBoosterConfig.POSTGRESQL_ARTIFACT_ID)) {
} else if (driverName.equals(JDBCBoosterConfig.POSTGRESQL_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_POSTGRESQL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,16 @@ public void testAddDatasource_DB2() throws Exception {
String variablesXml = outputDir.getRoot().getAbsolutePath() + LibertyServerConfigGenerator.CONFIG_DROPINS_DIR
+ "/variables.xml";

String urlFound = ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL);
assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", DB2_URL,
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL));

assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", DB2_URL, urlFound);
// No user set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_USER + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_USER));

// No password set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_PASSWORD + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_PASSWORD));
}

/**
Expand Down Expand Up @@ -318,9 +325,16 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception {
String variablesXml = outputDir.getRoot().getAbsolutePath() + LibertyServerConfigGenerator.CONFIG_DROPINS_DIR
+ "/variables.xml";

String urlFound = ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL);
assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", MYSQL_URL,
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL));

// No user set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_USER + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_USER));

assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", MYSQL_URL, urlFound);
// No password set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_PASSWORD + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_PASSWORD));
}

/**
Expand Down Expand Up @@ -397,10 +411,16 @@ public void testAddJdbcBoosterConfig_PostgreSQL() throws Exception {
String variablesXml = outputDir.getRoot().getAbsolutePath() + LibertyServerConfigGenerator.CONFIG_DROPINS_DIR
+ "/variables.xml";

String urlFound = ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL);

assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", POSTGRESQL_URL,
urlFound);
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL));

// No user set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_USER + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_USER));

// No password set.
assertEquals("The variable set for " + BoostProperties.DATASOURCE_PASSWORD + " is not correct", "",
ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_PASSWORD));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void addDataSource(Map<String, String> driverInfo, Properties boostDbProp
StringBuilder jdbcUrl = new StringBuilder();
jdbcUrl.append("jdbc:" + driverName);

if (driverName.equals(JDBCBoosterConfig.DERBY_ARTIFACT_ID)) {
if (driverName.equals(JDBCBoosterConfig.DERBY_DRIVER_NAME)) {

// Derby's URL is slightly different than MySQL and DB2
String databaseName = (String) boostDbProperties.remove(BoostProperties.DATASOURCE_DATABASE_NAME);
Expand Down

0 comments on commit 9b20996

Please sign in to comment.