jetty: how to declare JVM scoped jndi entries and bind to java:comp/env
java, jetty, jndi
Solution
You can bind `prop/someValue` to `java:comp/env/prop/someValue` in two ways. The first one is to refer to the env-entry in the web.xml like this:
<env-entry>
<env-entry-name>prop/someValue</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>hello</env-entry-value>
</env-entry>
BUT, as in your case you don't have WEB-INF/web.xml which is not required for a valid web application according to J2EE. You can use `bindToENC` method to leave binding to Jetty. This can be done like that:
<Configure id="server" class="org.eclipse.jetty.server.Server">
<New class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg></Arg> <!-- Empty argument - scope set to JVM -->
<Arg>prop/someValue</Arg>
<Arg type="java.lang.String">hello</Arg>
<Arg type="boolean">true</Arg>
<Call name="bindToENC">
<Arg>prop/someValue</Arg>
</Call>
</New>
</Configure>
So your mistake was leaving `<Call>` block outside EnvEntry. Although keep in mind that calling `bindToENC` will work only in `jetty-env.xml` NOT in `context.xml` or `jetty.xml`.
MOREOVER, in order to make Jetty read `jetty-env.xml` file and include its contents you need to enable jndi and plus modules (necessarily both) like this `java -jar [jetty]/start.jar --add-to-startd=jndi,plus` (no spaces between modules) in Jetty 9, in previous versions you can find instruction here: http://wiki.eclipse.org/Jetty/Feature/JNDI#Detailed_Setup
Problem
I'm trying to define some JNDI entries in the Jetty server (they will be JVM scoped) but they aren't getting automatically bound to the "java:comp/env" namespace as expected. I'm following the documentation at: http://wiki.eclipse.org/Jetty/Feature/JNDI#Configuring_env-entries Here's my jetty.xml: ``` <Configure id="server" class="org.eclipse.jetty.server.Server"> <New class="org.eclipse.jetty.plus.jndi.EnvEntry"> <Arg></Arg> <Arg>prop/someValue</Arg> <Arg type="java.lang.String">hello</Arg> <Arg type="boolean">true</Arg> </New> </Configure> ``` I was expecting this to get bound to "java:comp/env/prop/someValue", as the Jetty example suggests, but "java:comp/env" doesn't appear to get created at all. However, a lookup on "prop/someValue" does work. Note- I don't have a webapp, so no WEB-INF/jetty-env.xml nor web.xml. Am just starting a Jetty server and trying to configure it with required JNDI entries. Have also tried to explicitly bind my entry to using the "bindToENC" method: ``` <Configure id="server" class="org.eclipse.jetty.server.Server"> <New class="org.eclipse.jetty.plus.jndi.EnvEntry"> <Arg></Arg> <Arg>prop/someValue</Arg> <Arg type="java.lang.String">hello</Arg> <Arg type="boolean">true</Arg> </New> <Call name="bindToENC"> <Arg>prop/someValue</Arg> </Call> </Configure> ``` But this results in server startup failure: ``` 2012-11-14 11:17:25,648 DEBUG - XML new class org.eclipse.jetty.plus.jndi.EnvEntry (org.eclipse.jetty.xml.XmlConfiguration) 2012-11-14 11:17:25,650 DEBUG - SAVE prop/someValue in null (jndi) 2012-11-14 11:17:25,656 DEBUG - InitialContextFactory.getInitialContext() (jndi) 2012-11-14 11:17:25,664 DEBUG - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@664883c (jndi) 2012-11-14 11:17:25,665 DEBUG - InitialContextFactory.getInitialContext() (jndi) 2012-11-14 11:17:25,665 DEBUG - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@6e811c88 (jndi) 2012-11-14 11:17:25,666 DEBUG - Looking up name="__" (jndi) 2012-11-14 11:17:25,666 DEBUG - Adding binding with key=__ obj=org.eclipse.jetty.jndi.NamingContext@39dd3812 for context=null as __: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@39dd3812 (jndi) 2012-11-14 11:17:25,666 DEBUG - Subcontext __ created (jndi) 2012-11-14 11:17:25,666 DEBUG - Looking up name="prop" (jndi) 2012-11-14 11:17:25,666 DEBUG - Adding binding with key=prop obj=org.eclipse.jetty.jndi.NamingContext@6a8c436b for context=__ as prop: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@6a8c436b (jndi) 2012-11-14 11:17:25,666 DEBUG - Subcontext prop created (jndi) 2012-11-14 11:17:25,667 DEBUG - Removing binding with key=someValue (jndi) 2012-11-14 11:17:25,667 DEBUG - Adding binding with key=someValue obj=prop/someValue for context=prop as someValue: org.eclipse.jetty.plus.jndi.EnvEntry:prop/someValue (jndi) 2012-11-14 11:17:25,667 DEBUG - Bound object to someValue (jndi) 2012-11-14 11:17:25,667 DEBUG - Looking up name="prop" (jndi) 2012-11-14 11:17:25,667 DEBUG - Adding binding with key=prop obj=org.eclipse.jetty.jndi.NamingContext@27b15692 for context=null as prop: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@27b15692 (jndi) 2012-11-14 11:17:25,667 DEBUG - Subcontext prop created (jndi) 2012-11-14 11:17:25,667 DEBUG - Removing binding with key=someValue (jndi) 2012-11-14 11:17:25,668 DEBUG - Adding binding with key=someValue obj=hello for context=prop as someValue: java.lang.String:hello (jndi) 2012-11-14 11:17:25,668 DEBUG - Bound object to someValue (jndi) 2012-11-14 11:17:25,668 DEBUG - XML call bindToENC (org.eclipse.jetty.xml.XmlConfiguration) 2012-11-14 11:17:25,668 DEBUG - InitialContextFactory.getInitialContext() (jndi) 2012-11-14 11:17:25,668 DEBUG - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@6baa9f99 (jndi) 2012-11-14 11:17:25,671 DEBUG - >>> new root context requested (org.eclipse.jetty.jndi.java.javaURLContextFactory) 2012-11-14 11:17:25,673 DEBUG - Adding binding with key=comp obj=Reference Class Name: javax.naming.Context Type: parser Content: org.eclipse.jetty.jndi.java.javaNameParser for context=null as comp: javax.naming.Reference:Reference Class Name: javax.naming.Context Type: parser Content: org.eclipse.jetty.jndi.java.javaNameParser (jndi) 2012-11-14 11:17:25,673 DEBUG - Looking up name="comp/env" (jndi) 2012-11-14 11:17:25,690 DEBUG - Using thread context classloader (jndi) 2012-11-14 11:17:25,690 DEBUG - No entry for classloader: sun.misc.Launcher$AppClassLoader@35a16869 (jndi) 2012-11-14 11:17:25,690 DEBUG - Looking up name="env" (jndi) 2012-11-14 11:17:25,690 WARN - Config error at <Call name="bindToENC"><Arg>prop/someValue</Arg></Call> (org.eclipse.jetty.xml.XmlConfiguration) 2012-11-14 11:17:25,690 WARN - Config error at <New class="org.eclipse.jetty.plus.jndi.EnvEntry"><Arg/><Arg>prop/someValue</Arg><Arg type="java.lang.String">hello</Arg><Arg type="boolean">true</Arg><Call name="bindToENC"><Arg>prop/someValue</Arg></Call></New> (org.eclipse.jetty.xml.XmlConfiguration) Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.eclipse.jetty.util.TypeUtil.call(TypeUtil.java:538) at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.call(XmlConfiguration.java:732) ``` Anyone know how to get this to work?