/*
* Copyright (c) 2005, Marco Petris
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of Marco Petris nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package de.petris.dynamicaspects;
import java.util.regex.Pattern;
import de.petris.dynamicaspects.helperadvices.CFlowConditionAdvice;
/**
* A condition which restricts the execution of an advice.
* An {@link de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice CFlowBeforeAfterAdvice}
* is installed for every condition. This special advices monitor a certain pointcut for passing
* threads. The condition is fulfilled if a thread passes the pointcut.<br>
* ( If you set the pass argument of the constructors of this class to false,
* the condition is fulfilled for a thread as long as it does not pass the pointcut )
*
* @author Marco Petris
* @see de.petris.dynamicaspects.CFlow
* @see de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice
*/
public class CFlowCondition {
// target package for the condition advices
private Package conditionTargetPackage;
// target classes for the conditio advices
private Class[] conditionTargetClasses;
private Pattern pointcut;
// flag if true the condition is fulfilled for a thread if it passes the pointcut
private boolean pass;
// the advice which monitors the condition
private CFlowConditionAdvice advice;
/**
* Creates an instance of this class.
*
* @param conditionTargetPackage A target package for the conditions advices.
* @param pointcut the pointcut which shall be passed or not passed.
* @param pass if true the condition is fulfilled for a thread if the thread passes the pointcut in the target
* package, if false the condition is fulfilled for a thread until the thread passes the pointcut
* in the target package.
*
*/
public CFlowCondition( Package conditionTargetPackage, Pattern pointcut, boolean pass ) {
this.conditionTargetPackage = conditionTargetPackage;
this.pointcut = pointcut;
this.pass = pass;
}
/**
* Creates an instance of this class.
*
* @param conditionTargetClass A target class for the conditions advices.
* @param pointcut the pointcut which shall be passed or not passed.
* @param pass if true the condition is fulfilled for a thread if the thread passes the pointcut in the target
* class, if false the condition is fulfilled for a thread until the thread passes the pointcut
* in the target class.
*
*/
public CFlowCondition( Class conditionTargetClass, String pointcut, boolean pass ) {
this( new Class[]{ conditionTargetClass }, Pattern.compile( pointcut ), pass );
}
/**
* Creates an instance of this class.
*
* @param conditionTargetClasses target classes for the conditions advices.
* @param pointcut the pointcut which shall be passed or not passed.
* @param pass if true the condition is fulfilled for a thread if the thread passes the pointcut in the target
* classes, if false the condition is fulfilled for a thread until the thread passes the pointcut
* in the target classes.
*
*/
public CFlowCondition( Class[] conditionTargetClasses, Pattern pointcut, boolean pass ) {
this.conditionTargetClasses = conditionTargetClasses;
this.pointcut = pointcut;
this.pass = pass;
}
/**
* Installs the conditions advice and returns it.
*
* @return the advice which monitors the condition.
*/
CFlowConditionAdvice install() {
// create the advice
advice =
new CFlowConditionAdvice( this, pass );
// install the advice
if( conditionTargetPackage != null ) {
AspectAgent.install( conditionTargetPackage, advice, pointcut, WeaveType.EXECUTION );
}
else {
for( Class c : conditionTargetClasses ) {
AspectAgent.install( c, advice, pointcut, WeaveType.EXECUTION );
}
}
return advice;
}
/**
* Deinstalls the conditions advice
*/
public void deinstall() {
if( conditionTargetPackage != null ){
AspectAgent.deinstall( conditionTargetPackage, advice );
}
else {
for( Class c : conditionTargetClasses ) {
AspectAgent.deinstall( c, advice );
}
}
}
/**
* Tests if this condition is fulfilled for the given thread.
* @param t the thread for which you want to test the condition
* @return true if the condition is fulfilled for the given thread, else false
*/
public boolean isFulfilled( Thread t) {
return advice.isFulfilled( t );
}
/**
* Clears the fulfilled-state of the given thread, setting it to initial value.
* The initial value is the negation of the pass-argument of the constructor.
*
* @param t the thread we want to clear the fulfilled-state.
*/
public void clearFulfilled( Thread t ) {
advice.clearFulfilled( t );
}
}