KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > connection > ConnectionProviderFactory


1 //$Id: ConnectionProviderFactory.java,v 1.4 2005/07/18 22:36:43 epbernard Exp $
2
package org.hibernate.connection;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.beans.Introspector JavaDoc;
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.IntrospectionException JavaDoc;
12 import java.beans.PropertyDescriptor JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
24  * Instantiates a connection provider given either <tt>System</tt> properties or
25  * a <tt>java.util.Properties</tt> instance. The <tt>ConnectionProviderFactory</tt>
26  * first attempts to find a name of a <tt>ConnectionProvider</tt> subclass in the
27  * property <tt>hibernate.connection.provider_class</tt>. If missing, heuristics are used
28  * to choose either <tt>DriverManagerConnectionProvider</tt>,
29  * <tt>DatasourceConnectionProvider</tt>, <tt>C3P0ConnectionProvider</tt> or
30  * <tt>DBCPConnectionProvider</tt>.
31  * @see ConnectionProvider
32  * @author Gavin King
33  */

34
35 public final class ConnectionProviderFactory {
36
37     private static final Log log = LogFactory.getLog(ConnectionProviderFactory.class);
38
39     /**
40      * Instantiate a <tt>ConnectionProvider</tt> using <tt>System</tt> properties.
41      * @return ConnectionProvider
42      * @throws HibernateException
43      */

44     public static ConnectionProvider newConnectionProvider() throws HibernateException {
45         return newConnectionProvider( Environment.getProperties() );
46     }
47
48     /**
49      * Instantiate a <tt>ConnectionProvider</tt> using given properties.
50      * Method newConnectionProvider.
51      * @param properties hibernate <tt>SessionFactory</tt> properties
52      * @return ConnectionProvider
53      * @throws HibernateException
54      */

55     public static ConnectionProvider newConnectionProvider(Properties JavaDoc properties) throws HibernateException {
56         return newConnectionProvider( properties, null );
57     }
58
59     /**
60      * Instantiate a <tt>ConnectionProvider</tt> using given properties.
61      * Method newConnectionProvider.
62      * @param properties hibernate <tt>SessionFactory</tt> properties
63      * @Param connectionProviderInjectionData object to be injected in the conenction provided
64      * @return ConnectionProvider
65      * @throws HibernateException
66      */

67     public static ConnectionProvider newConnectionProvider(Properties JavaDoc properties, Map JavaDoc connectionProviderInjectionData) throws HibernateException {
68         ConnectionProvider connections;
69         String JavaDoc 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 JavaDoc 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             //inject the data
102
try {
103                 BeanInfo JavaDoc info = Introspector.getBeanInfo( connections.getClass() );
104                 PropertyDescriptor JavaDoc[] descritors = info.getPropertyDescriptors();
105                 int size = descritors.length;
106                 for (int index = 0 ; index < size ; index++) {
107                     String JavaDoc propertyName = descritors[index].getName();
108                     if ( connectionProviderInjectionData.containsKey( propertyName ) ) {
109                         Method JavaDoc method = descritors[index].getWriteMethod();
110                         method.invoke( connections, new Object JavaDoc[] { connectionProviderInjectionData.get( propertyName ) } );
111                     }
112                 }
113             }
114             catch (IntrospectionException JavaDoc e) {
115                 throw new HibernateException("Unable to inject objects into the conenction provider", e);
116             }
117             catch (IllegalAccessException JavaDoc e) {
118                 throw new HibernateException("Unable to inject objects into the conenction provider", e);
119             }
120             catch (InvocationTargetException JavaDoc 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     // cannot be instantiated
129
private ConnectionProviderFactory() { throw new UnsupportedOperationException JavaDoc(); }
130
131     /**
132      * Transform JDBC connection properties.
133      *
134      * Passed in the form <tt>hibernate.connection.*</tt> to the
135      * format accepted by <tt>DriverManager</tt> by triming the leading "<tt>hibernate.connection</tt>".
136      */

137     public static Properties JavaDoc getConnectionProperties(Properties JavaDoc properties) {
138
139         Iterator JavaDoc iter = properties.keySet().iterator();
140         Properties JavaDoc result = new Properties JavaDoc();
141         while ( iter.hasNext() ) {
142             String JavaDoc prop = (String JavaDoc) 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 JavaDoc userName = properties.getProperty(Environment.USER);
151         if (userName!=null) result.setProperty( "user", userName );
152         return result;
153     }
154
155     private static final Set JavaDoc SPECIAL_PROPERTIES;
156     static {
157         SPECIAL_PROPERTIES = new HashSet JavaDoc();
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