001: //$Id: ConnectionProviderFactory.java 7541 2005-07-18 22:37:31Z epbernard $
002: package org.hibernate.connection;
003:
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Properties;
007: import java.util.Set;
008: import java.util.Map;
009: import java.beans.Introspector;
010: import java.beans.BeanInfo;
011: import java.beans.IntrospectionException;
012: import java.beans.PropertyDescriptor;
013: import java.lang.reflect.Method;
014: import java.lang.reflect.InvocationTargetException;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018:
019: import org.hibernate.HibernateException;
020: import org.hibernate.cfg.Environment;
021: import org.hibernate.util.ReflectHelper;
022:
023: /**
024: * Instantiates a connection provider given either <tt>System</tt> properties or
025: * a <tt>java.util.Properties</tt> instance. The <tt>ConnectionProviderFactory</tt>
026: * first attempts to find a name of a <tt>ConnectionProvider</tt> subclass in the
027: * property <tt>hibernate.connection.provider_class</tt>. If missing, heuristics are used
028: * to choose either <tt>DriverManagerConnectionProvider</tt>,
029: * <tt>DatasourceConnectionProvider</tt>, <tt>C3P0ConnectionProvider</tt> or
030: * <tt>DBCPConnectionProvider</tt>.
031: * @see ConnectionProvider
032: * @author Gavin King
033: */
034:
035: public final class ConnectionProviderFactory {
036:
037: private static final Log log = LogFactory
038: .getLog(ConnectionProviderFactory.class);
039:
040: /**
041: * Instantiate a <tt>ConnectionProvider</tt> using <tt>System</tt> properties.
042: * @return ConnectionProvider
043: * @throws HibernateException
044: */
045: public static ConnectionProvider newConnectionProvider()
046: throws HibernateException {
047: return newConnectionProvider(Environment.getProperties());
048: }
049:
050: /**
051: * Instantiate a <tt>ConnectionProvider</tt> using given properties.
052: * Method newConnectionProvider.
053: * @param properties hibernate <tt>SessionFactory</tt> properties
054: * @return ConnectionProvider
055: * @throws HibernateException
056: */
057: public static ConnectionProvider newConnectionProvider(
058: Properties properties) throws HibernateException {
059: return newConnectionProvider(properties, null);
060: }
061:
062: /**
063: * Instantiate a <tt>ConnectionProvider</tt> using given properties.
064: * Method newConnectionProvider.
065: * @param properties hibernate <tt>SessionFactory</tt> properties
066: * @Param connectionProviderInjectionData object to be injected in the conenction provided
067: * @return ConnectionProvider
068: * @throws HibernateException
069: */
070: public static ConnectionProvider newConnectionProvider(
071: Properties properties, Map connectionProviderInjectionData)
072: throws HibernateException {
073: ConnectionProvider connections;
074: String providerClass = properties
075: .getProperty(Environment.CONNECTION_PROVIDER);
076: if (providerClass != null) {
077: try {
078: log.info("Initializing connection provider: "
079: + providerClass);
080: connections = (ConnectionProvider) ReflectHelper
081: .classForName(providerClass).newInstance();
082: } catch (Exception e) {
083: log.fatal("Could not instantiate connection provider",
084: e);
085: throw new HibernateException(
086: "Could not instantiate connection provider: "
087: + providerClass);
088: }
089: } else if (properties.getProperty(Environment.DATASOURCE) != null) {
090: connections = new DatasourceConnectionProvider();
091: } else if (properties.getProperty(Environment.C3P0_MAX_SIZE) != null) {
092: connections = new C3P0ConnectionProvider();
093: } else if (properties.getProperty(Environment.PROXOOL_XML) != null
094: || properties
095: .getProperty(Environment.PROXOOL_PROPERTIES) != null
096: || properties
097: .getProperty(Environment.PROXOOL_EXISTING_POOL) != null) {
098: connections = new ProxoolConnectionProvider();
099: } else if (properties.getProperty(Environment.URL) != null) {
100: connections = new DriverManagerConnectionProvider();
101: } else {
102: connections = new UserSuppliedConnectionProvider();
103: }
104:
105: if (connectionProviderInjectionData != null
106: && connectionProviderInjectionData.size() != 0) {
107: //inject the data
108: try {
109: BeanInfo info = Introspector.getBeanInfo(connections
110: .getClass());
111: PropertyDescriptor[] descritors = info
112: .getPropertyDescriptors();
113: int size = descritors.length;
114: for (int index = 0; index < size; index++) {
115: String propertyName = descritors[index].getName();
116: if (connectionProviderInjectionData
117: .containsKey(propertyName)) {
118: Method method = descritors[index]
119: .getWriteMethod();
120: method
121: .invoke(
122: connections,
123: new Object[] { connectionProviderInjectionData
124: .get(propertyName) });
125: }
126: }
127: } catch (IntrospectionException e) {
128: throw new HibernateException(
129: "Unable to inject objects into the conenction provider",
130: e);
131: } catch (IllegalAccessException e) {
132: throw new HibernateException(
133: "Unable to inject objects into the conenction provider",
134: e);
135: } catch (InvocationTargetException e) {
136: throw new HibernateException(
137: "Unable to inject objects into the conenction provider",
138: e);
139: }
140: }
141: connections.configure(properties);
142: return connections;
143: }
144:
145: // cannot be instantiated
146: private ConnectionProviderFactory() {
147: throw new UnsupportedOperationException();
148: }
149:
150: /**
151: * Transform JDBC connection properties.
152: *
153: * Passed in the form <tt>hibernate.connection.*</tt> to the
154: * format accepted by <tt>DriverManager</tt> by triming the leading "<tt>hibernate.connection</tt>".
155: */
156: public static Properties getConnectionProperties(
157: Properties properties) {
158:
159: Iterator iter = properties.keySet().iterator();
160: Properties result = new Properties();
161: while (iter.hasNext()) {
162: String prop = (String) iter.next();
163: if (prop.indexOf(Environment.CONNECTION_PREFIX) > -1
164: && !SPECIAL_PROPERTIES.contains(prop)) {
165: result.setProperty(prop
166: .substring(Environment.CONNECTION_PREFIX
167: .length() + 1), properties
168: .getProperty(prop));
169: }
170: }
171: String userName = properties.getProperty(Environment.USER);
172: if (userName != null)
173: result.setProperty("user", userName);
174: return result;
175: }
176:
177: private static final Set SPECIAL_PROPERTIES;
178: static {
179: SPECIAL_PROPERTIES = new HashSet();
180: SPECIAL_PROPERTIES.add(Environment.DATASOURCE);
181: SPECIAL_PROPERTIES.add(Environment.URL);
182: SPECIAL_PROPERTIES.add(Environment.CONNECTION_PROVIDER);
183: SPECIAL_PROPERTIES.add(Environment.POOL_SIZE);
184: SPECIAL_PROPERTIES.add(Environment.ISOLATION);
185: SPECIAL_PROPERTIES.add(Environment.DRIVER);
186: SPECIAL_PROPERTIES.add(Environment.USER);
187:
188: }
189:
190: }
|