Subversion Repositories bacoAlunos

Rev

Blame | Last modification | View Log | RSS feed

package pt.estgp.es.exemplos.hibernate.servicecontrol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import pt.estgp.es.exemplos.hibernate.HibernateUtils;

@Aspect
public class ControloServicos
{
    //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications
    //The pointcut will look for the @YourAnnotation
    @Pointcut("@annotation(ServiceAnnotation)")
    public void serviceAnnotationPointCutDefinition(){}

    //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications
    //The pointcut is a catch all pointcut with the scope of execution
    @Pointcut("execution(* *(..))")
    public void atExecution(){}

    //A junção de vários point cuts num só criando um conjunto de condições lógicas
    @Pointcut("atExecution() && serviceAnnotationPointCutDefinition()")
    public void chamadaServico() {}

    @Around("chamadaServico()")
    public 5+0%2Fdocs%2Fapi+Object">Object envolventeServico(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        HibernateUtils.getCurrentSession().beginTransaction();
        //Sugestão: um motor de serviços poderia neste ponto abrir a transação na base de dados
        1.5.0/docs/api/java/lang/System.html">System.out.println("Iniciando chamada do servico:" + pjp.getSignature().getName() +
                " na classe " + pjp.getSourceLocation().getClass().getName());
        try {
            5+0%2Fdocs%2Fapi+Object">Object returnObj = pjp.proceed();
            //Sugestão: um motor de serviços poderia neste ponto fazer commit da transação na base de dados

            1.5.0/docs/api/java/lang/System.html">System.out.println("Terminado chamada do servico");
            HibernateUtils.getCurrentSession().getTransaction().commit();
            return returnObj;
        } catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) {
            //Sugestão: um motor de serviços poderia neste ponto fazer rollback da transação na base de dados
            1.5.0/docs/api/java/lang/System.html">System.out.println("Excepcao no Servico");
                       
        HibernateUtils.getCurrentSession().getTransaction().rollback();
        }

        return 0;
    }
}