9

I use Java EE 5. I wrote an interceptor for all EJBs with three methods for logging:

public class DefaultInterceptor {
    public static final String PREFIX = "!!!!!!!!!Interceptor:";

    @PostConstruct
    public void postConstruct(InvocationContext ctx) {
        try {
            System.out.println(PREFIX + " postConstruct");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @PreDestroy
    public void preDestroy(InvocationContext ctx) {
        try {
            System.out.println(PREFIX + " predestroy");
            System.out.println(PREFIX + "ctx.preceed=" + ctx.proceed());
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
        System.out.println(PREFIX + "method invocation '" + ctx.getMethod().getName() + "'");
        System.out.println(PREFIX + "parameters ='" + Arrays.deepToString(ctx.getParameters()) + "'");
        System.out.println(Arrays.deepToString(ctx.getContextData().keySet().toArray()));
        Object result = null;
        try {
            result = ctx.proceed();
            System.out.println(PREFIX + "Method result='" + result + "'");
            return result;
        } catch (Exception ex) {
            System.out.println(PREFIX + "Method exception ='" + ex.getMessage() + "'");
            throw ex;
        } finally {
            System.out.println(PREFIX + "Method finished");
        }
    }
} 

I want to get the name of EJB which called this interceptor. How can I do it?

I tried ctx.getMethod().getDeclaringClass().getSimpleName() but ctx.getMethod() returns null in postConstruct(-) and predestroy(-) methods.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
Balconsky
  • 2,234
  • 3
  • 26
  • 39
  • The `ctx.getMethod()` returns null or the `ctx` **is** null? – Piotr Nowicki Nov 30 '11 at 14:36
  • @Piotr Nowicki ctx.getMethod() returns always null in lifecycle callbacks. – Mikko Maunu Nov 30 '11 at 16:29
  • @MikkoMaunu yeah, I know that it's called by the container, not the method invoker. I was more interested that the lifecycle callback method can take `InvocationContext` parameter, but I did forgot that Interceptor ones can :-) – Piotr Nowicki Nov 30 '11 at 19:37

2 Answers2

4

For lifecycle callbacks ctx.getMethod() returns null. This is documented for example here: http://docs.oracle.com/javaee/5/api/javax/interceptor/InvocationContext.html

That is so, because it is not your EJB, but container who calls lifecycle callback methods.

If you are interested about association between interceptor and bean it belongs to, doesn't ctx.getTarget() method serve your purpose?

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • 1
    `context.getMethod()` actually returns the target method, not the invoker. And he asked "I want to get the name of EJB which called this interceptor". – Jaumzera Jun 22 '16 at 11:28
1

On WebLogic server you can use this in postConstructor, etc, to get the EJB name:

ctx.getTarget().getClass().getSuperclass().getName();
Pecenin
  • 11
  • 1