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 16 public class HibernateAdvice implements AroundAdvice, ThrowsAdvice { 17 private HibernateService _hib; 18 private Level _level = Level.READ; 19 private ThreadLocal _stackRef = new ThreadLocal (); 20 21 public HibernateAdvice() { 22 } 23 24 27 public void preInvoke(Invocation invocation) throws Throwable { 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 42 public void postInvoke(Invocation invocation) throws Throwable { 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 55 public void onThrows(Invocation invocation, Throwable thrown) 56 throws Throwable { 57 _hib.unregister(); 58 } 59 60 64 public void setLevel(String level) { 65 _level = Level.getLevelFor(level); 66 } 67 68 71 public void setHibernate(HibernateService hib) { 72 _hib = hib; 73 } 74 } 75 | Popular Tags |