Author: tomdz Date: Sat Oct 29 11:15:55 2005 New Revision: 329463 URL: http://svn.apache.org/viewcvs?rev=329463&view=rev Log: Added ability to specify username & password when creating a platform instance for a given data source (fixes DDLUTILS-34)
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java?rev=329463&r1=329462&r2=329463&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Sat Oct 29 11:15:55 2005 @@ -66,7 +66,7 @@ * @return The model reader */ public JdbcModelReader getModelReader(); - + /** * Returns the data source that this platform uses to access the database. * @@ -80,6 +80,34 @@ * @param dataSource The data source */ public void setDataSource(DataSource dataSource); + + /** + * Returns the username that this platform shall use to access the database. + * + * @return The username + */ + public String getUsername(); + + /** + * Sets the username that this platform shall use to access the database. + * + * @param username The username + */ + public void setUsername(String username); + + /** + * Returns the password that this platform shall use to access the database. + * + * @return The password + */ + public String getPassword(); + + /** + * Sets the password that this platform shall use to access the database. + * + * @param password The password + */ + public void setPassword(String password); /** * Returns a (new) JDBC connection from the data source. Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java?rev=329463&r1=329462&r2=329463&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java Sat Oct 29 11:15:55 2005 @@ -90,7 +90,8 @@ /** * Creates a new platform for the specified database. This is a shortcut method that uses * [EMAIL PROTECTED] PlatformUtils#determineDatabaseType(String, String)} to determine the parameter - * for [EMAIL PROTECTED] #createNewPlatformInstance(String)}. + * for [EMAIL PROTECTED] #createNewPlatformInstance(String)}. Note that no database connection is + * established when using this method. * * @param jdbcDriver The jdbc driver * @param jdbcConnectionUrl The connection url @@ -115,6 +116,27 @@ Platform platform = createNewPlatformInstance(new PlatformUtils().determineDatabaseType(dataSource)); platform.setDataSource(dataSource); + return platform; + } + + /** + * Creates a new platform for the specified database. This is a shortcut method that uses + * [EMAIL PROTECTED] PlatformUtils#determineDatabaseType(DataSource)} to determine the parameter + * for [EMAIL PROTECTED] #createNewPlatformInstance(String)}. Note that this method sets the data source + * at the returned platform instance (method [EMAIL PROTECTED] Platform#setDataSource(DataSource)}). + * + * @param dataSource The data source for the database + * @param username The user name to use for connecting to the database + * @param password The password to use for connecting to the database + * @return The platform or <code>null</code> if the database is not supported + */ + public static synchronized Platform createNewPlatformInstance(DataSource dataSource, String username, String password) throws DdlUtilsException + { + Platform platform = createNewPlatformInstance(new PlatformUtils().determineDatabaseType(dataSource, username, password)); + + platform.setDataSource(dataSource); + platform.setUsername(username); + platform.setPassword(password); return platform; } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java?rev=329463&r1=329462&r2=329463&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java Sat Oct 29 11:15:55 2005 @@ -218,11 +218,32 @@ */ public String determineDatabaseType(DataSource dataSource) throws DynaSqlException { + return determineDatabaseType(dataSource, null, null); + } + + /** + * Tries to determine the database type for the given data source. Note that this will establish + * a connection to the database. + * + * @param dataSource The data source + * @param username The user name to use for connecting to the database + * @param password The password to use for connecting to the database + * @return The database type or <code>null</code> if the database type couldn't be determined + */ + public String determineDatabaseType(DataSource dataSource, String username, String password) throws DynaSqlException + { Connection connection = null; try { - connection = dataSource.getConnection(); + if (username != null) + { + connection = dataSource.getConnection(username, password); + } + else + { + connection = dataSource.getConnection(); + } DatabaseMetaData metaData = connection.getMetaData(); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java?rev=329463&r1=329462&r2=329463&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java Sat Oct 29 11:15:55 2005 @@ -42,48 +42,75 @@ private final Log _log = LogFactory.getLog(JdbcSupport.class); /** The data source. */ private DataSource _dataSource; + /** The username for accessing the database. */ + private String _username; + /** The password for accessing the database. */ + private String _password; /** The names of the currently borrowed connections (for debugging). */ private HashSet _openConnectionNames = new HashSet(); + // Properties + //------------------------------------------------------------------------- + /** - * Creates a new instance without a data source. + * Returns the data source used for communicating with the database. + * + * @return The data source */ - public JdbcSupport() + public DataSource getDataSource() { + return _dataSource; } /** - * Creates a new instance that uses the given data source for talking to - * the database. + * Sets the DataSource used for communicating with the database. * * @param dataSource The data source */ - public JdbcSupport(DataSource dataSource) + public void setDataSource(DataSource dataSource) { _dataSource = dataSource; } - // Properties - //------------------------------------------------------------------------- - + /** - * Returns the data source used for communicating with the database. + * Returns the username used to access the database. * - * @return The data source + * @return The username */ - public DataSource getDataSource() + public String getUsername() { - return _dataSource; + return _username; } /** - * Sets the DataSource used for communicating with the database. + * Sets the username to be used to access the database. * - * @param dataSource The data source + * @param username The username */ - public void setDataSource(DataSource dataSource) + public void setUsername(String username) { - _dataSource = dataSource; + _username = username; + } + + /** + * Returns the password used to access the database. + * + * @return The password + */ + public String getPassword() + { + return _password; + } + + /** + * Sets the password to be used to access the database. + * + * @param password The password + */ + public void setPassword(String password) + { + _password = password; } // Implementation methods @@ -98,8 +125,16 @@ { try { - Connection connection = getDataSource().getConnection(); + Connection connection = null; + if (_username == null) + { + connection = getDataSource().getConnection(); + } + else + { + connection = getDataSource().getConnection(_username, _password); + } if (_log.isDebugEnabled()) { String connName = connection.toString();