01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.cflow;
05:
06: import java.util.Stack;
07:
08: /**
09: * An abstraction for the JIT gen cflow aspects.
10: * <p/>
11: * A concrete JIT gen cflow aspect *class* will be generated per
12: * cflow sub expression with a consistent naming scheme aka cflowID.
13: * <p/>
14: * The concrete cflow class will extends this one and implements two static methods.
15: * See the sample nested class.
16: * <p/>
17: * Note: the Cflow implements a real aspectOf singleton scheme and is not visible to Aspects.aspectOf
18: *
19: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
20: */
21: public abstract class AbstractCflowSystemAspect {
22:
23: //TODO do we really need a stack ? I think that an int increment wrapped in a ThreadLocal
24: // will be ok. The stack might only be needed for perCflow deployments
25: public ThreadLocal m_cflowStackLocal = new ThreadLocal() {
26: protected Object initialValue() {
27: return new Stack();
28: }
29: };
30:
31: /**
32: * before advice when entering this cflow
33: */
34: public void enter() {
35: ((Stack) m_cflowStackLocal.get()).push(Boolean.TRUE);
36: }
37:
38: /**
39: * after finally advice when exiting this cflow
40: */
41: public void exit() {
42: ((Stack) m_cflowStackLocal.get()).pop();
43: }
44:
45: /**
46: * @return true if in the cflow
47: */
48: public boolean inCflow() {
49: return ((Stack) m_cflowStackLocal.get()).size() > 0;
50: }
51:
52: /**
53: * Sample jit cflow aspect that will gets generated.
54: * Note that we need to test the INSTANCE in case the cflow subexpression
55: * was out of the scope of the weaver (else we gets NullPointerExceptions)
56: *
57: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
58: */
59: private static class Cflow_sample extends AbstractCflowSystemAspect {
60:
61: private static Cflow_sample INSTANCE = null;
62:
63: private Cflow_sample() {
64: super ();
65: }
66:
67: /**
68: * this method will be invoked by the JIT joinpoint
69: */
70: public static boolean isInCflow() {
71: if (INSTANCE == null) {
72: return false;
73: }
74: return INSTANCE.inCflow();
75: }
76:
77: /**
78: * Real aspectOf as a singleton
79: */
80: public static Cflow_sample aspectOf() {
81: if (INSTANCE == null) {
82: INSTANCE = new Cflow_sample();
83: }
84: return INSTANCE;
85: }
86:
87: }
88:
89: }
|