Source Code Cross Referenced for HibernateConfig.java in  » ESB » wso2-esb » org » wso2 » esb » util » 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 » ESB » wso2 esb » org.wso2.esb.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004,2005 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.wso2.esb.util;
017:
018:        import org.hibernate.HibernateException;
019:        import org.hibernate.Session;
020:        import org.hibernate.SessionFactory;
021:        import org.hibernate.cfg.Configuration;
022:
023:        /**
024:         * A utility for managing Hibernate Configuration related stuff.
025:         * <p/>
026:         * We should maintain only one instance of getHibernateConfig within WSO2 ESB admin.
027:         * Each of the modules/services deployed should also maintain there own single instance
028:         */
029:        public class HibernateConfig {
030:            private final SessionFactory SESSION_FACTORY;
031:            private final ThreadLocal SESSION = new ThreadLocal();
032:
033:            /**
034:             * Constructor for initializing this Object providing the DB Connection Identifier
035:             * (<code>dbConnIdentifier</code>), and the the Hibernate Data Object classes
036:             * (<code>dataObjects</code>), along with other DB parameters.
037:             *
038:             * @param dbConnIdentifier The database connection identifier.
039:             *                         Can be a DB URL or a Datasource JNDI name. If it is a Datsource JNDI name,
040:             *                         it should start with <i>java:comp</i>.
041:             *                         This is a <b>required parameter</b> <br/>
042:             * @param dbDriver         The fully qualified DB driver class name.
043:             *                         This is a <b>required parameter</b> <br/>
044:             * @param sqlDialect       The hibernate SQL dialect for the specifed RDBMS.
045:             *                         This is a <b>required parameter</b><br/>
046:             * @param username         The username to connect to the DB<br/>
047:             * @param password         The password corresponding to the <code>username</code><br/>
048:             * @param dataObjects      Array of Hibernate Data Object Classes.
049:             *                         This is a <b>required parameter</b><br/>
050:             */
051:            public HibernateConfig(String dbConnIdentifier, String dbDriver,
052:                    String sqlDialect, String username, String password,
053:                    Class[] dataObjects) {
054:
055:                if (!validateRequiredParams(new Object[] { dbConnIdentifier,
056:                        dbDriver, sqlDialect, dataObjects })) {
057:                    throw new IllegalArgumentException(
058:                            "One or more of the required parameters "
059:                                    + "were not provided");
060:                }
061:                try {
062:                    Configuration cfg = new Configuration();
063:                    for (int i = 0; i < dataObjects.length; i++) {
064:                        cfg.addClass(dataObjects[i]);
065:                    }
066:
067:                    commonInit(cfg, sqlDialect, dbDriver, dbConnIdentifier,
068:                            username, password);
069:                    SESSION_FACTORY = cfg.buildSessionFactory();
070:                } catch (Throwable ex) {
071:                    System.err
072:                            .println("Initial SessionFactory creation failed."
073:                                    + ex);
074:                    throw new ExceptionInInitializerError(ex);
075:                }
076:            }
077:
078:            /**
079:             * Constructor for initializing this Object providing the DB Connection Identifier
080:             * (<code>dbConnIdentifier</code>), and the the Hibernate hbm XML filenames
081:             * (<code>hbmXMLFilenames</code>), along with other DB parameters.
082:             *
083:             * @param dbConnIdentifier The database connection identifier.
084:             *                         Can be a DB URL or a Datasource JNDI name. If it is a Datsource JNDI name,
085:             *                         it should start with <i>java:comp</i>.
086:             *                         This is a <b>required parameter</b> <br/>
087:             * @param dbDriver         The fully qualified DB driver class name.
088:             *                         This is a <b>required parameter</b> <br/>
089:             * @param sqlDialect       The hibernate SQL dialect for the specifed RDBMS.
090:             *                         This is a <b>required parameter</b><br/>
091:             * @param username         The username to connect to the DB<br/>
092:             * @param password         The password corresponding to the <code>username</code><br/>
093:             * @param hbmXMLFilenames  Array of Hibernate hbm XML Filenames.
094:             *                         This is a <b>required parameter</b><br/>
095:             */
096:            public HibernateConfig(String dbConnIdentifier, String dbDriver,
097:                    String sqlDialect, String username, String password,
098:                    String[] hbmXMLFilenames) {
099:
100:                if (!validateRequiredParams(new Object[] { dbConnIdentifier,
101:                        dbDriver, sqlDialect, hbmXMLFilenames })) {
102:                    throw new IllegalArgumentException(
103:                            "One or more of the required parameters "
104:                                    + "were not provided");
105:                }
106:                try {
107:                    Configuration cfg = new Configuration();
108:                    for (int i = 0; i < hbmXMLFilenames.length; i++) {
109:                        cfg.addResource(hbmXMLFilenames[i]);
110:                    }
111:
112:                    commonInit(cfg, sqlDialect, dbDriver, dbConnIdentifier,
113:                            username, password);
114:                    SESSION_FACTORY = cfg.buildSessionFactory();
115:                } catch (Throwable ex) {
116:                    System.err
117:                            .println("Initial SessionFactory creation failed."
118:                                    + ex);
119:                    throw new ExceptionInInitializerError(ex);
120:                }
121:            }
122:
123:            /**
124:             * Default Constructor. This should be package protected since we should only instantiate it
125:             * through it HibernateConfigCache
126:             * <br/>
127:             * <p/>
128:             * This will pickup hibernate.cfg.xml from the classpath
129:             * and load the configuration from this file.
130:             * @param conFigFileName   hibernate.cfg.xml - hibernate configuration file
131:             */
132:            public HibernateConfig(String conFigFileName) {
133:                try {
134:                    // Create the SessionFactory from hibernate.cfg.xml, which has to be in the classpath
135:                    SESSION_FACTORY = new Configuration().configure(
136:                            conFigFileName).buildSessionFactory();
137:                } catch (Throwable ex) {
138:                    System.err
139:                            .println("Initial SessionFactory creation failed."
140:                                    + ex);
141:                    throw new ExceptionInInitializerError(ex);
142:                }
143:            }
144:
145:            private void commonInit(Configuration cfg, String sqlDialect,
146:                    String dbDriver, String dbConnIdentifier, String username,
147:                    String password) {
148:
149:                cfg.setProperty("hibernate.dialect", sqlDialect);
150:
151:                if (dbConnIdentifier.indexOf("java:comp") == 0) { // Is it a datasource JNDI?
152:                    cfg.setProperty("hibernate.connection.datasource",
153:                            dbConnIdentifier);
154:                } else { // it is a db URL
155:                    cfg.setProperty("hibernate.connection.url",
156:                            dbConnIdentifier);
157:                    if (username != null && username.trim().length() != 0) {
158:                        cfg.setProperty("hibernate.connection.username",
159:                                username);
160:                    }
161:                    if (password != null && password.trim().length() != 0) {
162:                        cfg.setProperty("hibernate.connection.password",
163:                                password);
164:                    }
165:
166:                    //            cfg.setProperty("hibernate.hbm2ddl.auto", "update");
167:                    cfg.setProperty("hibernate.connection.driver_class",
168:                            dbDriver);
169:                }
170:            }
171:
172:            private boolean validateRequiredParams(Object[] params) {
173:                if (params == null) {
174:                    return false;
175:                }
176:                for (int i = 0; i < params.length; i++) {
177:                    Object param = params[i];
178:                    if (param == null) {
179:                        return false;
180:                    }
181:                    if (param instanceof  String
182:                            && ((String) param).trim().length() == 0) {
183:                        return false;
184:                    }
185:                    if (param instanceof  Object[]) {
186:                        if (!validateRequiredParams((Object[]) param)) {
187:                            return false;
188:                        }
189:                    }
190:                }
191:                return true;
192:            }
193:
194:            public Session currentSession() throws HibernateException {
195:                Session session = (Session) SESSION.get();
196:                if (session == null) {
197:                    session = SESSION_FACTORY.openSession();
198:                    SESSION.set(session);
199:                }
200:                return session;
201:            }
202:
203:            public void closeSession() throws HibernateException {
204:                Session session = (Session) SESSION.get();
205:                if (session != null) {
206:                    session.close();
207:                }
208:                SESSION.set(null);
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.