001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.persistence.db;
023:
024: import javax.sql.DataSource;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.hibernate.SessionFactory;
029: import org.hibernate.cfg.Configuration;
030: import org.hibernate.tool.hbm2ddl.SchemaExport;
031: import org.jbpm.JbpmConfiguration;
032: import org.jbpm.db.hibernate.HibernateHelper;
033: import org.jbpm.svc.Service;
034: import org.jbpm.svc.ServiceFactory;
035: import org.jbpm.util.JndiUtil;
036:
037: public class DbPersistenceServiceFactory implements ServiceFactory {
038:
039: private static final long serialVersionUID = 1L;
040:
041: Configuration configuration = null;
042:
043: String sessionFactoryJndiName = null;
044: SessionFactory sessionFactory = null;
045:
046: String dataSourceJndiName = null;
047: DataSource dataSource = null;
048:
049: boolean isTransactionEnabled = true;
050: boolean isCurrentSessionEnabled = false;
051:
052: SchemaExport schemaExport = null;
053:
054: public Service openService() {
055: log.debug("creating persistence service");
056: return new DbPersistenceService(this );
057: }
058:
059: public synchronized Configuration getConfiguration() {
060: if (configuration == null) {
061: String hibernateCfgXmlResource = null;
062: if (JbpmConfiguration.Configs
063: .hasObject("resource.hibernate.cfg.xml")) {
064: hibernateCfgXmlResource = JbpmConfiguration.Configs
065: .getString("resource.hibernate.cfg.xml");
066: }
067: String hibernatePropertiesResource = null;
068: if (JbpmConfiguration.Configs
069: .hasObject("resource.hibernate.properties")) {
070: hibernatePropertiesResource = JbpmConfiguration.Configs
071: .getString("resource.hibernate.properties");
072: }
073: configuration = HibernateHelper.createConfiguration(
074: hibernateCfgXmlResource,
075: hibernatePropertiesResource);
076: }
077: return configuration;
078: }
079:
080: public synchronized SchemaExport getSchemaExport() {
081: if (schemaExport == null) {
082: log.debug("creating schema export");
083: schemaExport = new SchemaExport(getConfiguration());
084: }
085: return schemaExport;
086: }
087:
088: public synchronized SessionFactory getSessionFactory() {
089: if (sessionFactory == null) {
090:
091: if (sessionFactoryJndiName != null) {
092: log
093: .debug("looking up hibernate session factory in jndi '"
094: + sessionFactoryJndiName + "'");
095: sessionFactory = (SessionFactory) JndiUtil.lookup(
096: sessionFactoryJndiName, SessionFactory.class);
097:
098: } else {
099: log.debug("building hibernate session factory");
100: sessionFactory = getConfiguration()
101: .buildSessionFactory();
102: }
103: }
104: return sessionFactory;
105: }
106:
107: public DataSource getDataSource() {
108: if ((dataSource == null) && (dataSourceJndiName != null)) {
109: log.debug("looking up datasource from jndi location '"
110: + dataSourceJndiName + "'");
111: dataSource = (DataSource) JndiUtil.lookup(
112: dataSourceJndiName, DataSource.class);
113: }
114: return dataSource;
115: }
116:
117: public void createSchema() {
118: getSchemaExport().create(getScript(), true);
119: HibernateHelper.clearHibernateCache(getSessionFactory());
120: }
121:
122: public void dropSchema() {
123: HibernateHelper.clearHibernateCache(getSessionFactory());
124: getSchemaExport().drop(getScript(), true);
125: }
126:
127: boolean getScript() {
128: boolean script = false;
129: String showSql = getConfiguration().getProperty(
130: "hibernate.show_sql");
131: if ("true".equalsIgnoreCase(showSql)) {
132: script = true;
133: }
134: return script;
135: }
136:
137: public void close() {
138: if (sessionFactory != null) {
139: log.debug("closing hibernate session factory");
140: sessionFactory.close();
141: }
142: }
143:
144: public String getDataSourceJndiName() {
145: return dataSourceJndiName;
146: }
147:
148: public void setDataSourceJndiName(String dataSourceJndiName) {
149: this .dataSourceJndiName = dataSourceJndiName;
150: }
151:
152: public String getSessionFactoryJndiName() {
153: return sessionFactoryJndiName;
154: }
155:
156: public void setSessionFactoryJndiName(String sessionFactoryJndiName) {
157: this .sessionFactoryJndiName = sessionFactoryJndiName;
158: }
159:
160: public void setConfiguration(Configuration configuration) {
161: this .configuration = configuration;
162: }
163:
164: public void setDataSource(DataSource dataSource) {
165: this .dataSource = dataSource;
166: }
167:
168: public void setSchemaExport(SchemaExport schemaExport) {
169: this .schemaExport = schemaExport;
170: }
171:
172: public void setSessionFactory(SessionFactory sessionFactory) {
173: this .sessionFactory = sessionFactory;
174: }
175:
176: public boolean isTransactionEnabled() {
177: return isTransactionEnabled;
178: }
179:
180: public void setTransactionEnabled(boolean isTransactionEnabled) {
181: this .isTransactionEnabled = isTransactionEnabled;
182: }
183:
184: public boolean isCurrentSessionEnabled() {
185: return isCurrentSessionEnabled;
186: }
187:
188: public void setCurrentSessionEnabled(boolean isCurrentSessionEnabled) {
189: this .isCurrentSessionEnabled = isCurrentSessionEnabled;
190: }
191:
192: private static Log log = LogFactory
193: .getLog(DbPersistenceServiceFactory.class);
194: }
|