diff --git a/boost-common/src/main/java/boost/common/boosters/JDBCBoosterConfig.java b/boost-common/src/main/java/boost/common/boosters/JDBCBoosterConfig.java index 4373edc2..913a63cb 100644 --- a/boost-common/src/main/java/boost/common/boosters/JDBCBoosterConfig.java +++ b/boost-common/src/main/java/boost/common/boosters/JDBCBoosterConfig.java @@ -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; @@ -26,49 +27,84 @@ @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 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"; + 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"; - 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_DEFAULT_VERSION = "10.14.2.0"; - private static String DERBY_DEFAULT = "org.apache.derby:derby:10.14.2.0"; + public static String DRIVER_CLASS_NAME = "driverClassName"; + public static String DRIVER_NAME = "driverName"; + public static String DRIVER_JAR = "driverJar"; + BoostLoggerI logger; protected Properties boostConfigProperties; private String dependency; - private String productName; + private Map driverInfo; public JDBCBoosterConfig(BoosterConfigParams params, BoostLoggerI logger) throws BoostException { super(params.getProjectDependencies().get(getCoordinates(JDBCBoosterConfig.class))); - + + this.logger = logger; + Map 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(); + + 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; + + 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.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_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"); } } @@ -84,23 +120,41 @@ public Properties getDatasourceProperties() { } } - 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)) { + // 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, ""); + } + } + + // Check authentication properties + if (!datasourceProperties.containsKey(BoostProperties.DATASOURCE_USER) + && !datasourceProperties.containsKey(BoostProperties.DATASOURCE_PASSWORD)) { - } else if (productName.equals(DB2)) { - datasourceProperties.put(BoostProperties.DATASOURCE_URL, - "jdbc:db2://localhost:" + DB2_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 (productName.equals(MYSQL)) { - datasourceProperties.put(BoostProperties.DATASOURCE_URL, - "jdbc:mysql://localhost:" + MYSQL_DEFAULT_PORT_NUMBER); + datasourceProperties.put(BoostProperties.DATASOURCE_USER, ""); + datasourceProperties.put(BoostProperties.DATASOURCE_PASSWORD, ""); } } return datasourceProperties; @@ -114,7 +168,7 @@ public List getDependencies() { return deps; } - public String getProductName() { - return productName; + public Map getDriverInfo() { + return driverInfo; } } diff --git a/boost-common/src/main/java/boost/common/config/ConfigConstants.java b/boost-common/src/main/java/boost/common/config/ConfigConstants.java index 65cce403..099f5ab0 100644 --- a/boost-common/src/main/java/boost/common/config/ConfigConstants.java +++ b/boost-common/src/main/java/boost/common/config/ConfigConstants.java @@ -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"; @@ -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"; diff --git a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/LibertyServerConfigGenerator.java b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/LibertyServerConfigGenerator.java index e4abf253..b72ffa6d 100644 --- a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/LibertyServerConfigGenerator.java +++ b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/LibertyServerConfigGenerator.java @@ -50,9 +50,8 @@ * */ public class LibertyServerConfigGenerator { - + public static final String CONFIG_DROPINS_DIR = "/configDropins/defaults"; - private final String serverPath; private final String libertyInstallPath; @@ -64,13 +63,14 @@ public class LibertyServerConfigGenerator { private Element featureManager; private Element serverRoot; private Element httpEndpoint; - + private Document variablesXml; private Element variablesRoot; private Set 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 @@ -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(); @@ -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 @@ -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")); @@ -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 { @@ -188,7 +187,7 @@ private void addConfigVariable(String key, String value) throws IOException { Map propertiesToEncrypt = BoostProperties.getPropertiesToEncrypt(); if (propertiesToEncrypt.containsKey(key) && value != null && !value.equals("")) { - value = encrypt(key,value); + value = encrypt(key, value); } Element variable = variablesXml.createElement("variable"); @@ -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"; @@ -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 commands = new ArrayList(); @@ -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); } @@ -311,20 +310,19 @@ 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 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 @@ -332,7 +330,7 @@ public void addDataSource(String productName, Properties datasourceProperties) t 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); diff --git a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/boosters/LibertyJDBCBoosterConfig.java b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/boosters/LibertyJDBCBoosterConfig.java index 309f5fbf..d75723ea 100644 --- a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/boosters/LibertyJDBCBoosterConfig.java +++ b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/boost/runtimes/openliberty/boosters/LibertyJDBCBoosterConfig.java @@ -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 { @@ -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); } diff --git a/boost-maven/boost-runtimes/runtime-openliberty/src/test/java/io/openliberty/boost/runtimes/config/LibertyServerConfigGeneratorTest.java b/boost-maven/boost-runtimes/runtime-openliberty/src/test/java/io/openliberty/boost/runtimes/config/LibertyServerConfigGeneratorTest.java index 4ce2da41..96e853ae 100644 --- a/boost-maven/boost-runtimes/runtime-openliberty/src/test/java/io/openliberty/boost/runtimes/config/LibertyServerConfigGeneratorTest.java +++ b/boost-maven/boost-runtimes/runtime-openliberty/src/test/java/io/openliberty/boost/runtimes/config/LibertyServerConfigGeneratorTest.java @@ -45,7 +45,6 @@ import java.util.*; import io.openliberty.boost.runtimes.utils.BoosterUtil; - public class LibertyServerConfigGeneratorTest { @Rule @@ -54,6 +53,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"; /** @@ -116,7 +116,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 element is correctly configured List dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE); @@ -148,19 +150,20 @@ public void testAddDatasource_Derby() throws Exception { 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 variablesXml = outputDir.getRoot().getAbsolutePath() + LibertyServerConfigGenerator.CONFIG_DROPINS_DIR + + "/variables.xml"; String databaseNameFound = ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_DATABASE_NAME); - assertEquals("The variable set for " + BoostProperties.DATASOURCE_DATABASE_NAME - + " is not correct", DERBY_DB, databaseNameFound); + assertEquals("The variable set for " + BoostProperties.DATASOURCE_DATABASE_NAME + " is not correct", DERBY_DB, + databaseNameFound); String createFound = ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_CREATE_DATABASE); - assertEquals("The variable set for " + BoostProperties.DATASOURCE_CREATE_DATABASE - + " is not correct", "create", createFound); + assertEquals("The variable set for " + BoostProperties.DATASOURCE_CREATE_DATABASE + " is not correct", "create", + createFound); } /** @@ -178,11 +181,11 @@ public void testAddDatasource_DB2() throws Exception { outputDir.getRoot().getAbsolutePath(), null, logger); Map 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); - + BoosterConfigParams params = new BoosterConfigParams(jdbcDependency, boostProperties); LibertyJDBCBoosterConfig jdbcConfig = new LibertyJDBCBoosterConfig(params, logger); jdbcConfig.addServerConfig(serverConfig); @@ -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 element is correctly configured List dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE); @@ -232,14 +237,19 @@ public void testAddDatasource_DB2() throws Exception { 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 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)); } /** @@ -257,16 +267,16 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception { outputDir.getRoot().getAbsolutePath(), null, logger); Map 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); - + 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(); @@ -283,7 +293,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 element is correctly configured List dataSourceList = getDirectChildrenByTag(serverRoot, DATASOURCE); @@ -310,14 +322,105 @@ public void testAddJdbcBoosterConfig_MySQL() throws Exception { 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 variablesXml = outputDir.getRoot().getAbsolutePath() + LibertyServerConfigGenerator.CONFIG_DROPINS_DIR + + "/variables.xml"; + + 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)); + + // No password set. + assertEquals("The variable set for " + BoostProperties.DATASOURCE_PASSWORD + " is not correct", "", + ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_PASSWORD)); + } + + /** + * 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 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 element is correctly configured + List 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 element is correctly configured + List 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 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 element is correctly configured + List 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"; + + assertEquals("The variable set for " + BoostProperties.DATASOURCE_URL + " is not correct", POSTGRESQL_URL, + ConfigFileUtils.findVariableInXml(variablesXml, BoostProperties.DATASOURCE_URL)); - String 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)); - 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)); } } diff --git a/boost-maven/boost-runtimes/runtime-tomee/src/main/java/boost/runtimes/tomee/TomeeServerConfigGenerator.java b/boost-maven/boost-runtimes/runtime-tomee/src/main/java/boost/runtimes/tomee/TomeeServerConfigGenerator.java index edae89a3..6154c667 100644 --- a/boost-maven/boost-runtimes/runtime-tomee/src/main/java/boost/runtimes/tomee/TomeeServerConfigGenerator.java +++ b/boost-maven/boost-runtimes/runtime-tomee/src/main/java/boost/runtimes/tomee/TomeeServerConfigGenerator.java @@ -79,7 +79,8 @@ public TomeeServerConfigGenerator(String configPath, BoostLoggerI logger) throws public void addServerConfig(AbstractBoosterConfig boosterConfig) throws Exception { if (boosterConfig instanceof JDBCBoosterConfig) { - addDataSource(((JDBCBoosterConfig)boosterConfig).getProductName(), ((JDBCBoosterConfig)boosterConfig).getDatasourceProperties()); + addDataSource(((JDBCBoosterConfig) boosterConfig).getDriverInfo(), + ((JDBCBoosterConfig) boosterConfig).getDatasourceProperties()); } } @@ -215,7 +216,7 @@ public void addKeystore(Map keystoreProps, Map k // No keystore support yet } - public void addDataSource(String productName, Properties boostDbProperties) throws Exception { + public void addDataSource(Map driverInfo, Properties boostDbProperties) throws Exception { // Read tomee.xml File tomeeXml = new File(configPath + "/" + TOMEE_XML); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); @@ -231,23 +232,15 @@ public void addDataSource(String productName, Properties boostDbProperties) thro tomee.appendChild(resource); // Add driver class name - String driverClassName = ""; - if (productName.equals(JDBCBoosterConfig.DERBY)) { - driverClassName = JDBCBoosterConfig.DERBY_DRIVER_CLASS_NAME; - } else if (productName.equals(JDBCBoosterConfig.DB2)) { - driverClassName = JDBCBoosterConfig.DB2_DRIVER_CLASS_NAME; - } else if (productName.equals(JDBCBoosterConfig.MYSQL)) { - driverClassName = JDBCBoosterConfig.MYSQL_DRIVER_CLASS_NAME; - } - Text jdbcDriverText = doc.createTextNode(JDBC_DRIVER_PROPERTY + " = " + driverClassName + System.lineSeparator()); + Text jdbcDriverText = doc.createTextNode(JDBC_DRIVER_PROPERTY + " = " + + driverInfo.get(JDBCBoosterConfig.DRIVER_CLASS_NAME) + System.lineSeparator()); resource.appendChild(jdbcDriverText); - // Add UserName if set. Remove from list to avoid adding it again below String username = (String) boostDbProperties.remove(BoostProperties.DATASOURCE_USER); if (username != null) { - Text usernameText = doc.createTextNode( - USERNAME_PROPERTY + " = " + BoostUtil.makeVariable(BoostProperties.DATASOURCE_USER) + System.lineSeparator()); + Text usernameText = doc.createTextNode(USERNAME_PROPERTY + " = " + + BoostUtil.makeVariable(BoostProperties.DATASOURCE_USER) + System.lineSeparator()); resource.appendChild(usernameText); addCatalinaProperty(BoostProperties.DATASOURCE_USER, username); @@ -256,11 +249,10 @@ public void addDataSource(String productName, Properties boostDbProperties) thro // Add Password if set. Remove from list to avoid adding it again below String password = (String) boostDbProperties.remove(BoostProperties.DATASOURCE_PASSWORD); if (password != null) { - Text passwordText = doc.createTextNode( - PASSWORD_PROPERTY + " = " + BoostUtil.makeVariable(BoostProperties.DATASOURCE_PASSWORD) + System.lineSeparator()); + Text passwordText = doc.createTextNode(PASSWORD_PROPERTY + " = " + + BoostUtil.makeVariable(BoostProperties.DATASOURCE_PASSWORD) + System.lineSeparator()); resource.appendChild(passwordText); - addCatalinaProperty(BoostProperties.DATASOURCE_PASSWORD, password); } @@ -270,18 +262,19 @@ public void addDataSource(String productName, Properties boostDbProperties) thro // configured. String url = (String) boostDbProperties.remove(BoostProperties.DATASOURCE_URL); if (url != null) { - Text jdbcUrlText = doc - .createTextNode(JDBC_URL_PROPERTY + " = " + BoostUtil.makeVariable(BoostProperties.DATASOURCE_URL) + System.lineSeparator()); + Text jdbcUrlText = doc.createTextNode(JDBC_URL_PROPERTY + " = " + + BoostUtil.makeVariable(BoostProperties.DATASOURCE_URL) + System.lineSeparator()); resource.appendChild(jdbcUrlText); addCatalinaProperty(BoostProperties.DATASOURCE_URL, url); } else { // Build the url + String driverName = driverInfo.get(JDBCBoosterConfig.DRIVER_NAME); StringBuilder jdbcUrl = new StringBuilder(); - jdbcUrl.append("jdbc:" + productName); + jdbcUrl.append("jdbc:" + driverName); - if (productName.equals(JDBCBoosterConfig.DERBY)) { + 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); @@ -313,7 +306,8 @@ public void addDataSource(String productName, Properties boostDbProperties) thro } } - Text jdbcUrlText = doc.createTextNode(JDBC_URL_PROPERTY + " = " + jdbcUrl.toString() + System.lineSeparator()); + Text jdbcUrlText = doc + .createTextNode(JDBC_URL_PROPERTY + " = " + jdbcUrl.toString() + System.lineSeparator()); resource.appendChild(jdbcUrlText); } @@ -329,8 +323,8 @@ public void addDataSource(String productName, Properties boostDbProperties) thro addCatalinaProperty(boostProperty, boostDbProperties.getProperty(boostProperty)); } - Text connectionPropertiesText = doc - .createTextNode(CONNECTION_PROPERTIES_PROPERTY + " = [" + connectionProperties.toString() + "]" + System.lineSeparator()); + Text connectionPropertiesText = doc.createTextNode(CONNECTION_PROPERTIES_PROPERTY + " = [" + + connectionProperties.toString() + "]" + System.lineSeparator()); resource.appendChild(connectionPropertiesText); // Overwrite content