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.ant;
023:
024: import java.io.InputStream;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.List;
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.cfg.Configuration;
034: import org.jbpm.JbpmConfiguration;
035: import org.jbpm.JbpmException;
036: import org.jbpm.persistence.db.DbPersistenceServiceFactory;
037: import org.jbpm.svc.Services;
038:
039: /**
040: * common strategy for jbpm ant tasks to obtain a hibernate SessionFactory.
041: */
042: public abstract class AntHelper {
043:
044: final static Map configurations = new HashMap();
045: final static Map jbpmConfigurations = new HashMap();
046:
047: public static Configuration getConfiguration(
048: String hibernateCfgResource,
049: String hibernatePropertiesResource) {
050: Object key = getKey(hibernateCfgResource,
051: hibernatePropertiesResource);
052: Configuration configuration = (Configuration) configurations
053: .get(key);
054: if (configuration == null) {
055: log.debug("creating hibernate configuration from cfg '"
056: + hibernateCfgResource + "' and properties '"
057: + hibernatePropertiesResource + "'");
058: configuration = new Configuration();
059: configuration.configure(hibernateCfgResource);
060: if (hibernatePropertiesResource != null) {
061: try {
062: InputStream propertiesInputStream = AntHelper.class
063: .getClassLoader().getResourceAsStream(
064: hibernatePropertiesResource);
065: log.debug("properties input stream: "
066: + propertiesInputStream);
067: Properties properties = new Properties();
068: properties.load(propertiesInputStream);
069: configuration.setProperties(properties);
070: } catch (Exception e) {
071: throw new JbpmException("couldn't set properties '"
072: + hibernatePropertiesResource + "'", e);
073: }
074: }
075: configurations.put(key, configuration);
076: } else {
077: log.debug("got hibernate configuration from cfg '"
078: + hibernateCfgResource + "' and properties '"
079: + hibernatePropertiesResource + "' from the cache");
080: }
081: return configuration;
082: }
083:
084: public static JbpmConfiguration getJbpmConfiguration(String jbpmCfg) {
085: JbpmConfiguration jbpmConfiguration = (JbpmConfiguration) jbpmConfigurations
086: .get(jbpmCfg);
087: if (jbpmConfiguration == null) {
088: if (jbpmCfg == null) {
089: jbpmConfiguration = JbpmConfiguration.getInstance();
090:
091: } else {
092: jbpmConfiguration = JbpmConfiguration
093: .getInstance(jbpmCfg);
094: }
095:
096: jbpmConfigurations.put(jbpmCfg, jbpmConfiguration);
097: }
098: return jbpmConfiguration;
099: }
100:
101: static Object getKey(String cfg, String properties) {
102: List key = new ArrayList();
103: key.add(cfg);
104: key.add(properties);
105: return key;
106: }
107:
108: private static final Log log = LogFactory.getLog(AntHelper.class);
109: }
|