FileDocCategorySizeDatePackage
ConnectionProviderFactory.javaAPI DocHibernate 3.2.56331Mon Jul 18 17:37:32 BST 2005org.hibernate.connection

ConnectionProviderFactory

public final class ConnectionProviderFactory extends Object
Instantiates a connection provider given either System properties or a java.util.Properties instance. The ConnectionProviderFactory first attempts to find a name of a ConnectionProvider subclass in the property hibernate.connection.provider_class. If missing, heuristics are used to choose either DriverManagerConnectionProvider, DatasourceConnectionProvider, C3P0ConnectionProvider or DBCPConnectionProvider.
see
ConnectionProvider
author
Gavin King

Fields Summary
private static final Log
log
private static final Set
SPECIAL_PROPERTIES
Constructors Summary
private ConnectionProviderFactory()

 throw new UnsupportedOperationException(); 
Methods Summary
public static java.util.PropertiesgetConnectionProperties(java.util.Properties properties)
Transform JDBC connection properties. Passed in the form hibernate.connection.* to the format accepted by DriverManager by triming the leading "hibernate.connection".


		Iterator iter = properties.keySet().iterator();
		Properties result = new Properties();
		while ( iter.hasNext() ) {
			String prop = (String) iter.next();
			if ( prop.indexOf(Environment.CONNECTION_PREFIX) > -1 && !SPECIAL_PROPERTIES.contains(prop) ) {
				result.setProperty(
					prop.substring( Environment.CONNECTION_PREFIX.length()+1 ),
					properties.getProperty(prop)
				);
			}
		}
		String userName = properties.getProperty(Environment.USER);
		if (userName!=null) result.setProperty( "user", userName );
		return result;
	
public static ConnectionProvidernewConnectionProvider()
Instantiate a ConnectionProvider using System properties.

return
ConnectionProvider
throws
HibernateException


	          	 
	      
		return newConnectionProvider( Environment.getProperties() );
	
public static ConnectionProvidernewConnectionProvider(java.util.Properties properties)
Instantiate a ConnectionProvider using given properties. Method newConnectionProvider.

param
properties hibernate SessionFactory properties
return
ConnectionProvider
throws
HibernateException

		return newConnectionProvider( properties, null );
	
public static ConnectionProvidernewConnectionProvider(java.util.Properties properties, java.util.Map connectionProviderInjectionData)
Instantiate a ConnectionProvider using given properties. Method newConnectionProvider.

param
properties hibernate SessionFactory properties
Param
connectionProviderInjectionData object to be injected in the conenction provided
return
ConnectionProvider
throws
HibernateException

		ConnectionProvider connections;
		String providerClass = properties.getProperty(Environment.CONNECTION_PROVIDER);
		if ( providerClass!=null ) {
			try {
				log.info("Initializing connection provider: " + providerClass);
				connections = (ConnectionProvider) ReflectHelper.classForName(providerClass).newInstance();
			}
			catch (Exception e) {
				log.fatal("Could not instantiate connection provider", e);
				throw new HibernateException("Could not instantiate connection provider: " + providerClass);
			}
		}
		else if ( properties.getProperty(Environment.DATASOURCE)!=null ) {
			connections = new DatasourceConnectionProvider();
		}
		else if ( properties.getProperty(Environment.C3P0_MAX_SIZE)!=null ) {
			connections = new C3P0ConnectionProvider();
		}
		else if (
			properties.getProperty(Environment.PROXOOL_XML)!=null ||
			properties.getProperty(Environment.PROXOOL_PROPERTIES)!=null ||
			properties.getProperty(Environment.PROXOOL_EXISTING_POOL)!=null
		) {
			connections = new ProxoolConnectionProvider();
		}
		else if ( properties.getProperty(Environment.URL)!=null ) {
			connections = new DriverManagerConnectionProvider();
		}
		else {
			connections = new UserSuppliedConnectionProvider();
		}

		if ( connectionProviderInjectionData != null && connectionProviderInjectionData.size() != 0 ) {
			//inject the data
			try {
				BeanInfo info = Introspector.getBeanInfo( connections.getClass() );
				PropertyDescriptor[] descritors = info.getPropertyDescriptors();
				int size = descritors.length;
				for (int index = 0 ; index < size ; index++) {
					String propertyName = descritors[index].getName();
					if ( connectionProviderInjectionData.containsKey( propertyName ) ) {
						Method method = descritors[index].getWriteMethod();
						method.invoke( connections, new Object[] { connectionProviderInjectionData.get( propertyName ) } );
					}
				}
			}
			catch (IntrospectionException e) {
				throw new HibernateException("Unable to inject objects into the conenction provider", e);
			}
			catch (IllegalAccessException e) {
				throw new HibernateException("Unable to inject objects into the conenction provider", e);
			}
			catch (InvocationTargetException e) {
				throw new HibernateException("Unable to inject objects into the conenction provider", e);
			}
		}
		connections.configure(properties);
		return connections;