7

I'm having trouble getting some basic JNDI configuration going in Glassfish.

I have what I think ought to be a simple task: at run time, determine if a particular property is set to true or not. I think this is a good application of JNDI, but can't seem to get the path correct between the app server and my servlet code.

Here's how I have configured the property in Glassfish:

enter image description here

In my servlet code, I'm trying to look up the value with:

Boolean enabled = (Boolean) ctx.lookup("java:global/arizona/quartz_enabled");

In addition to this path, I've also tried the following without success:

  • java:global/arizona/arizona/quartz_enabled
  • java:module/arizona/quartz_enabled
  • java:module/arizona/arizona/quartz_enabled

My app is named "arizona", but deployed to the root context, if that matters.

I'm sure it's just a simple matter of figuring out the proper namespace to reach the property, but I feel like I'm just shooting in the dark trying to find it. Is there a simple way to browse the JNDI tree in Glassfish?

3 Answers 3

4

When looking up a JNDI resource created in the server, it's JNDI name is exactly what you entered as the name on the server. IE:

Boolean enabled = (Boolean)ctx.lookup("arizona");

For conventions on JNDI names and some example code on how to look everything up see this page:

http://www.javaworld.com/javaworld/jw-01-2000/jw-01-howto.html

1

In similar situations, I simply place a breakpoint where object (InitialContext in this case) is instantiated and evaluate it afterwards. IntelliJ IDEA has nice evaluator, not sure about other, arguably inferior, IDEs.

Btw, the correct prefix for all Java EE bindings is java:comp/env/, e.g. java:comp/env/arizona/quartz_enabled.

You might also want to look at this resource.

2
  • Hmm, that doesn't seem to work for me. I still get: Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/arizona/quartz_enabled' in SerialContext [Root exception is javax.naming.NameNotFoundException: No object bound to name java:comp/env/arizona/quartz_enabled] I'm also an Intellij user, and that evaluator is indeed nice, but unfortunately in this case you still seem to need to know the proper JNDI name. I didn't see anything "walkable" in the InitialContext that I could navigate through. Mar 12, 2011 at 22:40
  • In one of the appservers I needed to do debugging on (can't remember which exactly, t'was long time ago), the JNDI was implemented as a nested map, where keys are strings and values are either real (bound) values or maps (for nested paths). So I was able to cast it to a map and dig to the point I needed. InitialContext also has getEnvironment() method which returns Hashtable, so if Glassfish JNDI is not built on Map, it should still expose similar API. You can start from java:comp/env, see what's in it and dig until you find the answer.
    – mindas
    Mar 12, 2011 at 22:55
1

I can't make it work with javax.naming.InitialContext#lookup but injecting resource with

@Resource(name = "arizona/quartz_enabled")
private Boolean enabled;

works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.