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.db.hibernate;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.hibernate.SessionFactory;
034: import org.hibernate.cfg.Configuration;
035: import org.hibernate.tool.hbm2ddl.SchemaExport;
036: import org.jbpm.JbpmException;
037: import org.jbpm.context.def.VariableAccess;
038: import org.jbpm.graph.def.Action;
039: import org.jbpm.graph.def.Event;
040: import org.jbpm.graph.def.ExceptionHandler;
041: import org.jbpm.graph.def.Node;
042: import org.jbpm.graph.def.ProcessDefinition;
043: import org.jbpm.graph.def.Transition;
044: import org.jbpm.instantiation.Delegation;
045: import org.jbpm.module.def.ModuleDefinition;
046: import org.jbpm.taskmgmt.def.Task;
047: import org.jbpm.taskmgmt.def.TaskController;
048: import org.jbpm.util.ClassLoaderUtil;
049:
050: public abstract class HibernateHelper {
051:
052: /** maps SessionFactory's to Configurations. */
053: /** by default, configuration lookup will be enabled */
054: static Map configurations = new HashMap();
055:
056: public static void clearConfigurationsCache() {
057: configurations = new HashMap();
058: }
059:
060: public static SessionFactory createSessionFactory() {
061: return createSessionFactory(null, null, true);
062: }
063:
064: public static SessionFactory createSessionFactory(
065: String cfgXmlResource) {
066: return createSessionFactory(cfgXmlResource, null, true);
067: }
068:
069: public static SessionFactory createSessionFactory(
070: String cfgXmlResource, String propertiesResource) {
071: return createSessionFactory(cfgXmlResource, propertiesResource,
072: true);
073: }
074:
075: public static SessionFactory createSessionFactory(
076: String cfgXmlResource, String propertiesResource,
077: boolean isConfigLookupEnabled) {
078: Configuration configuration = createConfiguration(
079: cfgXmlResource, propertiesResource);
080: return createSessionFactory(configuration,
081: isConfigLookupEnabled);
082: }
083:
084: public static SessionFactory createSessionFactory(
085: Configuration configuration, boolean isConfigLookupEnabled) {
086: SessionFactory sessionFactory = configuration
087: .buildSessionFactory();
088: if (isConfigLookupEnabled) {
089: configurations.put(sessionFactory, configuration);
090: }
091: return sessionFactory;
092: }
093:
094: public static Configuration createConfiguration(
095: String cfgXmlResource, String propertiesResource) {
096: Configuration configuration = new Configuration();
097:
098: // if a special hibernate configuration xml file is specified,
099: if (cfgXmlResource != null) {
100: // use the configured file name
101: log.debug("creating hibernate configuration resource '"
102: + cfgXmlResource + "'");
103: configuration.configure(cfgXmlResource);
104: } else {
105: log
106: .debug("using default hibernate configuration resource (hibernate.cfg.xml)");
107: configuration.configure();
108: }
109:
110: // if the properties are specified in a separate file
111: if (propertiesResource != null) {
112: log.debug("using hibernate properties from resource '"
113: + propertiesResource + "'");
114: // load the properties
115: Properties properties = loadPropertiesFromResource(propertiesResource);
116: // and overwrite the properties with the specified properties
117: configuration.setProperties(properties);
118: }
119:
120: return configuration;
121: }
122:
123: public static Configuration getConfiguration(
124: SessionFactory sessionFactory) {
125: return (Configuration) configurations.get(sessionFactory);
126: }
127:
128: public static SchemaExport createSchemaExport(
129: SessionFactory sessionFactory) {
130: return new SchemaExport(getConfiguration(sessionFactory));
131: }
132:
133: public static boolean createSchemaExportScript(
134: SessionFactory sessionFactory) {
135: boolean script = false;
136: String showSql = getConfiguration(sessionFactory).getProperty(
137: "hibernate.show_sql");
138: if ("true".equalsIgnoreCase(showSql)) {
139: script = true;
140: }
141: return script;
142: }
143:
144: public static void clearHibernateCache(SessionFactory sessionFactory) {
145: sessionFactory.evictQueries();
146:
147: Map classMetadata = sessionFactory.getAllClassMetadata();
148: Iterator iter = classMetadata.keySet().iterator();
149: while (iter.hasNext()) {
150: String entityName = (String) iter.next();
151: log.debug("evicting entities " + entityName);
152: Class entityClass = ClassLoaderUtil.loadClass(entityName);
153: sessionFactory.evict(entityClass);
154: }
155:
156: Map collectionMetadata = sessionFactory
157: .getAllCollectionMetadata();
158: iter = collectionMetadata.keySet().iterator();
159: while (iter.hasNext()) {
160: String collectionName = (String) iter.next();
161: log.debug("evicting collection " + collectionName);
162: sessionFactory.evictCollection(collectionName);
163: }
164: }
165:
166: static Properties loadPropertiesFromResource(String resource) {
167: Properties properties = new Properties();
168: try {
169: InputStream inputStream = ClassLoaderUtil
170: .getStream(resource);
171: properties.load(inputStream);
172:
173: } catch (NullPointerException e) {
174: throw new JbpmException(
175: "couldn't load hibernate properties from unexisting resource '"
176: + resource + "'", e);
177: } catch (IOException e) {
178: throw new JbpmException(
179: "couldn't load hibernate properties from resource '"
180: + resource + "'", e);
181: }
182: return properties;
183: }
184:
185: private static Log log = LogFactory.getLog(HibernateHelper.class);
186: }
|