public void fillJCDFromDataSource(JdbcConnectionDescriptor jcd, DataSource dataSource, String username, String password) throws MetadataException
    {
        String           realUsername = (jcd.getUserName() != null ? jcd.getUserName() : username);
        String           realPassword = (jcd.getPassWord() != null ? jcd.getPassWord() : password);
        Connection       connection   = null;
        DatabaseMetaData metadata     = null;

        try
        {
            // we have to open a connection to be able to retrieve metadata
            if (realUsername != null)
            {
                connection = dataSource.getConnection(realUsername, realPassword);
            }
            else
            {
                connection = dataSource.getConnection();
            }

            metadata = connection.getMetaData();
        }
        catch (Throwable t)
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (SQLException ex)
                {}
            }
            throw new MetadataException("Could not get the metadata from the given datasource", t);
        }

        try
        {
            HashMap urlComponents = parseConnectionUrl(metadata.getURL());
    
            if (urlComponents.containsKey(PROPERTY_DBALIAS))
            {
                jcd.setProtocol((String)urlComponents.get(PROPERTY_PROTOCOL));
                jcd.setSubProtocol((String)urlComponents.get(PROPERTY_SUBPROTOCOL));
                jcd.setDbAlias((String)urlComponents.get(PROPERTY_DBALIAS));
                if (jdbcSubProtocolToPlatform.containsKey(jcd.getSubProtocol()))
                {
                    // TODO: We might be able to use this: metadata.getDatabaseProductName();
                    jcd.setDbms((String)jdbcSubProtocolToPlatform.get(jcd.getSubProtocol()));
                }
            }
        }
        catch (Throwable t)
        {
            try
            {
                connection.close();
            }
            catch (SQLException ex)
            {}
            throw new MetadataException("Could not get the metadata from the given datasource", t);
        }
        try
        {
            // this will only work with JDK >= 1.4 and only with some jdbc drivers
            Integer majorVersion = (Integer)PropertyUtils.getProperty(metadata, "JDBCMajorVersion");
            Integer minorVersion = (Integer)PropertyUtils.getProperty(metadata, "JDBCMinorVersion");

            jcd.setJdbcLevel(Double.parseDouble(majorVersion.toString()+"."+minorVersion.toString()));
        }
        catch (Throwable t)
        {
            // otherwise we're assuming JDBC 2.0 compliance
            jcd.setJdbcLevel(2.0);
        }
        try
        {
            connection.close();
        }
        catch (SQLException ex)
        {}
    }
public void fillJCDFromDataSource ( JdbcConnectionDescriptor jcd , DataSource dataSource , String username , String password ) throws MetadataException { String realUsername = ( jcd . getUserName ( ) != null ? jcd . getUserName ( ) : username ) ; String realPassword = ( jcd . getPassWord ( ) != null ? jcd . getPassWord ( ) : password ) ; Connection connection = null ; DatabaseMetaData metadata = null ; try { if ( realUsername != null ) { connection = dataSource . getConnection ( realUsername , realPassword ) ; } else { connection = dataSource . getConnection ( ) ; } metadata = connection . getMetaData ( ) ; } catch ( Throwable t ) { if ( connection != null ) { try { connection . close ( ) ; } catch ( SQLException ex ) { } } throw new MetadataException ( "Could not get the metadata from the given datasource" , t ) ; } try { HashMap urlComponents = parseConnectionUrl ( metadata . getURL ( ) ) ; if ( urlComponents . containsKey ( PROPERTY_DBALIAS ) ) { jcd . setProtocol ( ( String ) urlComponents . get ( PROPERTY_PROTOCOL ) ) ; jcd . setSubProtocol ( ( String ) urlComponents . get ( PROPERTY_SUBPROTOCOL ) ) ; jcd . setDbAlias ( ( String ) urlComponents . get ( PROPERTY_DBALIAS ) ) ; if ( jdbcSubProtocolToPlatform . containsKey ( jcd . getSubProtocol ( ) ) ) { jcd . setDbms ( ( String ) jdbcSubProtocolToPlatform . get ( jcd . getSubProtocol ( ) ) ) ; } } } catch ( Throwable t ) { try { connection . close ( ) ; } catch ( SQLException ex ) { } throw new MetadataException ( "Could not get the metadata from the given datasource" , t ) ; } try { Integer majorVersion = ( Integer ) PropertyUtils . getProperty ( metadata , "JDBCMajorVersion" ) ; Integer minorVersion = ( Integer ) PropertyUtils . getProperty ( metadata , "JDBCMinorVersion" ) ; jcd . setJdbcLevel ( Double . parseDouble ( majorVersion . toString ( ) + "." + minorVersion . toString ( ) ) ) ; } catch ( Throwable t ) { jcd . setJdbcLevel ( 2.0 ) ; } try { connection . close ( ) ; } catch ( SQLException ex ) { } }
Fills parameters of the given {@link JdbcConnectionDescriptor} with metadata
extracted from the given datasource.

@param jcd        The jdbc connection descriptor to fill
@param dataSource The data source
@param username   The username required to establish a connection via the data source
Can be empty if the data source does not require it or if one
is specified in the jdbc connection descriptor
@param password   The username required to establish a connection via the data source
Can be empty if the data source or username does not require it or if one
is specified in the jdbc connection descriptor