Source code: org/jbpm/workflow/delegation/impl/DbClassLoader.java
1 package org.jbpm.workflow.delegation.impl;
2
3 import java.util.*;
4 import net.sf.hibernate.type.*;
5 import org.apache.log4j.*;
6 import org.jbpm.util.db.*;
7
8 /**
9 * is used to load classes like Assigner's, Interaction's, Decision's
10 * from the database.
11 *
12 * @author Tom Baeyens
13 */
14 public class DbClassLoader extends ClassLoader {
15
16 private DbClassLoader( Long processDefinitionId ) {
17 super( DbClassLoader.class.getClassLoader() );
18 this.processDefinitionId = processDefinitionId;
19 }
20
21 public static DbClassLoader getInstance( Long processDefinitionId ) {
22 DbClassLoader instance = (DbClassLoader) classLoaderCache.get( processDefinitionId );
23 if ( instance == null ) {
24 instance = new DbClassLoader( processDefinitionId );
25 classLoaderCache.put( processDefinitionId, instance );
26 }
27
28 return instance;
29 }
30
31 private static final String queryFindClassFile =
32 "select cf " +
33 "from cf in class org.jbpm.workflow.delegation.impl.ClassFileImpl " +
34 "where ( cf.processDefinition.id = ? ) " +
35 " and ( cf.className = ? ) ";
36
37 protected Class findClass(String name) throws ClassNotFoundException {
38 log.debug( "getting class " + name + " from the database for process definition " + processDefinitionId );
39
40 ClassFileImpl classFile = null;
41
42 DbSession dbSession = null;
43 try {
44 dbSession = Db.openSession();
45 Object[] args = new Object[] { processDefinitionId, name };
46 Type[] types = new Type[] { DbType.LONG, DbType.STRING };
47 classFile = (ClassFileImpl) dbSession.findOne( queryFindClassFile, args, types );
48 log.debug( "DbClassLoader found class '" + name + "' for process definition '" + processDefinitionId + "' : " + classFile );
49
50 if ( classFile == null ) {
51 throw new Exception();
52 }
53
54 } catch (Throwable t) {
55 classFile = null;
56 t.printStackTrace();
57 throw new ClassNotFoundException( "delegation-exception : coudn't find class '" + name + "' for process definition '" + processDefinitionId + "'" );
58 } finally {
59 dbSession.close();
60 }
61
62 byte[] bytes = classFile.getBytes();
63 return defineClass(name, bytes, 0, bytes.length);
64 }
65
66 public Class loadClass(String name) throws ClassNotFoundException {
67 return super.loadClass( name );
68 }
69
70 private static Logger log = Logger.getLogger( DbClassLoader.class );
71 private static Map classLoaderCache = new HashMap();
72 private Long processDefinitionId = null;
73 }