Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

(Initial)Context caching within servlet

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!


Posts: 10445
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,
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic