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

Commit

Permalink
add postgresql support
Browse files Browse the repository at this point in the history
  • Loading branch information
awisniew90 committed Sep 12, 2019
1 parent 62b2517 commit 4eab5bb
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 112 deletions.
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 @@ -26,49 +27,80 @@
@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";

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_DEPENDENCY = "org.apache.derby:derby";
public static String DB2_DEPENDENCY = "com.ibm.db2.jcc:db2jcc";
public static String MYSQL_DEPENDENCY = "mysql:mysql-connector-java";
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";
public static String DB2_ARTIFACT_ID = "db2jcc";
public static String MYSQL_GROUP_ID = "mysql";
public static String MYSQL_ARTIFACT_ID = "mysql-connector-java";
public static String POSTGRESQL_GROUP_ID = "org.postgresql";
public static String POSTGRESQL_ARTIFACT_ID = "postgresql";

private static String DERBY_DEFAULT = "org.apache.derby:derby:10.14.2.0";
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_ARTIFACT_ID);
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_CLASS_NAME, DB2_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, DB2_ARTIFACT_ID + "-" + version + ".jar");

} else if (projectDependencies.containsKey(JDBCBoosterConfig.DB2_DEPENDENCY)) {
String db2Version = projectDependencies.get(JDBCBoosterConfig.DB2_DEPENDENCY);
this.dependency = JDBCBoosterConfig.DB2_DEPENDENCY + ":" + db2Version;
this.productName = DB2;
} 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;

} else if (projectDependencies.containsKey(JDBCBoosterConfig.MYSQL_DEPENDENCY)) {
String mysqlVersion = projectDependencies.get(JDBCBoosterConfig.MYSQL_DEPENDENCY);
this.dependency = JDBCBoosterConfig.MYSQL_DEPENDENCY + ":" + mysqlVersion;
this.productName = MYSQL;
driverInfo.put(DRIVER_NAME, MYSQL_ARTIFACT_ID);
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_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_ARTIFACT_ID);
driverInfo.put(DRIVER_CLASS_NAME, DERBY_DRIVER_CLASS_NAME);
driverInfo.put(DRIVER_JAR, DERBY_ARTIFACT_ID + "-" + DERBY_DEFAULT_VERSION + ".jar");
}
}

Expand All @@ -84,23 +116,29 @@ 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 +152,7 @@ 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 @@ -50,9 +50,8 @@
*
*/
public class LibertyServerConfigGenerator {

public static final String CONFIG_DROPINS_DIR = "/configDropins/defaults";


private final String serverPath;
private final String libertyInstallPath;
Expand All @@ -64,13 +63,14 @@ public class LibertyServerConfigGenerator {
private Element featureManager;
private Element serverRoot;
private Element httpEndpoint;

private Document variablesXml;
private Element variablesRoot;

private Set<String> featuresAdded;

public LibertyServerConfigGenerator(String serverPath, String encryptionKey, BoostLoggerI logger) throws ParserConfigurationException {
public LibertyServerConfigGenerator(String serverPath, String encryptionKey, BoostLoggerI logger)
throws ParserConfigurationException {

this.serverPath = serverPath;
this.libertyInstallPath = serverPath + "/../../.."; // Three directories
Expand Down Expand Up @@ -103,7 +103,7 @@ private void generateServerXml() throws ParserConfigurationException {
httpEndpoint.setAttribute("id", DEFAULT_HTTP_ENDPOINT);
serverRoot.appendChild(httpEndpoint);
}

private void generateVariablesXml() throws ParserConfigurationException {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Expand All @@ -113,7 +113,6 @@ private void generateVariablesXml() throws ParserConfigurationException {
variablesRoot.setAttribute("description", "Boost variables");
variablesXml.appendChild(variablesRoot);
}


/**
* Add a Liberty feature to the server configuration
Expand Down Expand Up @@ -153,7 +152,7 @@ public void writeToServer() throws TransformerException, IOException {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

// Replace auto-generated server.xml
DOMSource server = new DOMSource(serverXml);
StreamResult serverResult = new StreamResult(new File(serverPath + "/server.xml"));
Expand All @@ -162,12 +161,12 @@ public void writeToServer() throws TransformerException, IOException {
// Create configDropins/default path
Path configDropins = Paths.get(serverPath + CONFIG_DROPINS_DIR);
Files.createDirectories(configDropins);

// Write variables.xml to configDropins
DOMSource variables = new DOMSource(variablesXml);
StreamResult variablesResult = new StreamResult(new File(serverPath + CONFIG_DROPINS_DIR + "/variables.xml"));
transformer.transform(variables, variablesResult);

}

public void addConfigVariables(Properties properties) throws IOException {
Expand All @@ -188,7 +187,7 @@ private void addConfigVariable(String key, String value) throws IOException {
Map<String, String> propertiesToEncrypt = BoostProperties.getPropertiesToEncrypt();

if (propertiesToEncrypt.containsKey(key) && value != null && !value.equals("")) {
value = encrypt(key,value);
value = encrypt(key, value);
}

Element variable = variablesXml.createElement("variable");
Expand Down Expand Up @@ -248,7 +247,7 @@ public void addHttpsPort(String httpsPort) throws Exception {
public Document getServerXmlDoc() {
return serverXml;
}

private String getSecurityUtilCmd(String libertyInstallPath) {
if (OSUtil.isWindows()) {
return libertyInstallPath + "/bin/securityUtility.bat";
Expand All @@ -259,7 +258,7 @@ private String getSecurityUtilCmd(String libertyInstallPath) {

private String encrypt(String propertyKey, String propertyValue) throws IOException {

//Won't encode the property if it contains the aes flag
// Won't encode the property if it contains the aes flag
if (!isEncoded(propertyValue)) {
Runtime rt = Runtime.getRuntime();
List<String> commands = new ArrayList<String>();
Expand All @@ -270,14 +269,14 @@ private String encrypt(String propertyKey, String propertyValue) throws IOExcept

// Get the internal encryption type set for this property
String encryptionType = BoostProperties.getPropertiesToEncrypt().get(propertyKey);
if(encryptionType != null && !encryptionType.equals("")) {
if (encryptionType != null && !encryptionType.equals("")) {
commands.add("--encoding=" + encryptionType);
} else {
commands.add("--encoding=aes");
}

// Set the defined encryption key
if(encryptionKey != null && !encryptionKey.equals("")) {
if (encryptionKey != null && !encryptionKey.equals("")) {
commands.add("--key=" + encryptionKey);
}

Expand Down Expand Up @@ -311,28 +310,27 @@ private String encrypt(String propertyKey, String propertyValue) throws IOExcept
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_ARTIFACT_ID)) {
datasourcePropertiesElement = PROPERTIES_DERBY_EMBEDDED;
} else if (productName.equals(JDBCBoosterConfig.DB2)) {
driverJar = DB2_JAR;
} else if (driverName.equals(JDBCBoosterConfig.DB2_ARTIFACT_ID)) {
datasourcePropertiesElement = PROPERTIES_DB2_JCC;
} else if (productName.equals(JDBCBoosterConfig.MYSQL)) {
driverJar = MYSQL_JAR;
} else if (driverName.equals(JDBCBoosterConfig.MYSQL_ARTIFACT_ID)) {
datasourcePropertiesElement = PROPERTIES;
} else if (driverName.equals(JDBCBoosterConfig.POSTGRESQL_ARTIFACT_ID)) {
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 @@ -20,7 +20,6 @@
import boost.runtimes.openliberty.LibertyServerConfigGenerator;
import boost.runtimes.openliberty.boosters.LibertyBoosterI;


public class LibertyJDBCBoosterConfig extends JDBCBoosterConfig implements LibertyBoosterI {

public LibertyJDBCBoosterConfig(BoosterConfigParams params, BoostLoggerI logger) throws BoostException {
Expand All @@ -47,7 +46,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
Loading

0 comments on commit 4eab5bb

Please sign in to comment.