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: }
|