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