martes, 27 de marzo de 2007

Interceptores Jboss Seam

Fases de JSF ya es mucha diferencia

Hay que crear una @interface que la utilizaremos para etiquetar/interceptar la clase(componente en terminos de Seam) correspondiente


import javax.interceptor.Interceptors;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

/**
* Created by IntelliJ IDEA.
* User: juanitu
* Date: 12-abr-2007
* Time: 17:43:24
* To change this template use File | Settings | File Templates.
*/
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Interceptors(Interceptor.class)
public @interface EtiquetaInterceptora {

}


Despues definimos el interceptor poniendo especial interes en como tratamos el tema de las fases de la aplicacion:



import org.jboss.seam.annotations.Around;
import org.jboss.seam.annotations.Within;
import org.jboss.seam.interceptors.*;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.Lifecycle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.faces.event.PhaseId;

import es.cafs.dao.CafsDAO;
import es.cafs.metadata.componentes.Componentes;

import java.lang.reflect.Method;

/**
* Created by IntelliJ IDEA.
* User: juanitu
* Date: 12-abr-2007
* Time: 17:45:36
* To change this template use File | Settings | File Templates.
*/
@Around({BijectionInterceptor.class, ValidationInterceptor.class,
ConversationInterceptor.class, BusinessProcessInterceptor.class})
@Within(RemoveInterceptor.class)
public class Interceptor {

protected static Log log = LogFactory.getLog(org.jboss.seam.interceptors.Interceptor.class);

@AroundInvoke
public Object checkLoggedIn(InvocationContext invocation) throws Exception {

CafsDAO dao = (CafsDAO) Contexts.getApplicationContext().get(Componentes.appCafsDAO);
log.error(dao==null);

Method method = invocation.getMethod();
log.error(invocation.getTarget().getClass()+"-"+method.getName() + " " + method.getReturnType());
if (Lifecycle.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
log.error("INVOCANDO FASE");
} else {
log.error(invocation.getTarget().getClass()+"-"+method.getName() + " " + method.getReturnType());
log.error("OTRA FASE");

}

return invocation.proceed();
}
}





Y por último hay que etiquetar el componente correspondiente


@Scope(ScopeType.CONVERSATION)
@Name(Componentes.convVisorInformacion)
@Logeando
public class VisorInformacion{
}

No hay comentarios: