KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > db > JbpmSessionFactory


1 package org.jbpm.db;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import javax.naming.InitialContext JavaDoc;
12 import javax.rmi.PortableRemoteObject JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.hibernate.HibernateException;
17 import org.hibernate.Session;
18 import org.hibernate.SessionFactory;
19 import org.hibernate.cfg.Configuration;
20 import org.hibernate.mapping.PersistentClass;
21 import org.hibernate.type.LongType;
22 import org.hibernate.type.StringType;
23 import org.jbpm.JbpmConfiguration;
24 import org.jbpm.graph.def.ProcessDefinition;
25 import org.jbpm.instantiation.ClassLoaderUtil;
26
27 /**
28  * creates JbpmSessions.
29  * Obtain a JbpmSessionFactory with
30  * <pre>
31  * static JbpmSessionFactory jbpmSessionFactory = JbpmSessionFactory.buildJbpmSessionFactory();
32  * </pre>
33  * and store it somewhere static. It takes quite some time to create a DbSessionFactory,
34  * but you only have to do it once. After that, creating DbSession's is really fast.
35  */

36 public class JbpmSessionFactory implements Serializable JavaDoc {
37   
38   private static final long serialVersionUID = 1L;
39
40   private static String JavaDoc jndiName = JbpmConfiguration.getString("jbpm.session.factory.jndi.name");
41
42   private Configuration configuration = null;
43   private SessionFactory sessionFactory = null;
44   private Collection JavaDoc hibernatableLongIdClasses = null;
45   private Collection JavaDoc hibernatableStringIdClasses = null;
46   private JbpmSchema jbpmSchema = null;
47   
48   private static JbpmSessionFactory instance = null;
49   /**
50    * a singleton is kept in JbpmSessionFactory as a convenient central location.
51    */

52   public static JbpmSessionFactory getInstance() {
53     if (instance==null) {
54       
55       // if there is a JNDI name configured
56
if (jndiName!=null) {
57         try {
58           // fetch the JbpmSessionFactory from JNDI
59
log.debug("fetching JbpmSessionFactory from '"+jndiName+"'");
60           InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
61           Object JavaDoc o = initialContext.lookup(jndiName);
62           instance = (JbpmSessionFactory) PortableRemoteObject.narrow(o, JbpmSessionFactory.class);
63         } catch (Exception JavaDoc e) {
64           throw new RuntimeException JavaDoc("couldn't fetch JbpmSessionFactory from jndi '"+jndiName+"'");
65         }
66         
67       } else { // else there is no JNDI name configured
68
// create a new default instance.
69
log.debug("building singleton JbpmSessionFactory");
70         instance = buildJbpmSessionFactory();
71       }
72     }
73     return instance;
74   }
75   
76   public JbpmSessionFactory(Configuration configuration) {
77     this( configuration, buildSessionFactory(configuration) );
78   }
79
80   public JbpmSessionFactory(Configuration configuration, SessionFactory sessionFactory) {
81     this.configuration = configuration;
82     this.sessionFactory = sessionFactory;
83   }
84   
85   public static JbpmSessionFactory buildJbpmSessionFactory() {
86     return buildJbpmSessionFactory(getConfigResource());
87   }
88
89   public static JbpmSessionFactory buildJbpmSessionFactory(String JavaDoc configResource) {
90     return buildJbpmSessionFactory(createConfiguration(configResource));
91   }
92   
93   public static JbpmSessionFactory buildJbpmSessionFactory(Configuration configuration) {
94     return new JbpmSessionFactory(configuration);
95   }
96
97   private static String JavaDoc getConfigResource() {
98     return JbpmConfiguration.getString("jbpm.hibernate.cfg.xml");
99   }
100
101   public static Configuration createConfiguration() {
102     return createConfiguration(getConfigResource());
103   }
104
105   public static Configuration createConfiguration(String JavaDoc configResource) {
106     Configuration configuration = null;
107     // create the hibernate configuration
108
configuration = new Configuration();
109     if (configResource!=null) {
110       log.debug("using '"+configResource+"' as hibernate configuration for jbpm");
111       configuration.configure(configResource);
112     } else {
113       log.debug("using the default hibernate configuration file: hibernate.cfg.xml");
114       configuration.configure();
115     }
116     
117     // check if the properties in the hibernate.cfg.xml need to be overwritten by a separate properties file.
118
String JavaDoc hibernatePropertiesResource = JbpmConfiguration.getString("jbpm.hibernate.properties");
119     if (hibernatePropertiesResource!=null) {
120       Properties JavaDoc hibernateProperties = new Properties JavaDoc();
121       try {
122         hibernateProperties.load( ClassLoaderUtil.getStream(hibernatePropertiesResource) );
123       } catch (IOException JavaDoc e) {
124         e.printStackTrace();
125         throw new RuntimeException JavaDoc("couldn't load the hibernate properties from resource '"+hibernatePropertiesResource+"'", e);
126       }
127       log.debug("overriding hibernate properties with "+ hibernateProperties);
128       configuration.setProperties(hibernateProperties);
129     }
130     
131     return configuration;
132   }
133
134   public static SessionFactory buildSessionFactory(Configuration configuration) {
135     SessionFactory sessionFactory = null;
136     // create the hibernate session factory
137
log.debug("building hibernate session factory");
138     sessionFactory = configuration.buildSessionFactory();
139     return sessionFactory;
140   }
141
142   /**
143    * obtains a jdbc connection as specified in the hibernate configurations and
144    * creates a DbSession with it.
145    */

146   public JbpmSession openJbpmSession() {
147     return openJbpmSession(null);
148   }
149
150   /**
151    * creates a DbSession around the given connection. Note that you are
152    * responsible for closing the connection so closing the DbSession will
153    * not close the jdbc connection.
154    */

155   public JbpmSession openJbpmSession(Connection JavaDoc jdbcConnection) {
156     JbpmSession dbSession = null;
157     
158     try {
159       Session session = null;
160       
161       if ( jdbcConnection == null ) {
162         // use the hibernate properties in the nwsp.properties file to
163
// create a jdbc connection for the created hibernate session.
164
session = getSessionFactory().openSession();
165       } else {
166         // use the client provided jdbc connection in
167
// the created hibernate session.
168
session = getSessionFactory().openSession(jdbcConnection);
169       }
170       
171       dbSession = new JbpmSession( this, session );
172       
173     } catch (HibernateException e) {
174       log.error( e );
175       throw new RuntimeException JavaDoc( "couldn't create a hibernate persistence session", e );
176     }
177     return dbSession;
178   }
179
180   public JbpmSession openJbpmSessionAndBeginTransaction() {
181     JbpmSession dbSession = openJbpmSession(null);
182     dbSession.beginTransaction();
183     return dbSession;
184   }
185     
186   public SessionFactory getSessionFactory() {
187     return sessionFactory;
188   }
189   
190   public Configuration getConfiguration() {
191     return configuration;
192   }
193   
194   /**
195    * clears the process definitions from hibernate's second level cache.
196    */

197   public void evictCachedProcessDefinitions() {
198     sessionFactory.evict(ProcessDefinition.class);
199   }
200
201   /**
202    * checks if the given class is persistable with hibernate and has an id of type long.
203    */

204   public boolean isHibernatableWithLongId(Class JavaDoc clazz) {
205     if (hibernatableLongIdClasses==null) {
206       initHibernatableClasses();
207     }
208     return hibernatableLongIdClasses.contains(clazz);
209   }
210
211   /**
212    * checks if the given class is persistable with hibernate and has an id of type string.
213    */

214   public boolean isHibernatableWithStringId(Class JavaDoc clazz) {
215     if (hibernatableStringIdClasses==null) {
216       initHibernatableClasses();
217     }
218     return hibernatableStringIdClasses.contains(clazz);
219   }
220   
221   public JbpmSchema getJbpmSchema() {
222     if (jbpmSchema==null) {
223       jbpmSchema = new JbpmSchema(configuration);
224     }
225     return jbpmSchema;
226   }
227
228   private void initHibernatableClasses() {
229     hibernatableLongIdClasses = new HashSet JavaDoc();
230     hibernatableStringIdClasses = new HashSet JavaDoc();
231     Iterator JavaDoc iter = configuration.getClassMappings();
232     while (iter.hasNext()) {
233       PersistentClass persistentClass = (PersistentClass) iter.next();
234       if (LongType.class==persistentClass.getIdentifier().getType().getClass()) {
235         hibernatableLongIdClasses.add( persistentClass.getMappedClass() );
236       } else if (StringType.class==persistentClass.getIdentifier().getType().getClass()) {
237         hibernatableStringIdClasses.add( persistentClass.getMappedClass() );
238       }
239     }
240   }
241
242   private static final Log log = LogFactory.getLog(JbpmSessionFactory.class);
243 }
244
Popular Tags