1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.javaflow.bytecode;
18
19 import org.apache.commons.javaflow.utils.ReflectionUtils;
20 import org.apache.commons.javaflow.ContinuationDeath;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25
26
27
28
29
30 public final class StackRecorder extends Stack {
31
32 private static final Log log = LogFactory.getLog(StackRecorder.class);
33 private static final long serialVersionUID = 2L;
34
35 private static final ThreadLocal threadMap = new ThreadLocal();
36
37
38
39
40
41
42
43
44
45
46 public transient boolean isRestoring = false;
47
48
49
50
51
52
53
54 public transient boolean isCapturing = false;
55
56
57 private transient Object context;
58
59
60
61
62 public StackRecorder( final Runnable pTarget ) {
63 super(pTarget);
64 }
65
66
67
68
69 public StackRecorder(final Stack pParent) {
70 super(pParent);
71 }
72
73 public static void suspend() {
74 log.debug("suspend()");
75
76 final StackRecorder stackRecorder = get();
77 if(stackRecorder == null) {
78 throw new IllegalStateException("No continuation is running");
79 }
80
81 stackRecorder.isCapturing = !stackRecorder.isRestoring;
82 stackRecorder.isRestoring = false;
83 }
84
85 public StackRecorder execute(final Object context) {
86 final StackRecorder old = registerThread();
87 try {
88 isRestoring = !isEmpty();
89 this.context = context;
90
91 if (isRestoring) {
92 log.debug("Restoring state of " + ReflectionUtils.getClassName(runnable) + "/" + ReflectionUtils.getClassLoaderName(runnable));
93 }
94
95 log.debug("calling runnable");
96 runnable.run();
97
98 if (isCapturing) {
99 if(isEmpty()) {
100
101
102
103 throw new IllegalStateException("stack corruption. Is "+runnable.getClass()+" instrumented for javaflow?");
104 }
105
106
107
108 popReference();
109 return this;
110 } else {
111 return null;
112 }
113 } catch(ContinuationDeath cd) {
114
115 throw cd;
116 } catch(Error e) {
117 log.error(e.getMessage(),e);
118 throw e;
119 } catch(RuntimeException e) {
120 log.error(e.getMessage(),e);
121 throw e;
122 } finally {
123 this.context = null;
124 deregisterThread(old);
125 }
126 }
127
128 public Object getContext() {
129 return context;
130 }
131
132
133
134
135 private StackRecorder registerThread() {
136 StackRecorder old = get();
137 threadMap.set(this);
138 return old;
139 }
140
141
142
143
144 private void deregisterThread(final StackRecorder old) {
145 threadMap.set(old);
146 }
147
148
149
150
151 public static StackRecorder get() {
152 return (StackRecorder)threadMap.get();
153 }
154 }