KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > CurrentPrevayler


1 package com.tirsen.nanning.samples.prevayler;
2
3 import org.prevayler.Prevayler;
4 import org.prevayler.util.clock.ClockedSystem;
5
6 public class CurrentPrevayler {
7
8     private static ThreadLocal JavaDoc isInTransaction = new ThreadLocal JavaDoc() {
9         protected Object JavaDoc initialValue() {
10             return new Integer JavaDoc(0);
11         }
12     };
13     private static ThreadLocal JavaDoc currentPrevayler = new InheritableThreadLocal JavaDoc();
14     private static ThreadLocal JavaDoc currentSystem = new InheritableThreadLocal JavaDoc();
15
16     public static boolean isInitialized() {
17         return currentSystem.get() != null;
18     }
19
20     public static Object JavaDoc getSystem() {
21         Object JavaDoc system;
22         if (hasPrevayler()) {
23             system = getPrevayler().prevalentSystem();
24         } else {
25             system = currentSystem.get();
26         }
27         assert system != null : "Prevayler not initialized for this thread, no current system";
28         return system;
29     }
30
31     public static void setSystem(Object JavaDoc system) {
32         currentSystem.set(system);
33         if (system != null) {
34             setPrevayler(null);
35         }
36     }
37
38     public static Prevayler getPrevayler() {
39         Prevayler prevayler = (Prevayler) currentPrevayler.get();
40         assert prevayler != null : "Prevayler not initialized for this thread, no current Prevayler";
41         return prevayler;
42     }
43
44     public static ClockedSystem clockedSystem() {
45         return (ClockedSystem) getSystem();
46     }
47
48     public static void setPrevayler(Prevayler prevayler) {
49         currentPrevayler.set(prevayler);
50         if (prevayler != null) {
51             setSystem(null);
52         }
53     }
54
55     public static boolean isReplaying() {
56         return getSystem() != null && getPrevayler() == null;
57     }
58
59     public static void enterTransaction() {
60         isInTransaction.set(new Integer JavaDoc(transactionCount() + 1));
61     }
62
63     public static void exitTransaction() {
64         assert isInTransaction() : "not in transaction";
65         isInTransaction.set(new Integer JavaDoc(transactionCount() - 1));
66     }
67
68     public static boolean isInTransaction() {
69         return transactionCount() != 0;
70     }
71
72     private static int transactionCount() {
73         return ((Integer JavaDoc) isInTransaction.get()).intValue();
74     }
75
76     public static void withPrevayler(Prevayler prevayler, final Runnable JavaDoc runnable) {
77         try {
78             withPrevayler(prevayler, new PrevaylerAction() {
79                 public Object JavaDoc run() throws Exception JavaDoc {
80                     runnable.run();
81                     return null;
82                 }
83             });
84         } catch (Exception JavaDoc e) {
85             throw new RuntimeException JavaDoc(e);
86         }
87     }
88
89     public static Object JavaDoc withPrevayler(Prevayler prevayler, PrevaylerAction action) throws Exception JavaDoc {
90         Prevayler lastPrevayler = (Prevayler) currentPrevayler.get();
91         setPrevayler(prevayler);
92         try {
93             return action.run();
94         } finally {
95             setPrevayler(lastPrevayler);
96         }
97     }
98
99     public static boolean hasPrevayler() {
100         return currentPrevayler.get() != null;
101     }
102
103     public static boolean hasSystem() {
104         return hasPrevayler() || currentSystem.get() != null;
105     }
106 }
107
Popular Tags