KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > hibernate > HibernateAdvice


1 package org.sapia.soto.hibernate;
2
3 import org.sapia.soto.aop.AroundAdvice;
4 import org.sapia.soto.aop.Invocation;
5 import org.sapia.soto.aop.ThrowsAdvice;
6
7
8 /**
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class HibernateAdvice implements AroundAdvice, ThrowsAdvice {
17   private HibernateService _hib;
18   private Level _level = Level.READ;
19   private ThreadLocal JavaDoc _stackRef = new ThreadLocal JavaDoc();
20
21   public HibernateAdvice() {
22   }
23
24   /**
25    * @see org.sapia.soto.aop.BeforeAdvice#preInvoke(org.sapia.soto.aop.Invocation)
26    */

27   public void preInvoke(Invocation invocation) throws Throwable JavaDoc {
28     CallStack stack = (CallStack) _stackRef.get();
29
30     if (stack == null) {
31       stack = new CallStack();
32       _stackRef.set(stack);
33       _hib.currentSession();
34     }
35
36     stack.acquire(_level);
37   }
38
39   /**
40    * @see org.sapia.soto.aop.AfterAdvice#postInvoke(org.sapia.soto.aop.Invocation)
41    */

42   public void postInvoke(Invocation invocation) throws Throwable JavaDoc {
43     CallStack stack = (CallStack) _stackRef.get();
44
45     if (stack.release()) {
46       if (stack.getLevel().getLevel() == Level.WRITE.getLevel()) {
47         _hib.currentSession().flush();
48       }
49     }
50   }
51
52   /**
53    * @see org.sapia.soto.aop.ThrowsAdvice#onThrows(org.sapia.soto.aop.Invocation, java.lang.Throwable)
54    */

55   public void onThrows(Invocation invocation, Throwable JavaDoc thrown)
56     throws Throwable JavaDoc {
57     _hib.unregister();
58   }
59
60   /**
61    * @param level the access level of the methods that are advised by this instance.
62    * Valid values are either: "read" or "write".
63    */

64   public void setLevel(String JavaDoc level) {
65     _level = Level.getLevelFor(level);
66   }
67
68   /**
69    * @param hib the Hibernate service that this instance uses.
70    */

71   public void setHibernate(HibernateService hib) {
72     _hib = hib;
73   }
74 }
75
Popular Tags