View Javadoc

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