Exception interception for logging, using AOP with CDI in Java EE 7

Asked

Viewed 84 times

1

Below I have part of a class, of which I want to eliminate repetitive logging code through AOP.

/**
 * Javadoc omitido
 */
public abstract class JpaDao<T extends LongIdentifiable> implements
        GenericDao<T> { 

    @Override
    @Transactional
    public Long persist(T type) throws GeneralPersistenceException {
        if (type == null) {
            throw new GeneralPersistenceException("Can't persist null");
        }

        try {
            entityManager.persist(type);
            return type.getId();
        } catch (PersistenceException e) {
            String message = "Failed to persist an entity";
            logger.error(message, e);
            throw new GeneralPersistenceException(message, e);
        }

    }

    @Override
    @Transactional
    public T merge(T type) throws GeneralPersistenceException {
        if (type == null) {
            throw new GeneralPersistenceException("Can't merge null");
        }

        try {
            T mergedType = entityManager.merge(type);
            return mergedType;
        } catch (PersistenceException e) {
            String message = "Failed to merge an entity";
            logger.error(message, e);
            throw new GeneralPersistenceException(message, e);
        }
    }
}

Note that the following piece of code is very repetitive in the methods persist and merge, in addition to which I believe that log writing can be treated elsewhere:

} catch (PersistenceException e) {
    String message = "Failed to persist an entity";
    logger.error(message, e);
    throw new GeneralPersistenceException(message, e);
}

The candidate code to replace the above is the below:

} catch (PersistenceException e) {
    throw new GeneralPersistenceException("Failed to persist an entity", e);
}

In other words, I’d like every time GeneralPersistenceException was launched, a log message was recorded. Below is my Exception:

public class GeneralPersistenceException extends Exception {
    private static final long serialVersionUID = -6057737927996676949L;

    public GeneralPersistenceException() {
    }

    public GeneralPersistenceException(String message) {
        super(message);
    }

    public GeneralPersistenceException(String message, Throwable cause) {
        super(message, cause);
    }

    public GeneralPersistenceException(Throwable cause) {
        super(cause);
    }
}

As you can see, it only has builders. Given this premise, I have the following doubts:

  1. The best strategy to solve the problem in a Java EE 7 application is AOP with CDI?
  2. If it’s AOP, I must intercept the GeneralPersistenceException?
  3. It is possible to intercept the builders of the GeneralPersistenceException, or only methods (which do not yet exist)?
  4. There’s already something ready for this and I’m reinventing the wheel?
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.