Package com.discursive.dao.generic.spring

Source Code of com.discursive.dao.generic.spring.LifecycleCallbackInterceptor

package com.discursive.dao.generic.spring;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import org.springframework.aop.IntroductionInterceptor;

import com.discursive.dao.generic.GenericDao;
import com.discursive.dao.generic.LifecycleCallbacks;

public class LifecycleCallbackInterceptor implements IntroductionInterceptor {

    private static Logger log = Logger.getLogger( LifecycleCallbackInterceptor.class );

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        Object returnObject = null;
        String methodName = methodInvocation.getMethod().getName();
        Object[] arguments = methodInvocation.getArguments();
        if (methodName.startsWith("makePersistent")) {
            Object object = arguments[0];
            log.debug( "Intercepting call to make persistent for " + object.toString() );
           
            if( object instanceof LifecycleCallbacks ) {
                LifecycleCallbacks lifecycle = (LifecycleCallbacks) object;
                if( lifecycle.getId() != null ) {
                    lifecycle.beforeUpdate();
                } else {
                    lifecycle.beforeCreate();
                }
                lifecycle.beforeSave();
               
            } else {
                log.debug( "Object doesn't implement lifecycle callbacks, not running before.." );
            }
           
            returnObject = methodInvocation.proceed();

            if( object instanceof LifecycleCallbacks ) {
                LifecycleCallbacks lifecycle = (LifecycleCallbacks) object;
                if( lifecycle.getId() != null ) {
                    lifecycle.afterUpdate();
                } else {
                    lifecycle.afterCreate();
                }
                lifecycle.afterSave();
            } else {
                log.debug( "Object doesn't implement lifecycle callbacks, not running after.." );
            }
           
        } else if( methodName.startsWith( "makeTransient" ) ) {
            Object object = arguments[0];
            log.debug( "Intercepting call to make transient for " + object.toString() );

            if( object instanceof LifecycleCallbacks ) {
                ((LifecycleCallbacks) object).beforeDestroy();               
            } else {
                log.debug( "Object doesn't implement lifecycle callbacks, not running before.." );
            }

            returnObject = methodInvocation.proceed();

            if( object instanceof LifecycleCallbacks ) {
                ((LifecycleCallbacks) object).afterDestroy();                               
            } else {
                log.debug( "Object doesn't implement lifecycle callbacks, not running after.." );
            }
           
        } else {
            returnObject = methodInvocation.proceed();
        }

        return returnObject;
    }

    @SuppressWarnings("unchecked")
  public boolean implementsInterface(Class intf) {
        return intf.isInterface() && GenericDao.class.isAssignableFrom(intf);
    }
}
TOP

Related Classes of com.discursive.dao.generic.spring.LifecycleCallbackInterceptor

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.