简体   繁体   中英

Session objects into Seam Interceptors

once more i'm here asking help on seam subject.

Currently we have the following interceptor for audit

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Interceptors(LoggingInterceptor.class)
public @interface IAuditavel {

}

and the interceptor itself

private EntityManager em;

    @Logger
    private Log logger;

    @In(required = false)
    Usuario usuario;

    @AroundInvoke
    public Object aroundInvoke(InvocationContext ctx) throws Exception {
        if (ctx.getMethod().isAnnotationPresent(IAuditavel.class) || isInterceptorEnabled()) {
            // Inicializa o EM fora do escopo do SEAM
            em = (EntityManager) Component.getInstance("entityManager");

            // Entidade para logging
            LogEntidade entidade = new LogEntidade();

            // Chave 0
            entidade.setIdLog(new BigDecimal(0));

            // Metodo chamado
            entidade.setAcao( ctx.getTarget().getClass().getSimpleName() + "." + ctx.getMethod().getName() );

            // Usuario logado no momento
            entidade.setUsuario( usuario );

            // Parametros
            Object[] params = ctx.getParameters();
            StringBuilder sb = new StringBuilder("");

            for (Object o : params){
                sb.append(o + ", "); 
            }

            // Data da execução
            entidade.setDataAlteracao(new Date());

            // Salva e desconecta a entidade
            em.persist(entidade);
            em.flush();

            // Põe os valores da entidade no log do jboss
            saveToServerLog(entidade);
        }

        // Continua a execução do método interceptado
        return ctx.proceed();
    }

    /***
     * Retorna true caso a classe / método seja anotada com o nosso interceptor
     */
    public boolean isInterceptorEnabled() {
        return getComponent().beanClassHasAnnotation(IAuditavel.class);
    }

    public void saveToServerLog(LogEntidade entidade) {
        if (logger.isInfoEnabled()) {
            logger.info("> " + entidade.getDataAlteracao() + ":"
                    + entidade.getAcao() + " com os parametros : "
                    + entidade.getParametros());
        }
    }

I presume the

@In(required = false)
    Usuario usuario;

won't work because seam domain don't get into the interceptor. So how do I inject a session atribute setted on the login method as:

 @In(required = false)
    @Out(scope = ScopeType.SESSION, required = false)
    Usuario usuario;

on the authenticator class.

Thanks in advance.

The answer need was:

// Inicializa o EM fora do escopo do SEAM
em = (EntityManager) Component.getInstance("entityManager");

// Recupera o usuário logado
usuario = (Usuario) Contexts.getSessionContext().get("usuario");

both off injection

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM