1 package org.hibernate.connection; 3 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 import java.util.Properties ; 7 import java.util.Set ; 8 import java.util.Map ; 9 import java.beans.Introspector ; 10 import java.beans.BeanInfo ; 11 import java.beans.IntrospectionException ; 12 import java.beans.PropertyDescriptor ; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import org.hibernate.HibernateException; 20 import org.hibernate.cfg.Environment; 21 import org.hibernate.util.ReflectHelper; 22 23 34 35 public final class ConnectionProviderFactory { 36 37 private static final Log log = LogFactory.getLog(ConnectionProviderFactory.class); 38 39 44 public static ConnectionProvider newConnectionProvider() throws HibernateException { 45 return newConnectionProvider( Environment.getProperties() ); 46 } 47 48 55 public static ConnectionProvider newConnectionProvider(Properties properties) throws HibernateException { 56 return newConnectionProvider( properties, null ); 57 } 58 59 67 public static ConnectionProvider newConnectionProvider(Properties properties, Map connectionProviderInjectionData) throws HibernateException { 68 ConnectionProvider connections; 69 String providerClass = properties.getProperty(Environment.CONNECTION_PROVIDER); 70 if ( providerClass!=null ) { 71 try { 72 log.info("Initializing connection provider: " + providerClass); 73 connections = (ConnectionProvider) ReflectHelper.classForName(providerClass).newInstance(); 74 } 75 catch (Exception e) { 76 log.fatal("Could not instantiate connection provider", e); 77 throw new HibernateException("Could not instantiate connection provider: " + providerClass); 78 } 79 } 80 else if ( properties.getProperty(Environment.DATASOURCE)!=null ) { 81 connections = new DatasourceConnectionProvider(); 82 } 83 else if ( properties.getProperty(Environment.C3P0_MAX_SIZE)!=null ) { 84 connections = new C3P0ConnectionProvider(); 85 } 86 else if ( 87 properties.getProperty(Environment.PROXOOL_XML)!=null || 88 properties.getProperty(Environment.PROXOOL_PROPERTIES)!=null || 89 properties.getProperty(Environment.PROXOOL_EXISTING_POOL)!=null 90 ) { 91 connections = new ProxoolConnectionProvider(); 92 } 93 else if ( properties.getProperty(Environment.URL)!=null ) { 94 connections = new DriverManagerConnectionProvider(); 95 } 96 else { 97 connections = new UserSuppliedConnectionProvider(); 98 } 99 100 if ( connectionProviderInjectionData != null && connectionProviderInjectionData.size() != 0 ) { 101 try { 103 BeanInfo info = Introspector.getBeanInfo( connections.getClass() ); 104 PropertyDescriptor [] descritors = info.getPropertyDescriptors(); 105 int size = descritors.length; 106 for (int index = 0 ; index < size ; index++) { 107 String propertyName = descritors[index].getName(); 108 if ( connectionProviderInjectionData.containsKey( propertyName ) ) { 109 Method method = descritors[index].getWriteMethod(); 110 method.invoke( connections, new Object [] { connectionProviderInjectionData.get( propertyName ) } ); 111 } 112 } 113 } 114 catch (IntrospectionException e) { 115 throw new HibernateException("Unable to inject objects into the conenction provider", e); 116 } 117 catch (IllegalAccessException e) { 118 throw new HibernateException("Unable to inject objects into the conenction provider", e); 119 } 120 catch (InvocationTargetException e) { 121 throw new HibernateException("Unable to inject objects into the conenction provider", e); 122 } 123 } 124 connections.configure(properties); 125 return connections; 126 } 127 128 private ConnectionProviderFactory() { throw new UnsupportedOperationException (); } 130 131 137 public static Properties getConnectionProperties(Properties properties) { 138 139 Iterator iter = properties.keySet().iterator(); 140 Properties result = new Properties (); 141 while ( iter.hasNext() ) { 142 String prop = (String ) iter.next(); 143 if ( prop.indexOf(Environment.CONNECTION_PREFIX) > -1 && !SPECIAL_PROPERTIES.contains(prop) ) { 144 result.setProperty( 145 prop.substring( Environment.CONNECTION_PREFIX.length()+1 ), 146 properties.getProperty(prop) 147 ); 148 } 149 } 150 String userName = properties.getProperty(Environment.USER); 151 if (userName!=null) result.setProperty( "user", userName ); 152 return result; 153 } 154 155 private static final Set SPECIAL_PROPERTIES; 156 static { 157 SPECIAL_PROPERTIES = new HashSet (); 158 SPECIAL_PROPERTIES.add(Environment.DATASOURCE); 159 SPECIAL_PROPERTIES.add(Environment.URL); 160 SPECIAL_PROPERTIES.add(Environment.CONNECTION_PROVIDER); 161 SPECIAL_PROPERTIES.add(Environment.POOL_SIZE); 162 SPECIAL_PROPERTIES.add(Environment.ISOLATION); 163 SPECIAL_PROPERTIES.add(Environment.DRIVER); 164 SPECIAL_PROPERTIES.add(Environment.USER); 165 166 } 167 168 } 169 170 171 172 173 174 175 | Popular Tags |