Contents of jetty-web.xml in /WEB-INF:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Array id="plusConfig" type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> <!-- add for jndi -->
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item> <!-- add for jndi -->
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
</Array>
<Call class="java.lang.System" name="setProperty">
<Arg>java.naming.factory.initial</Arg>
<Arg><Property name="java.naming.factory.initial" default="org.eclipse.jetty.jndi.InitialContextFactory"/></Arg>
</Call>
<Call class="java.lang.System" name="setProperty">
<Arg>java.naming.factory.url.pkgs</Arg>
<Arg><Property name="java.naming.factory.url.pkgs" default="org.eclipse.jetty.jndi"/></Arg>
</Call>
</Configure>
Contents of jetty-plus.xml in /WEB-INF:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add plus Configuring classes to all webapps for this Server -->
<!-- =========================================================== -->
<Call class="org.eclipse.jetty.webapp.Configuration$ClassList" name="setServerDefault">
<Arg><Ref refid="Server" /></Arg>
<Call name="addAfter">
<Arg name="afterClass">org.eclipse.jetty.webapp.FragmentConfiguration</Arg>
<Arg>
<Array type="String">
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
</Array>
</Arg>
</Call>
</Call>
</Configure>
This is my calling code in Servlet's init() method:
Context _initCtx = new InitialContext();
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
Error it throws:
javax.naming.NameNotFoundException; remaining name 'env'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:449)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:551)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:117)
at javax.naming.InitialContext.lookup(Unknown Source)
I'm running vanilla GWT 2.7 and I'm trying to use JNDI lookup in my server code. I have also attached jetty-all-8.1.9.v20130131.jar and jetty-plus-8.1.9.v20130131.jar to the project since some of Context and JNDI classes are missing in GWT jars. I tried to verify the version of jetty in GWT, and I got this:
Server server = new Server(7070);
System.out.println(server.getVersion());
8.y.z-SNAPSHOT
I saw solution where you can configure JNDI other ways, however since I'm running in GWT, I can only work with the jetty-web.xml and jetty-plus.xml files.
When debugging the servlet, I'm getting context for:
Context _envCtx = (Context) _initCtx.lookup("java:comp");
But not for:
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
Stacktrace is above.
I'm really stuck here. Any jetty gurus out there?
Added jetty-env.xml :
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='portal' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DStest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="org.hsqldb.jdbcDriver">
<Set name="Url">jdbc:hsqldb:hsql://localhost</Set>
<Set name="User">sa</Set>
<Set name="Password"></Set>
</New>
</Arg>
</New>
</Configure>
And this to my web.xml:
<resource-ref>
<description>Primary database</description>
<res-ref-name>jdbc/DSTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Still _envCtx is null with the exception below. _initCtx.lookup("java:comp") does return org.eclipse.jetty.jndi.NamingContext instance.
Context _initCtx = new InitialContext();
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
results in
javax.naming.NameNotFoundException; remaining name 'env'
I must be missing something else (basic)....??!? I changed that 1 line in jetty-env.xml to:
<Arg><Ref refid="portal"/>jdbc/DSTest</Arg>
And the doctype:
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
I could not find more appropriate .dtd for v8.X Still same problem.
What's working for me (assuming you're not using SuperDevMode):
Add -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory
to the JVM parameters when you run DevMode. (in extraJvmArgs
for gwt-maven-plugin)
Add dependencies for Jetty Plus and JNDI to your POM:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jndi</artifactId>
<version>8.1.12.v20130726</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>8.1.12.v20130726</version>
<scope>provided</scope>
</dependency>
Have the JNDI entries configured in jetty-web.xml
:
<?xml version="1.0"?>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DataSource" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/XYZ</Arg>
<Arg>
<New class="org.postgresql.ds.PGSimpleDataSource">
<Set name="user">X</Set>
<Set name="password">Y</Set>
<Set name="databaseName">Z</Set>
<Set name="serverName">X</Set>
<Set name="portNumber">A</Set>
</New>
</Arg>
</New>
<New class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>config/ABC</Arg>
<Arg type="java.lang.String">Value</Arg>
</New>
<!-- ... -->
</Configure>
That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With