KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > ant > AntTaskJbpmSessionFactory


1 package org.jbpm.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
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 /**
19  * common strategy for jbpm ant tasks to obtain a {@link org.jbpm.db.JbpmSessionFactory}.
20  */

21 public abstract class AntTaskJbpmSessionFactory {
22   
23   private final static Map JavaDoc jbpmSessionFactoryCache = new HashMap JavaDoc();
24   private final static Map JavaDoc configurationCache = new HashMap JavaDoc();
25   
26   public static JbpmSessionFactory getJbpmSessionFactory(String JavaDoc cfg, String JavaDoc properties) {
27     JbpmSessionFactory jbpmSessionFactory = null;
28     
29     List JavaDoc 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 JavaDoc cfg, String JavaDoc properties) {
46     Configuration configuration = null;
47     
48     List JavaDoc 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 JavaDoc getKey(String JavaDoc cfg, String JavaDoc properties) {
65     List JavaDoc key = new ArrayList JavaDoc();
66     key.add(cfg);
67     key.add(properties);
68     return key;
69   }
70
71   private static JbpmSessionFactory createJbpmSessionFactory(String JavaDoc cfg, String JavaDoc properties) {
72     JbpmSessionFactory jbpmSessionFactory = null;
73     try {
74       Configuration configuration = getConfiguration(cfg, properties);
75
76       // creating the session factory
77
jbpmSessionFactory = new JbpmSessionFactory(configuration);
78
79     } catch (Exception JavaDoc 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 JavaDoc cfg, String JavaDoc properties) {
87     // configure the hibernate configuration
88
Configuration configuration = null;
89     
90     try {
91       if (cfg!=null) {
92         File JavaDoc cfgFile = new File JavaDoc(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 specified, overwrite the properties with the properties
101
// in the specified properties file
102
if (properties!=null) {
103         InputStream JavaDoc inputStream = new FileInputStream JavaDoc(new File JavaDoc(properties));
104         Properties JavaDoc props = new Properties JavaDoc();
105         props.load(inputStream);
106         configuration.setProperties(props);
107       }
108       List JavaDoc key = getKey(cfg, properties);
109       configurationCache.put(key, configuration);
110       
111     } catch (Exception JavaDoc 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