Source Code Cross Referenced for ConnectionProviderFactory.java in  » Database-ORM » hibernate » org » hibernate » connection » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » hibernate » org.hibernate.connection 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.