2 Replies Latest reply on Feb 13, 2006 8:55 AM by christophe spada

    Pass object from an Interceptor to a Session bean.

    christophe spada Newbie

      Hi.
      Is it possible to instanciate some object in an interceptor and passing it to the current session bean method ?

      I tried to use the InvocationContext with the
      InvocationContext.getContextData().put("MyKey", "MyValue").
      But actually i can only use this InvocationContext in others interceptors.
      Is there a way to use it in the SessionBean back ? (injection ?)

      Thanks.
      Christophe.

        • 1. Re: Pass object from an Interceptor to a Session bean.
          Bill Burke Master

          sure.

          INvocationContext has a get/setParameters method.

          Also, if you *want* to use the COntextData, you could stuff it in a threadlocal:

          public class MyInterceptor {
           public static ThreadLocal contextData = new ThreadLocal();
          
           @AroundInvoke
           public Object interceptor(InvocationContext ctx) throws Exception {
           contextData.set(ctx.getContextData());
           try {
           return ctx.proceed();
           } finally {
           contextData.set(null); // clear the threadlocal
           }
          }
          

          In your SEssionBean code:
           {
           Map map = (Map)MyInterceptor.contextData.get();
           }
          



          • 2. Re: Pass object from an Interceptor to a Session bean.
            christophe spada Newbie

            Thanks, it works fine with this trick !

            But is it the only way to do that ?
            If a chain of interceptor are sharing and adding some information to the contextData of an InvocationContext, each of them (or the last one) should store the contextData in a ThreadLocal for the session bean ?
            In this case it could be a problem if the interceptor is removed for the call chain and if it's the one storing the ThreadLocal.

            Can we inject the InvocationContext in the session bean to access the context shared and updated by all the interceptors ?

            Thank you.
            Christophe.