1 package org.jbpm.webapp.context; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 public class Context { 7 8 static ThreadLocal contextsThreadLocal = new ThreadLocal (); 9 10 public static void create() { 11 contextsThreadLocal.set(new HashMap ()); 12 } 13 14 public static void destroy() { 15 contextsThreadLocal.set(null); 16 } 17 18 public static Object getContext(Class clazz) { 19 Map contexts = (Map ) contextsThreadLocal.get(); 20 Object context = contexts.get(clazz); 21 if (context==null) { 22 try { 23 context = clazz.newInstance(); 24 contexts.put(clazz, context); 25 } catch (Exception e) { 26 throw new RuntimeException ("couldn't instantiate context '"+clazz.getName()+"'"); 27 } 28 } 29 return context; 30 } 31 32 public static PersistenceContext getPersistenceContext() { 33 return (PersistenceContext) getContext(PersistenceContext.class); 34 } 35 36 public static BpmContext getBpmContext() { 37 return (BpmContext) getContext(BpmContext.class); 38 } 39 } 40 | Popular Tags |