001    
002    /*
003     * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: BigFatLockHeldAdvice.java 79 2011-04-20 01:50:09Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.servlet;
009    
010    import java.lang.reflect.Method;
011    
012    import org.springframework.aop.aspectj.AspectInstanceFactory;
013    import org.springframework.aop.aspectj.AspectJExpressionPointcut;
014    import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
015    
016    /**
017     * AspectJ method "before advice" that verifies that the {@link BigFatLock} is held.
018     */
019    public class BigFatLockHeldAdvice extends AspectJMethodBeforeAdvice {
020    
021        /**
022         * Constructor.
023         */
024        public BigFatLockHeldAdvice(Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
025            super(aspectJBeforeAdviceMethod, pointcut, aif);
026        }
027    
028        /**
029         * Verifies the {@link BigFatLock} is held. If not, {@link #lockNotHeld} is invoked.
030         */
031        @Override
032        public void before(Method method, Object[] args, Object target) {
033            if (!BigFatLock.isLockHeld())
034                this.lockNotHeld();
035        }
036    
037        /**
038         * Invoked in cases where the current thread does not hold the {@link BigFatLock}.
039         *
040         * <p>
041         * The implementation in {@link BigFatLockHeldAdvice} throws a {@link BigFatLockNotHeldException}.
042         * Subclasses can override to suit.
043         */
044        protected void lockNotHeld() {
045            throw new BigFatLockNotHeldException();
046        }
047    }
048