my goal ist to call secured (JAAS) Enterprise Java Beans (2.1) from a servlet (servlet and EJBs are running withing a JBoss 5 container).
Currently I create a new InitialContext for every request the servlet receives. With that InitialContext instance, I narrow the EJB home object from JNDI.
Now I dont want to create a new InitialContext for every single request. But every attempt to use a cached InitialContext resulted in a SecurityException (principal = null) on the EJB side.
As far as I know, the InitialContext is bound to its Thread it was created in. I tried to use a ThreadLocal varibale within my servlet, so every time I receive a request for my servlet,
I check if the ThreadLocal already contains a InitialContext instance. If not, I create a new instance and put it into the ThreadLocal. If the ThreadLocal contains an instance, I take that one.
Problem is: I can access my EJBs with the newly created InitialContext. With the one from the ThreadLocal it fails.
Is my approach basicly wrong? Is there another way to go? Any ideas or hints?
Any feedback would be highly appreciated.
Thanks a lot!
My code looks (very simplified) as follows. If the InitialContext from the ThreadLocal is null and a new InitialContext is created,
everything works well. Narrowing and accessing the homeObject succeeds. But if the InitialContext is not null and I try narrowing
the homeObject with the cached InitialContext from the ThreadLocal, I receive a SecurityException.
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems: