1 package org.jbpm.ant; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.InputStream ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.apache.tools.ant.BuildException; 15 import org.hibernate.cfg.Configuration; 16 import org.jbpm.db.JbpmSessionFactory; 17 18 21 public abstract class AntTaskJbpmSessionFactory { 22 23 private final static Map jbpmSessionFactoryCache = new HashMap (); 24 private final static Map configurationCache = new HashMap (); 25 26 public static JbpmSessionFactory getJbpmSessionFactory(String cfg, String properties) { 27 JbpmSessionFactory jbpmSessionFactory = null; 28 29 List key = getKey(cfg, properties); 30 31 log.debug("checking jbpm session factory cache for key "+key); 32 log.debug("jbpm session factory cache: "+jbpmSessionFactoryCache); 33 if (jbpmSessionFactoryCache.containsKey(key)) { 34 log.debug("getting jbpm session factory from cache"); 35 jbpmSessionFactory = (JbpmSessionFactory) jbpmSessionFactoryCache.get(key); 36 } else { 37 log.debug("creating new jbpm session factory"); 38 jbpmSessionFactory = createJbpmSessionFactory(cfg, properties); 39 jbpmSessionFactoryCache.put(key, jbpmSessionFactory); 40 } 41 42 return jbpmSessionFactory; 43 } 44 45 public static Configuration getConfiguration(String cfg, String properties) { 46 Configuration configuration = null; 47 48 List key = getKey(cfg, properties); 49 50 log.debug("checking hibernate config cache for key "+key); 51 log.debug("hibernate config cache: "+configurationCache); 52 if (configurationCache.containsKey(key)) { 53 log.debug("getting hibernate config from cache"); 54 configuration = (Configuration) configurationCache.get(key); 55 } else { 56 log.debug("creating new hibernate config"); 57 configuration = createConfiguration(cfg, properties); 58 configurationCache.put(key, configuration); 59 } 60 61 return configuration; 62 } 63 64 private static List getKey(String cfg, String properties) { 65 List key = new ArrayList (); 66 key.add(cfg); 67 key.add(properties); 68 return key; 69 } 70 71 private static JbpmSessionFactory createJbpmSessionFactory(String cfg, String properties) { 72 JbpmSessionFactory jbpmSessionFactory = null; 73 try { 74 Configuration configuration = getConfiguration(cfg, properties); 75 76 jbpmSessionFactory = new JbpmSessionFactory(configuration); 78 79 } catch (Exception e) { 80 e.printStackTrace(); 81 throw new BuildException( "couldn't create JbpmSessionFactory: " + e.getMessage() ); 82 } 83 return jbpmSessionFactory; 84 } 85 86 private static Configuration createConfiguration(String cfg, String properties) { 87 Configuration configuration = null; 89 90 try { 91 if (cfg!=null) { 92 File cfgFile = new File (cfg); 93 log.debug("using '"+cfgFile+"' for hibernate configuration"); 94 configuration = new Configuration().configure(cfgFile); 95 } else { 96 log.debug("using the default jbpm configured hibernate configuration"); 97 configuration = JbpmSessionFactory.createConfiguration(); 98 } 99 100 if (properties!=null) { 103 InputStream inputStream = new FileInputStream (new File (properties)); 104 Properties props = new Properties (); 105 props.load(inputStream); 106 configuration.setProperties(props); 107 } 108 List key = getKey(cfg, properties); 109 configurationCache.put(key, configuration); 110 111 } catch (Exception e) { 112 e.printStackTrace(); 113 throw new BuildException( "couldn't create configuration: " + e.getMessage() ); 114 } 115 116 return configuration; 117 } 118 119 private static final Log log = LogFactory.getLog(AntTaskJbpmSessionFactory.class); 120 } 121 | Popular Tags |