title: Decorator Operation not allowed
body: <p>I have a decorator on an EJB (so this decorator is CDI if I understand it correct). I need do some business logic depending on the role the logged in user is. So in the EJB I have the SessionContext, but since this is an EJB object, I need to look it up via JNDI.</p>
  
  <pre><code>InitialContext ic = new InitialContext();
  SessionContext ctx = (SessionContext) ic.lookup("java:comp/EJBContext");
  </code></pre>
  
  <p>This works, but creates another error. When I try to call <code>ctx.isCallerInRole("MyRole");</code> I get this error:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: Operation not allowed
      at com.sun.ejb.containers.SessionContextImpl.checkAccessToCallerSecurity(SessionContextImpl.java:205)
      at com.sun.ejb.containers.EJBContextImpl.isCallerInRole(EJBContextImpl.java:447)
      at com.example.decorators.MyDecorator.findAll(MyDecorator.java:46)
  </code></pre>
  
  <p>What I expect is happening is that a CDI is not allowed to ask the SessionContext if the logged in user is in the specified role. Is there a way how I can solve this? I've been roaming Google for a few days now, without success.</p>
  
  <p>Erates</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>This is what I have at the moment:</p>
  
  <p>Interface:</p>
  
  <pre><code>@Local
  public interface StatelessSessionBeanLocal extends Serializable {
      &lt;T&gt; Collection&lt;T&gt; findAll(Class&lt;T&gt; clazz);
      boolean isCallerInRole(String role);
  }
  </code></pre>
  
  <p>EJB:</p>
  
  <pre><code>@Stateless
  public class StatelessSessionBean implements StatelessSessionBeanLocal {
      @Resource
      private SessionContext ctx;
  
      @Override
      public &lt;T&gt; Collection&lt;T&gt; findAll(Class&lt;T&gt; clazz){
         ...
      }
  
      @Override
      public boolean isCallerInRole(String role){
          return ctx.isCallerInRole(role);
      }
  }
  </code></pre>
  
  <p>Decorator:</p>
  
  <pre><code>@Decorator
  public abstract class StatelessSessionBeanDecorator implements StatelessSessionBeanLocal {
      @Inject
      @Delegate
      StatelessSessionBeanLocal sb;
  
      @Override
      public &lt;T&gt; Collection&lt;T&gt; findAll(Class&lt;T&gt; clazz){
          if (sb.isCallerInRole("TestRole")){
              return new ArrayList();
          } else {
              return sb.findAll(clazz);
          }
      }
  }
  </code></pre>
  
  <p>This gives me a NullPointerException at StatelessSessionBean.isCallerInRole pointing to the fact that the SessionContext is not injected. (I think because of the difference between SessionContext (EJB) and Inject (CDI)) Note, the EJB and Decorator are in different packages in different JARs in the EAR.</p>