Subversion Repositories bacoAlunos

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2062 es 1
package pt.estgp.es.exemplos.hibernate.servicecontrol;
2
 
3
import org.aspectj.lang.ProceedingJoinPoint;
4
import org.aspectj.lang.annotation.Around;
5
import org.aspectj.lang.annotation.Aspect;
6
import org.aspectj.lang.annotation.Pointcut;
7
import pt.estgp.es.exemplos.hibernate.HibernateUtils;
8
 
9
@Aspect
10
public class ControloServicos
11
{
12
    //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications
13
    //The pointcut will look for the @YourAnnotation
14
    @Pointcut("@annotation(ServiceAnnotation)")
15
    public void serviceAnnotationPointCutDefinition(){}
16
 
17
    //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications
18
    //The pointcut is a catch all pointcut with the scope of execution
19
    @Pointcut("execution(* *(..))")
20
    public void atExecution(){}
21
 
22
    //A junção de vários point cuts num só criando um conjunto de condições lógicas
23
    @Pointcut("atExecution() && serviceAnnotationPointCutDefinition()")
24
    public void chamadaServico() {}
25
 
26
    @Around("chamadaServico()")
27
    public 5+0%2Fdocs%2Fapi+Object">Object envolventeServico(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
28
    {
29
        HibernateUtils.getCurrentSession().beginTransaction();
30
        //Sugestão: um motor de serviços poderia neste ponto abrir a transação na base de dados
31
        1.5.0/docs/api/java/lang/System.html">System.out.println("Iniciando chamada do servico:" + pjp.getSignature().getName() +
32
                " na classe " + pjp.getSourceLocation().getClass().getName());
33
        try {
34
            5+0%2Fdocs%2Fapi+Object">Object returnObj = pjp.proceed();
35
            //Sugestão: um motor de serviços poderia neste ponto fazer commit da transação na base de dados
36
 
37
            1.5.0/docs/api/java/lang/System.html">System.out.println("Terminado chamada do servico");
38
            HibernateUtils.getCurrentSession().getTransaction().commit();
39
            return returnObj;
40
        } catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) {
41
            //Sugestão: um motor de serviços poderia neste ponto fazer rollback da transação na base de dados
42
            1.5.0/docs/api/java/lang/System.html">System.out.println("Excepcao no Servico");
43
 
44
        HibernateUtils.getCurrentSession().getTransaction().rollback();
45
        }
46
 
47
        return 0;
48
    }
49
}