KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > webapp > context > Context


1 package org.jbpm.webapp.context;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 public class Context {
7
8   static ThreadLocal JavaDoc contextsThreadLocal = new ThreadLocal JavaDoc();
9   
10   public static void create() {
11     contextsThreadLocal.set(new HashMap JavaDoc());
12   }
13   
14   public static void destroy() {
15     contextsThreadLocal.set(null);
16   }
17   
18   public static Object JavaDoc getContext(Class JavaDoc clazz) {
19     Map JavaDoc contexts = (Map JavaDoc) contextsThreadLocal.get();
20     Object JavaDoc context = contexts.get(clazz);
21     if (context==null) {
22       try {
23         context = clazz.newInstance();
24         contexts.put(clazz, context);
25       } catch (Exception JavaDoc e) {
26         throw new RuntimeException JavaDoc("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