diff --git a/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/AbstractJDBCOperator.java b/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/AbstractJDBCOperator.java index eeacf77..5243ae9 100644 --- a/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/AbstractJDBCOperator.java +++ b/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/AbstractJDBCOperator.java @@ -5,6 +5,7 @@ package com.ibm.streamsx.jdbc; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; @@ -13,6 +14,7 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.logging.Logger; @@ -161,7 +163,7 @@ public void setJdbcPassword(String jdbcPassword){ //Parameter jdbcProperties @Parameter(name = "jdbcProperties", optional = true, - description = "This optional parameter specifies the path name of the file that contains the jdbc connection properties: 'user' and 'password'") + description = "This optional parameter specifies the path name of the file that contains the jdbc connection properties: 'user', 'password' and jdbcUrl") public void setJdbcProperties(String jdbcProperties){ this.jdbcProperties = jdbcProperties; } @@ -335,9 +337,10 @@ public static void checkParametersRuntime(OperatorContextChecker checker) { public static void checkParameters(OperatorContextChecker checker) { // If statement is set as parameter, statementAttr can not be set checker.checkExcludedParameters("statement", "statementAttr"); - // If jdbcProperties is set as parameter, jdbcUser and jdbcPassword can not be set + // If jdbcProperties is set as parameter, jdbcUser, jdbcPassword and jdbcUrl can not be set checker.checkExcludedParameters("jdbcUser", "jdbcProperties"); checker.checkExcludedParameters("jdbcPassword", "jdbcProperties"); + checker.checkExcludedParameters("jdbcUrl", "jdbcProperties"); // If credentials is set as parameter, jdbcUser, jdbcPassword and jdbcUrl can not be set. checker.checkExcludedParameters("jdbcUser", "credentials"); @@ -345,7 +348,7 @@ public static void checkParameters(OperatorContextChecker checker) { checker.checkExcludedParameters("jdbcUrl", "credentials"); checker.checkExcludedParameters("credentials", "jdbcUrl"); - // If credentials is set as parameter, credentials can not be set + // If credentials is set as parameter, jdbcProperties can not be set checker.checkExcludedParameters("jdbcProperties", "credentials"); // check reconnection related parameters @@ -356,8 +359,9 @@ public static void checkParameters(OperatorContextChecker checker) { OperatorContext context = checker.getOperatorContext(); if ((!context.getParameterNames().contains("credentials")) && (!context.getParameterNames().contains("appConfigName")) - && (!context.getParameterNames().contains("jdbcUrl"))) { - checker.setInvalidContext("The parameter 'jdbcUrl' is not defined. It must be set in one of these parameters: 'jdbcUrl' or 'credentials' or via the credentials parameter in an application configuration.", null); + && (!context.getParameterNames().contains("jdbcUrl")) + && (!context.getParameterNames().contains("jdbcProperties"))) { + checker.setInvalidContext("The parameter 'jdbcUrl' is not defined. It must be set in one of these parameters: 'jdbcUrl' or 'credentials' or via the credentials parameter in an application configuration or via properties file.", null); } if ((!context.getParameterNames().contains("credentials")) @@ -548,9 +552,9 @@ protected void processControlPort(StreamingInput stream, Tuple tuple) thr LOGGER.log(LogLevel.ERROR, Messages.getString("JDBC_CLASS_NAME_NOT_EXIST")); } // if jdbcProperties is relative path, convert to absolute path - if (jdbcProperties != null && !jdbcProperties.trim().isEmpty() && !jdbcProperties.startsWith(File.separator)) + if (jdbcProperties != null && !jdbcProperties.trim().isEmpty()) { - jdbcProperties = getOperatorContext().getPE().getApplicationDirectory() + File.separator + jdbcProperties; + getProperties(jdbcProperties); } if (credentials != null && !credentials.trim().isEmpty()) @@ -562,8 +566,6 @@ protected void processControlPort(StreamingInput stream, Tuple tuple) thr if (jdbcUrl == null || jdbcUrl.trim().isEmpty()){ LOGGER.log(LogLevel.ERROR, Messages.getString("JDBC_URL_NOT_EXIST")); } - - // System.out.println("credentials : " + credentials); // Roll back the transaction jdbcClientHelper.rollbackWithClearBatch(); @@ -681,9 +683,9 @@ private synchronized void setupJDBCConnection() throws Exception{ TRACE.log(TraceLevel.DEBUG, "Create JDBC Connection, jdbcUrl: " + jdbcUrl); try{ // if jdbcProperties is relative path, convert to absolute path - if (jdbcProperties != null && !jdbcProperties.trim().isEmpty() && !jdbcProperties.startsWith(File.separator)) + if (jdbcProperties != null && !jdbcProperties.trim().isEmpty()) { - jdbcProperties = getOperatorContext().getPE().getApplicationDirectory() + File.separator + jdbcProperties; + getProperties(jdbcProperties); } if (credentials != null && !credentials.trim().isEmpty()) { @@ -707,8 +709,48 @@ private synchronized void setupJDBCConnection() throws Exception{ } } - - + + // read properties file and set user name, password and jdbcUrl. + public void getProperties(String jdbcProperties) throws IOException { + try { + // if jdbcProperties is relative path, convert to absolute path + if (!jdbcProperties.startsWith(File.separator)) + { + jdbcProperties = getOperatorContext().getPE().getApplicationDirectory() + File.separator + jdbcProperties; + } + + System.out.println("JDBC Properties file from Operator '" + getOperatorContext().getName() + "' : " + jdbcProperties); + + Properties jdbcConnectionProps = new Properties(); + FileInputStream fileInput = new FileInputStream(jdbcProperties); + jdbcConnectionProps.load(fileInput); + fileInput.close(); + jdbcUser = jdbcConnectionProps.getProperty("user"); + if (null == jdbcUser){ + LOGGER.log(LogLevel.ERROR, "'user' is not defined in property file: " + jdbcProperties); + throw new Exception(Messages.getString("'jdbcUser' is required to create JDBC connection.")); + } + jdbcPassword = jdbcConnectionProps.getProperty("password"); + if (null == jdbcPassword){ + LOGGER.log(LogLevel.ERROR, "'password' is not defined in property file: " + jdbcProperties); + throw new Exception(Messages.getString("'jdbcPassword' is required to create JDBC connection.")); + } + // It supports jdbcUrl and jdbcurl + jdbcUrl = jdbcConnectionProps.getProperty("jdbcUrl"); + if (null == jdbcUrl){ + jdbcUrl = jdbcConnectionProps.getProperty("jdbcurl"); + if (null == jdbcUrl){ + LOGGER.log(LogLevel.ERROR, "'jdbcUrl' is not defined in property file: " + jdbcProperties); + throw new Exception(Messages.getString("JDBC_URL_NOT_EXIST")); + } + } + + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // read credentials and set user name, password and jdbcUrl. public void getCredentials(String credentials) throws IOException { String jsonString = credentials; @@ -728,11 +770,16 @@ public void getCredentials(String credentials) throws IOException { } jdbcUrl = (String)obj.get("jdbcurl"); + if (jdbcUrl == null || jdbcUrl.trim().isEmpty()){ + jdbcUrl = (String)obj.get("jdbcUrl"); + } // jdbcUrl is required if (jdbcUrl == null || jdbcUrl.trim().isEmpty()){ LOGGER.log(LogLevel.ERROR, Messages.getString("JDBC_URL_NOT_EXIST")); throw new Exception(Messages.getString("JDBC_URL_NOT_EXIST")); } + System.out.println("jdbcUrl from credentials in Operator '" + getOperatorContext().getName() + "' :" + jdbcUrl); + } catch (Exception ex) { ex.printStackTrace(); } diff --git a/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/JDBCClientHelper.java b/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/JDBCClientHelper.java index d40a112..be8678d 100644 --- a/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/JDBCClientHelper.java +++ b/com.ibm.streamsx.jdbc/impl/java/src/com/ibm/streamsx/jdbc/JDBCClientHelper.java @@ -52,8 +52,6 @@ public class JDBCClientHelper { // the user's password. private String jdbcPassword = null; private boolean sslConnection = false; - // the jdbc properties file. - private String jdbcProperties = null; // the transaction isolation level at which statement runs. private String isolationLevel = IJDBCConstants.TRANSACTION_READ_UNCOMMITTED; // transactions are automatically committed or not. @@ -87,7 +85,6 @@ public JDBCClientHelper(String jdbcClassName, String jdbcUrl, this.jdbcUser = jdbcUser; this.jdbcPassword = jdbcPassword; this.sslConnection = sslConnection; - this.jdbcProperties = jdbcProperties; this.autoCommit = autoCommit; this.isolationLevel = isolationLevel; this.reconnectionPolicy = reconnectionPolicy; @@ -108,33 +105,16 @@ public synchronized void createConnection() throws Exception, SQLException{ //Load class into memory Class.forName(jdbcClassName); - // Load jdbc properties Properties jdbcConnectionProps = new Properties(); - if (jdbcProperties != null){ - FileInputStream fileInput = new FileInputStream(jdbcProperties); - jdbcConnectionProps.load(fileInput); - fileInput.close(); - String user = jdbcConnectionProps.getProperty("user"); - if (null == user){ - LOGGER.log(LogLevel.ERROR, "'user' is not defined in property file: " + jdbcProperties); - return; - } - String password = jdbcConnectionProps.getProperty("password"); - if (null == password){ - LOGGER.log(LogLevel.ERROR, "'password' is not defined in property file: " + jdbcProperties); - return; - } - - } else { - // pick up user and password if they are parameters - if (jdbcUser != null && jdbcPassword != null) { - jdbcConnectionProps.put("user", jdbcUser); - jdbcConnectionProps.put("password", jdbcPassword); - jdbcConnectionProps.put("avatica_user", jdbcUser); - jdbcConnectionProps.put("avatica_password", jdbcPassword); - } + // pick up user and password if they are parameters + if (jdbcUser != null && jdbcPassword != null) { + jdbcConnectionProps.put("user", jdbcUser); + jdbcConnectionProps.put("password", jdbcPassword); + // properties for phoenix jdbc properties. + jdbcConnectionProps.put("avatica_user", jdbcUser); + jdbcConnectionProps.put("avatica_password", jdbcPassword); + } - } // add sslConnection to properties if (sslConnection) @@ -257,7 +237,6 @@ public synchronized void resetConnection(String jdbcClassName, String jdbcUrl, this.jdbcUrl = jdbcUrl; this.jdbcUser = jdbcUser; this.jdbcPassword = jdbcPassword; - this.jdbcProperties = jdbcProperties; // Set JDBC Connection Status as false connected = false; diff --git a/com.ibm.streamsx.jdbc/info.xml b/com.ibm.streamsx.jdbc/info.xml index a3ef694..aaa09df 100644 --- a/com.ibm.streamsx.jdbc/info.xml +++ b/com.ibm.streamsx.jdbc/info.xml @@ -41,8 +41,13 @@ * Insert BLOB bug fixed #82 +++ What is new in version 1.5.0 + + * Add **jdbcUrl** to the jdbc properties. + * Support both **jdbcUrl** and **jdbcurl** in credentials and in properties file. + - 1.4.4 + 1.5.0 4.2.0.0