Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(Initial)Context caching within servlet

 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you attaching any security principal within the InitialContext? What does that code look like?
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic