HHH-5618 : Support legacy connection providers

This commit is contained in:
Gail Badner 2010-10-12 15:20:46 -07:00
parent 0b9888e0e5
commit a23796c92d
3 changed files with 57 additions and 6 deletions

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.connection; package org.hibernate.service.jdbc.connections.internal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@ -38,6 +38,7 @@ import com.mchange.v2.c3p0.DataSources;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.util.ReflectHelper; import org.hibernate.util.ReflectHelper;
/** /**
@ -98,7 +99,7 @@ public class C3P0ConnectionProvider implements ConnectionProvider {
public void configure(Properties props) throws HibernateException { public void configure(Properties props) throws HibernateException {
String jdbcDriverClass = props.getProperty( Environment.DRIVER ); String jdbcDriverClass = props.getProperty( Environment.DRIVER );
String jdbcUrl = props.getProperty( Environment.URL ); String jdbcUrl = props.getProperty( Environment.URL );
Properties connectionProps = ConnectionProviderFactory.getConnectionProperties( props ); Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( props );
log.info( "C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl ); log.info( "C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl );
log.info( "Connection properties: " + ConfigurationHelper.maskOut( connectionProps, "password" ) ); log.info( "Connection properties: " + ConfigurationHelper.maskOut( connectionProps, "password" ) );

View File

@ -26,6 +26,7 @@ package org.hibernate.service.jdbc.connections.internal;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -54,13 +55,44 @@ public class ConnectionProviderInitiator implements ServiceInitiator<ConnectionP
private static final Logger log = LoggerFactory.getLogger( ConnectionProviderInitiator.class ); private static final Logger log = LoggerFactory.getLogger( ConnectionProviderInitiator.class );
public static final String C3P0_CONFIG_PREFIX = "hibernate.c3p0"; public static final String C3P0_CONFIG_PREFIX = "hibernate.c3p0";
public static final String C3P0_PROVIDER_CLASS_NAME = "org.hibernate.connection.C3P0ConnectionProvider"; public static final String C3P0_PROVIDER_CLASS_NAME =
"org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider";
public static final String PROXOOL_CONFIG_PREFIX = "hibernate.proxool"; public static final String PROXOOL_CONFIG_PREFIX = "hibernate.proxool";
public static final String PROXOOL_PROVIDER_CLASS_NAME = "org.hibernate.connection.ProxoolConnectionProvider"; public static final String PROXOOL_PROVIDER_CLASS_NAME =
"org.hibernate.service.jdbc.connections.internal.ProxoolConnectionProvider";
public static final String INJECTION_DATA = "hibernate.connection_provider.injection_data"; public static final String INJECTION_DATA = "hibernate.connection_provider.injection_data";
// mapping from legacy connection provider name to actual
// connection provider that will be used
private static final Map<String,String> LEGACY_CONNECTION_PROVIDER_MAPPING;
static {
LEGACY_CONNECTION_PROVIDER_MAPPING = new HashMap<String,String>( 5 );
LEGACY_CONNECTION_PROVIDER_MAPPING.put(
"org.hibernate.connection.DatasourceConnectionProvider",
DatasourceConnectionProviderImpl.class.getName()
);
LEGACY_CONNECTION_PROVIDER_MAPPING.put(
"org.hibernate.connection.DriverManagerConnectionProvider",
DriverManagerConnectionProviderImpl.class.getName()
);
LEGACY_CONNECTION_PROVIDER_MAPPING.put(
"org.hibernate.connection.UserSuppliedConnectionProvider",
UserSuppliedConnectionProviderImpl.class.getName()
);
LEGACY_CONNECTION_PROVIDER_MAPPING.put(
"org.hibernate.connection.C3P0ConnectionProvider",
C3P0_PROVIDER_CLASS_NAME
);
LEGACY_CONNECTION_PROVIDER_MAPPING.put(
"org.hibernate.connection.ProxoolConnectionProvider",
PROXOOL_PROVIDER_CLASS_NAME
);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -75,7 +107,7 @@ public class ConnectionProviderInitiator implements ServiceInitiator<ConnectionP
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class ); final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
ConnectionProvider connectionProvider = null; ConnectionProvider connectionProvider = null;
String providerClassName = (String) configurationValues.get( Environment.CONNECTION_PROVIDER ); String providerClassName = (String) getConfiguredConnectionProviderName( configurationValues );
if ( providerClassName != null ) { if ( providerClassName != null ) {
connectionProvider = instantiateExplicitConnectionProvider( providerClassName, classLoaderService ); connectionProvider = instantiateExplicitConnectionProvider( providerClassName, classLoaderService );
} }
@ -137,6 +169,23 @@ public class ConnectionProviderInitiator implements ServiceInitiator<ConnectionP
return connectionProvider; return connectionProvider;
} }
private String getConfiguredConnectionProviderName( Map configurationValues ) {
String providerClassName = ( String ) configurationValues.get( Environment.CONNECTION_PROVIDER );
if ( LEGACY_CONNECTION_PROVIDER_MAPPING.containsKey( providerClassName ) ) {
String actualProviderClassName = LEGACY_CONNECTION_PROVIDER_MAPPING.get( providerClassName );
if ( log.isWarnEnabled() ) {
StringBuffer buf = new StringBuffer()
.append( providerClassName )
.append( " has been deprecated in favor of ")
.append( actualProviderClassName )
.append( "; that provider will be used instead." );
log.warn( buf.toString() );
}
providerClassName = actualProviderClassName;
}
return providerClassName;
}
private ConnectionProvider instantiateExplicitConnectionProvider( private ConnectionProvider instantiateExplicitConnectionProvider(
String providerClassName, String providerClassName,
ClassLoaderService classLoaderService) { ClassLoaderService classLoaderService) {

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.connection; package org.hibernate.service.jdbc.connections.internal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -31,6 +31,7 @@ import java.util.Properties;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.util.StringHelper; import org.hibernate.util.StringHelper;
import org.hibernate.util.ConfigHelper; import org.hibernate.util.ConfigHelper;