my dog learned polymorphism*
The moose likes EJB and other Java EE Technologies and the fly likes (Initial)Context caching within servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "(Initial)Context caching within servlet" Watch "(Initial)Context caching within servlet" New topic
Author

(Initial)Context caching within servlet

Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hello ranchers,

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!

Regards,
Thomas

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Are you attaching any security principal within the InitialContext? What does that code look like?

[My Blog] [JavaRanch Journal]
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hi Jaikiran,

thanks for you reply!

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.



Best regards,
Thomas
 
jQuery in Action, 2nd edition
 
subject: (Initial)Context caching within servlet