Package de.petris.dynamicaspects.classhandler

Source Code of de.petris.dynamicaspects.classhandler.CFlowClassHandler

/*
* 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.classhandler;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.bcel.classfile.JavaClass;

import de.petris.dynamicaspects.Advice;
import de.petris.dynamicaspects.AdviceFactory;
import de.petris.dynamicaspects.AspectException;
import de.petris.dynamicaspects.BeforeAfterAdvice;
import de.petris.dynamicaspects.CFlowCondition;
import de.petris.dynamicaspects.WeaveType;
import de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice;

/**
* A classhandler which handles the installation of cflow-restricted advices. The original
* advice is wrapped by a {@link de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice CFlowBeforeAfterAdvice }.
* This advice is installed by the original classhandler. All given 
* {@link de.petris.dynamicaspects.CFlowCondition CFlowConditions} are installed as
* {@link de.petris.dynamicaspects.helperadvices.CFlowConditionAdvice CFlowConditionAdvices}.
*
* The execution of the original advice is restricted by these conditions.
*
* @author Marco Petris
*
* @see de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice
* @see de.petris.dynamicaspects.helperadvices.CFlowConditionAdvice
* @see de.petris.dynamicaspects.CFlow
* @see de.petris.dynamicaspects.CFlowCondition
*/
public class CFlowClassHandler implements ClassHandler {

    // the classhandler which installs the advice
    private ClassHandler innerClassHandler;
    // the conditions which restrict the advice execution
    private List<CFlowCondition> conditions;
   
    // keys are the original advices and values are the wrapped around CFlowBeforeAfterAdvices
    private Map<Advice, Advice> adviceMapping;
   
    // a mapping targetclassname -> classhandler
    private static Map<String, CFlowClassHandler> targetClass_ClassHandlerMapping
        = new HashMap<String, CFlowClassHandler>();
   
   
    /**
     * Returns a classhandler for a given target class.
     *
     * @param targetClassName the name of the target class
     * @param weaveType the weaveType for the wrapped advice
     * @param conditions the conditions to restrict the execution of the wrapped advice
     * @return the classhandler for the given target class
     */
    public static CFlowClassHandler getClassHandler(
            String targetClassName, WeaveType weaveType,
            List<CFlowCondition> conditions ) {
       
        // if no classhandler has been created yet for this class,
        // we are creating it now
        if( !targetClass_ClassHandlerMapping.containsKey( targetClassName ) ) {
          
            // install a classhandler for the given target class
            targetClass_ClassHandlerMapping.put(
                targetClassName,
                new CFlowClassHandler(
                    weaveType.getClassHandler( targetClassName ), // get the inner class handler
                    conditions ) );
        }
       
        return targetClass_ClassHandlerMapping.get( targetClassName );
    }
   
    /**
     * Creates an instance of this class.
     *
     * @param innerClassHandler the classHandler for the wrapped advice
     * @param conditions the conditions to restrict the wrapped advice.
     */
    CFlowClassHandler(
            ClassHandler innerClassHandler,
            List<CFlowCondition> conditions ) {
       
        this.innerClassHandler = innerClassHandler;
        this.conditions = conditions;
        this.adviceMapping = new HashMap<Advice,Advice>();
    }
   
    /**
     * Adds an advice. The given advice is wrapped by a
     * {@link de.petris.dynamicaspects.helperadvices.CFlowBeforeAfterAdvice CFlowBeforeAfterAdvice }.
     *
     * @param advice
     * @return the newly created wrapper advice
     */
    Advice addAdvice( BeforeAfterAdvice advice ) {
        // create the wrapper advice
        CFlowBeforeAfterAdvice cflowAdvice =
            new CFlowBeforeAfterAdvice(
                    advice,
                    conditions );
        // create mapping
        adviceMapping.put( advice, cflowAdvice );
       
        return cflowAdvice;
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#deinstall(de.petris.dynamicaspects.Advice)
     */
    public void deinstall(Advice advice) {
        innerClassHandler.deinstall( adviceMapping.get( advice ) );
        adviceMapping.remove( advice );
        if( adviceMapping.isEmpty() ) {
            deinstallConditions();
        }
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#deinstall(java.lang.Class)
     */
    public void deinstall(Class< ? extends Advice> aspectClass) {
       
        Iterator<Map.Entry<Advice,Advice>> iter =
            adviceMapping.entrySet().iterator();
       
        while( iter.hasNext() )  {
            Map.Entry<Advice,Advice> entry = iter.next();
            if( entry.getKey().getClass().getName().equals(
                    aspectClass.getName() ) ) {
                innerClassHandler.deinstall( entry.getValue() );
                iter.remove();
            }
        }
        if( adviceMapping.isEmpty() ) {
            deinstallConditions();
        }
    }
   
    /**
     * Deinstalls all conditions.
     */
    private void deinstallConditions() {
        for( CFlowCondition condition : conditions ) {
            condition.deinstall();
        }
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#install(de.petris.dynamicaspects.Advice, java.util.regex.Pattern)
     */
    public void install(Advice advice, Pattern joinpointPattern) {
        innerClassHandler.install(
            addAdvice( (BeforeAfterAdvice)advice ), joinpointPattern);
       
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#install(de.petris.dynamicaspects.AdviceFactory, java.util.regex.Pattern)
     */
    public void install(
            final AdviceFactory factory, Pattern joinpointPattern) {
        innerClassHandler.install(
            new AdviceFactory() {
               
                /* (non-Javadoc)
                 * @see de.petris.dynamicaspects.AdviceFactory#getAdvice()
                 */
                public Advice getAdvice() {
                    BeforeAfterAdvice inner = (BeforeAfterAdvice)factory.getAdvice();
                    return addAdvice( inner );
                }
                /* (non-Javadoc)
                 * @see de.petris.dynamicaspects.AdviceFactory#getAdviceClassName()
                 */
                public String getAdviceClassName() {
                    return CFlowBeforeAfterAdvice.class.getName();
                }
            }, joinpointPattern );
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#install(java.lang.Class, java.util.regex.Pattern)
     */
    public void install( final Class< ? extends Advice> aspectClass,
            Pattern joinpointPattern) {
        innerClassHandler.install(
            new AdviceFactory() {
                /* (non-Javadoc)
                 * @see de.petris.dynamicaspects.AdviceFactory#getAdvice()
                 */
                public Advice getAdvice() {
                    try {
                        BeforeAfterAdvice inner =
                            (BeforeAfterAdvice)aspectClass.newInstance();
                        return addAdvice( inner );
                    }
                    catch( Exception exc ) {
                        throw new AspectException( exc );
                    }
                }
                /* (non-Javadoc)
                 * @see de.petris.dynamicaspects.AdviceFactory#getAdviceClassName()
                 */
                public String getAdviceClassName() {
                    return CFlowBeforeAfterAdvice.class.getName();
                }
            }, joinpointPattern );
       
    }
   
    /* (non-Javadoc)
     * @see de.petris.dynamicaspects.classhandler.ClassHandler#setTargetClass(org.apache.bcel.classfile.JavaClass)
     */
    public void setTargetClass(JavaClass targetclass) {
        innerClassHandler.setTargetClass(targetclass);
    }
}
TOP

Related Classes of de.petris.dynamicaspects.classhandler.CFlowClassHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.