Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static boost.common.config.ConfigConstants.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -25,50 +26,88 @@

@BoosterCoordinates(AbstractBoosterConfig.BOOSTERS_GROUP_ID + ":jdbc")
public class JDBCBoosterConfig extends AbstractBoosterConfig {

public static String DERBY = "derby";
public static String DB2 = "db2";
public static String MYSQL = "mysql";


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";

//Assumption in TomEE is that "driverName" = part of URL prefix as in "jdbc:<prefix>:..." for DataSource URL
// Maybe a better name than "driverName would be URL prefix portion or something.

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 DERBY_DEPENDENCY = "org.apache.derby:derby";
public static String DB2_DEPENDENCY = "com.ibm.db2.jcc:db2jcc";
public static String MYSQL_DEPENDENCY = "mysql:mysql-connector-java";

private static String DERBY_DEFAULT = "org.apache.derby:derby:10.14.2.0";
public static String POSTGRESQL_DRIVER_CLASS_NAME = "org.postgresql.Driver";

public static String DERBY_GROUP_ID = "org.apache.derby";
public static String DERBY_ARTIFACT_ID = "derby";
public static String DERBY_DRIVER_NAME = "derby";
public static String DB2_GROUP_ID = "com.ibm.db2.jcc";
public static String DB2_ARTIFACT_ID = "db2jcc";
public static String DB2_DRIVER_NAME = "db2";
public static String MYSQL_GROUP_ID = "mysql";
public static String MYSQL_ARTIFACT_ID = "mysql-connector-java";
public static String MYSQL_DRIVER_NAME = "mysql";
public static String POSTGRESQL_GROUP_ID = "org.postgresql";
public static String POSTGRESQL_ARTIFACT_ID = "postgresql";
public static String POSTGRESQL_DRIVER_NAME = "postgresql";

public static String DERBY_DEFAULT_VERSION = "10.14.2.0";

public static String DRIVER_CLASS_NAME = "driverClassName";
public static String DRIVER_NAME = "driverName";
public static String DRIVER_JAR = "driverJar";

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

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

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

// Determine JDBC driver dependency
if (projectDependencies.containsKey(JDBCBoosterConfig.DERBY_DEPENDENCY)) {
String derbyVersion = projectDependencies.get(JDBCBoosterConfig.DERBY_DEPENDENCY);
this.dependency = JDBCBoosterConfig.DERBY_DEPENDENCY + ":" + derbyVersion;
this.productName = DERBY;
driverInfo = new HashMap<String, String>();

if (projectDependencies.containsKey(DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID)) {
String version = projectDependencies.get(DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID);
dependency = DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID + ":" + version;

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;

} else if (projectDependencies.containsKey(JDBCBoosterConfig.DB2_DEPENDENCY)) {
String db2Version = projectDependencies.get(JDBCBoosterConfig.DB2_DEPENDENCY);
this.dependency = JDBCBoosterConfig.DB2_DEPENDENCY + ":" + db2Version;
this.productName = DB2;
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(JDBCBoosterConfig.MYSQL_DEPENDENCY)) {
String mysqlVersion = projectDependencies.get(JDBCBoosterConfig.MYSQL_DEPENDENCY);
this.dependency = JDBCBoosterConfig.MYSQL_DEPENDENCY + ":" + mysqlVersion;
this.productName = MYSQL;
} 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_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_DRIVER_NAME);
driverInfo.put(DRIVER_CLASS_NAME, POSTGRESQL_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, POSTGRESQL_ARTIFACT_ID + "-" + version + ".jar");

} else {
this.dependency = DERBY_DEFAULT;
this.productName = DERBY;
dependency = DERBY_GROUP_ID + ":" + DERBY_ARTIFACT_ID + ":" + DERBY_DEFAULT_VERSION;

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 @@ -84,23 +123,28 @@ 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 (productName.equals(DERBY)) {
if (dependency.contains(DERBY_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_DATABASE_NAME, DERBY_DB);
datasourceProperties.put(BoostProperties.DATASOURCE_CREATE_DATABASE, "create");

} else if (productName.equals(DB2)) {
} else if (dependency.contains(DB2_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:db2://localhost:" + DB2_DEFAULT_PORT_NUMBER);

} else if (productName.equals(MYSQL)) {
} else if (dependency.contains(MYSQL_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:mysql://localhost:" + MYSQL_DEFAULT_PORT_NUMBER);

} else if (dependency.contains(POSTGRESQL_GROUP_ID)) {
datasourceProperties.put(BoostProperties.DATASOURCE_URL,
"jdbc:postgresql://localhost:" + POSTGRESQL_DEFAULT_PORT_NUMBER);
}
}
return datasourceProperties;
Expand All @@ -114,7 +158,8 @@ public List<String> getDependencies() {
return deps;
}

public String getProductName() {
return productName;
public Map<String, String> getDriverInfo() {
return driverInfo;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class ConfigConstants {
public static final String FILESET = "fileset";
public static final String PROPERTIES_DERBY_EMBEDDED = "properties.derby.embedded";
public static final String PROPERTIES_DB2_JCC = "properties.db2.jcc";
public static final String PROPERTIES_POSTGRESQL = "properties.postgresql";
public static final String PROPERTIES = "properties";
public static final String CONTAINER_AUTH_DATA_REF = "containerAuthDataRef";
public static final String URL = "url";
Expand All @@ -63,11 +64,6 @@ public final class ConfigConstants {
public static final String JDBC_LIBRARY_1 = "Library1";
public static final String DERBY_DB = "DerbyDB";
public static final String DATASOURCE_AUTH_DATA = "datasourceAuth";
public static final String DERBY_JAR = "derby*.jar";
public static final String DB2_JAR = "db2jcc*.jar";
public static final String MYSQL_JAR = "mysql*.jar";
public static final String DB2_DEFAULT_PORT_NUMBER = "50000";
public static final String MYSQL_DEFAULT_PORT_NUMBER = "3306";

// Authentication configuration element/attribute names
public static final String AUTH_DATA = "authData";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,27 +312,26 @@ public boolean isEncoded(String property) {
return property.contains("{aes}") || property.contains("{hash}") || property.contains("{xor}");
}

public void addDataSource(String productName, Properties datasourceProperties) throws Exception {
String driverJar = null;
public void addDataSource(Map<String, String> driverInfo, Properties datasourceProperties) throws Exception {
String datasourcePropertiesElement = null;

if (productName.equals(JDBCBoosterConfig.DERBY)) {
driverJar = DERBY_JAR;
String driverName = driverInfo.get(JDBCBoosterConfig.DRIVER_NAME);
if (driverName.equals(JDBCBoosterConfig.DERBY_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_DERBY_EMBEDDED;
} else if (productName.equals(JDBCBoosterConfig.DB2)) {
driverJar = DB2_JAR;
} else if (driverName.equals(JDBCBoosterConfig.DB2_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_DB2_JCC;
} else if (productName.equals(JDBCBoosterConfig.MYSQL)) {
driverJar = MYSQL_JAR;
} else if (driverName.equals(JDBCBoosterConfig.MYSQL_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES;
} else if (driverName.equals(JDBCBoosterConfig.POSTGRESQL_DRIVER_NAME)) {
datasourcePropertiesElement = PROPERTIES_POSTGRESQL;
}

// Add library
Element lib = serverXml.createElement(LIBRARY);
lib.setAttribute("id", JDBC_LIBRARY_1);
Element fileLoc = serverXml.createElement(FILESET);
fileLoc.setAttribute("dir", RESOURCES);
fileLoc.setAttribute("includes", driverJar);
fileLoc.setAttribute("includes", driverInfo.get(JDBCBoosterConfig.DRIVER_JAR));
lib.appendChild(fileLoc);
serverRoot.appendChild(lib);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String getFeature() {
@Override
public void addServerConfig(LibertyServerConfigGenerator libertyServerConfigGenerator) throws BoostException {
try {
libertyServerConfigGenerator.addDataSource(getProductName(), getDatasourceProperties());
libertyServerConfigGenerator.addDataSource(getDriverInfo(), getDatasourceProperties());
} catch (Exception e) {
throw new BoostException("Error when configuring JDBC data source.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class LibertyServerConfigGeneratorTest {
BoostLoggerI logger = CommonLogger.getInstance();

private final String MYSQL_URL = "jdbc:mysql://localhost:3306/testdb";
private final String POSTGRESQL_URL = "jdbc:postgresql://localhost:5432/testdb";
private final String DB2_URL = "jdbc:db2://localhost:50000/testdb";

/**
Expand Down Expand Up @@ -116,7 +117,9 @@ public void testAddDatasource_Derby() throws Exception {

Element fileset = getDirectChildrenByTag(library, FILESET).get(0);
assertEquals("Fileset dir attribute is not correct", RESOURCES, fileset.getAttribute("dir"));
assertEquals("Fileset includes attribute is not correct", DERBY_JAR, fileset.getAttribute("includes"));

String derbyJar = JDBCBoosterConfig.DERBY_ARTIFACT_ID + "-" + JDBCBoosterConfig.DERBY_DEFAULT_VERSION + ".jar";
assertEquals("Fileset includes attribute is not correct", derbyJar, fileset.getAttribute("includes"));

// Check that the <dataSource> element is correctly configured
List<Element> dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE);
Expand Down Expand Up @@ -178,7 +181,7 @@ public void testAddDatasource_DB2() throws Exception {
outputDir.getRoot().getAbsolutePath(), null, logger);

Map<String, String> jdbcDependency = BoosterUtil.getJDBCDependency();
jdbcDependency.put(JDBCBoosterConfig.DB2_DEPENDENCY, "the db2 dependency version");
jdbcDependency.put(JDBCBoosterConfig.DB2_GROUP_ID + ":" + JDBCBoosterConfig.DB2_ARTIFACT_ID, "1.0");

Properties boostProperties = new Properties();
boostProperties.put(BoostProperties.DATASOURCE_URL, DB2_URL);
Expand All @@ -205,7 +208,9 @@ public void testAddDatasource_DB2() throws Exception {

Element fileset = getDirectChildrenByTag(library, FILESET).get(0);
assertEquals("Fileset dir attribute is not correct", RESOURCES, fileset.getAttribute("dir"));
assertEquals("Fileset includes attribute is not correct", DB2_JAR, fileset.getAttribute("includes"));

String db2Jar = JDBCBoosterConfig.DB2_ARTIFACT_ID + "-1.0.jar";
assertEquals("Fileset includes attribute is not correct", db2Jar, fileset.getAttribute("includes"));

// Check that the <dataSource> element is correctly configured
List<Element> dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE);
Expand Down Expand Up @@ -257,7 +262,7 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception {
outputDir.getRoot().getAbsolutePath(), null, logger);

Map<String, String> jdbcDependency = BoosterUtil.getJDBCDependency();
jdbcDependency.put(JDBCBoosterConfig.MYSQL_DEPENDENCY, "the mysql dependency version");
jdbcDependency.put(JDBCBoosterConfig.MYSQL_GROUP_ID + ":" + JDBCBoosterConfig.MYSQL_ARTIFACT_ID, "1.0");

Properties boostProperties = new Properties();
boostProperties.put(BoostProperties.DATASOURCE_URL, MYSQL_URL);
Expand All @@ -283,7 +288,9 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception {

Element fileset = getDirectChildrenByTag(library, FILESET).get(0);
assertEquals("Fileset dir attribute is not correct", RESOURCES, fileset.getAttribute("dir"));
assertEquals("Fileset includes attribute is not correct", MYSQL_JAR, fileset.getAttribute("includes"));

String mysqlJar = JDBCBoosterConfig.MYSQL_ARTIFACT_ID + "-1.0.jar";
assertEquals("Fileset includes attribute is not correct", mysqlJar, fileset.getAttribute("includes"));

// Check that the <dataSource> element is correctly configured
List<Element> dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE);
Expand Down Expand Up @@ -319,5 +326,85 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception {
"The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct",
MYSQL_URL, urlFound);
}

/**
* Test that the server.xml and boostrap.properties are fully configured
* with the MySQL datasource
*
* @throws ParserConfigurationException
* @throws TransformerException
* @throws IOException
*/
@Test
public void testAddJdbcBoosterConfig_PostgreSQL() throws Exception {

LibertyServerConfigGenerator serverConfig = new LibertyServerConfigGenerator(
outputDir.getRoot().getAbsolutePath(), null, logger);

Map<String, String> jdbcDependency = BoosterUtil.getJDBCDependency();
jdbcDependency.put(JDBCBoosterConfig.POSTGRESQL_GROUP_ID + ":" + JDBCBoosterConfig.POSTGRESQL_ARTIFACT_ID, "1.0");

Properties boostProperties = new Properties();
boostProperties.put(BoostProperties.DATASOURCE_URL, POSTGRESQL_URL);

BoosterConfigParams params = new BoosterConfigParams(jdbcDependency, boostProperties);
LibertyJDBCBoosterConfig jdbcConfig = new LibertyJDBCBoosterConfig(params, logger);
jdbcConfig.addServerConfig(serverConfig);
serverConfig.writeToServer();

File serverXml = new File(outputDir.getRoot().getAbsolutePath() + "/server.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(serverXml);

Element serverRoot = doc.getDocumentElement();

// Check that the <library> element is correctly configured
List<Element> libraryList = getDirectChildrenByTag(serverRoot, LIBRARY);
assertEquals("Didn't find one and only one library", 1, libraryList.size());

Element library = libraryList.get(0);
assertEquals("Library id is not correct", JDBC_LIBRARY_1, library.getAttribute("id"));

Element fileset = getDirectChildrenByTag(library, FILESET).get(0);
assertEquals("Fileset dir attribute is not correct", RESOURCES, fileset.getAttribute("dir"));

String postgresqlJar = JDBCBoosterConfig.POSTGRESQL_ARTIFACT_ID + "-1.0.jar";
assertEquals("Fileset includes attribute is not correct", postgresqlJar, fileset.getAttribute("includes"));

// Check that the <dataSource> element is correctly configured
List<Element> dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE);
assertEquals("Didn't find one and only one dataSource", 1, dataSourceList.size());

Element dataSource = dataSourceList.get(0);
assertEquals("DataSource id is not correct", DEFAULT_DATASOURCE, dataSource.getAttribute("id"));
assertEquals("DataSource jdbcDriverRef is not correct", JDBC_DRIVER_1,
dataSource.getAttribute(JDBC_DRIVER_REF));

List<Element> propertiesList = getDirectChildrenByTag(dataSource, PROPERTIES_POSTGRESQL);
assertEquals("Didn't find one and only one properties", 1, propertiesList.size());

Element properties = propertiesList.get(0);
assertEquals("The url attribute is not correct", BoostUtil.makeVariable(BoostProperties.DATASOURCE_URL),
properties.getAttribute(URL));

// Check that the <jdbcDriver> element is correctly configured
List<Element> jdbcDriverList = getDirectChildrenByTag(serverRoot, JDBC_DRIVER);
assertEquals("Didn't find one and only one jdbcDriver", 1, jdbcDriverList.size());

Element jdbcDriver = jdbcDriverList.get(0);
assertEquals("JdbcDriver id is not correct", JDBC_DRIVER_1, jdbcDriver.getAttribute("id"));
assertEquals("JdbcDriver libraryRef is not correct", JDBC_LIBRARY_1, jdbcDriver.getAttribute(LIBRARY_REF));

// Check variables.xml content
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);
}

}
Loading