1 package org.jbpm.db; 2 3 import java.io.IOException ; 4 import java.io.Serializable ; 5 import java.sql.Connection ; 6 import java.util.Collection ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.Properties ; 10 11 import javax.naming.InitialContext ; 12 import javax.rmi.PortableRemoteObject ; 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 36 public class JbpmSessionFactory implements Serializable { 37 38 private static final long serialVersionUID = 1L; 39 40 private static String jndiName = JbpmConfiguration.getString("jbpm.session.factory.jndi.name"); 41 42 private Configuration configuration = null; 43 private SessionFactory sessionFactory = null; 44 private Collection hibernatableLongIdClasses = null; 45 private Collection hibernatableStringIdClasses = null; 46 private JbpmSchema jbpmSchema = null; 47 48 private static JbpmSessionFactory instance = null; 49 52 public static JbpmSessionFactory getInstance() { 53 if (instance==null) { 54 55 if (jndiName!=null) { 57 try { 58 log.debug("fetching JbpmSessionFactory from '"+jndiName+"'"); 60 InitialContext initialContext = new InitialContext (); 61 Object o = initialContext.lookup(jndiName); 62 instance = (JbpmSessionFactory) PortableRemoteObject.narrow(o, JbpmSessionFactory.class); 63 } catch (Exception e) { 64 throw new RuntimeException ("couldn't fetch JbpmSessionFactory from jndi '"+jndiName+"'"); 65 } 66 67 } else { 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 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 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 configResource) { 106 Configuration configuration = null; 107 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 String hibernatePropertiesResource = JbpmConfiguration.getString("jbpm.hibernate.properties"); 119 if (hibernatePropertiesResource!=null) { 120 Properties hibernateProperties = new Properties (); 121 try { 122 hibernateProperties.load( ClassLoaderUtil.getStream(hibernatePropertiesResource) ); 123 } catch (IOException e) { 124 e.printStackTrace(); 125 throw new RuntimeException ("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 log.debug("building hibernate session factory"); 138 sessionFactory = configuration.buildSessionFactory(); 139 return sessionFactory; 140 } 141 142 146 public JbpmSession openJbpmSession() { 147 return openJbpmSession(null); 148 } 149 150 155 public JbpmSession openJbpmSession(Connection jdbcConnection) { 156 JbpmSession dbSession = null; 157 158 try { 159 Session session = null; 160 161 if ( jdbcConnection == null ) { 162 session = getSessionFactory().openSession(); 165 } else { 166 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 ( "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 197 public void evictCachedProcessDefinitions() { 198 sessionFactory.evict(ProcessDefinition.class); 199 } 200 201 204 public boolean isHibernatableWithLongId(Class clazz) { 205 if (hibernatableLongIdClasses==null) { 206 initHibernatableClasses(); 207 } 208 return hibernatableLongIdClasses.contains(clazz); 209 } 210 211 214 public boolean isHibernatableWithStringId(Class 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 (); 230 hibernatableStringIdClasses = new HashSet (); 231 Iterator 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 |