Setting java: comp / env context in Jetty running inside GWT 2.7

Content 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> 

Content 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 the Servlet init () method:

 Context _initCtx = new InitialContext(); Context _envCtx = (Context) _initCtx.lookup("java:comp/env"); 

Error:

 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 am running vanilla GWT 2.7 and I am trying to use JNDI search in my server code. I also attached the project jetty-all-8.1.9.v20130131.jar and jetty-plus-8.1.9.v20130131.jar to the project, since some of the Context and JNDI classes are not in the GWT banks. I tried to check the version of the pier in GWT, and I got the following:

 Server server = new Server(7070); System.out.println(server.getVersion()); 8.yz-SNAPSHOT 

I saw a solution in which you can configure other JNDI methods, however, since I work in GWT, I can only work with the jetty-web.xml and jetty-plus.xml files.

When debugging a servlet, I get context for:

 Context _envCtx = (Context) _initCtx.lookup("java:comp"); 

But not for:

 Context _envCtx = (Context) _initCtx.lookup("java:comp/env"); 

Stacktrace above.

I am really stuck here. Are there any berth gurus there?


Change 1:

Jetty-env.xml added:

 <?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 is 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> 

However, _envCtx is null with the exception below. _initCtx.lookup ("java: comp") returns an instance of org.eclipse.jetty.jndi.NamingContext.

 Context _initCtx = new InitialContext(); Context _envCtx = (Context) _initCtx.lookup("java:comp/env"); 

leads to

 javax.naming.NameNotFoundException; remaining name 'env' 

Edit 2:

I need to miss something else (main) .... ??!? I changed this 1 line in jetty-env.xml to:

 <Arg><Ref refid="portal"/>jdbc/DSTest</Arg> 

And doctype:

 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> 

I did not find a more suitable .dtd for v8.X Another problem.

+6
source share
4 answers

What works for me (if you are not using SuperDevMode):

  • Add -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory to the JVM options when starting DevMode. (in extraJvmArgs for gwt-maven-plugin)

  • Add the 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> 
  • Enter the JNDI entries 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> 

What is it.

+4
source

This is not supported, but it should work: https://code.google.com/p/google-web-toolkit/issues/detail?id=8526

However, you must use the same version of the Jetty dependencies, and GWT uses 8.1.12

+1
source

You also need to specify <resource-ref> for this resource in WEB-INF/web.xml .

The fact is that java:comp/env does not exist until:

  • Link created (either in your jetty-env.xml web application or in the top level xml file)
  • This link is tied to the webapp instance (and not to the server instance). See the first argument in the declaration of org.eclipse.jetty.plus.jndi.Resource .
  • Your webapp WEB-INF/web.xml declares a <resource-ref> pointing to this resource.

Jetty just follows the servlet specification.

Reply to edit # 1

Jetty 8 - EOL (End of Life) , upgrade to Jetty 9.

These instructions are for the Jetty 9.

Two things ... (like the link in the original answer above)

  • Your DOCTYPE invalid. Update it.
 <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> 
  • You did not bind your link to your webapp, in fact your XML fragment is configured to bind to the JVM context. (Argument # 1 not specified / null)
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//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><Ref refid="portal"/></Arg> <!-- this is where the resource is bound --> <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> 
+1
source

Thanks for the question and answer of Thomas. I had a very similar situation. It was very difficult to get the right combination of settings, but I finally got it to work. I guess I can document my results here.

I did this to configure GWT 2.6 , which uses Jetty 8.1.12v20130726 . my goal was to create JNDI for the DBCP2 connection DBCP2 . I am running GWT in Eclipse in DevMode

WEB-INF / web.xml pier-

I use the JVM scope but set the values ​​inside the WebAppContext

Please note that the name that is used is java:comp/env/jdbc/datalakeDbcp2 (i.e. with the java:comp/env/ prefix java:comp/env/ )

 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext"> <New id="datalakeDsDbcp2" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <!-- scope --> <Arg>java:comp/env/jdbc/datalakeDbcp2</Arg> <!-- name --> <Arg> <!-- value --> <New class="org.apache.commons.dbcp2.BasicDataSource"> <Set name="driverClassName">org.apache.hive.jdbc.HiveDriver</Set> <Set name="url">jdbc:hive2://someServer:somePort/someDb</Set> <Set name="username"></Set> <Set name="password"></Set> </New> </Arg> </New> </Configure> 

JettyLauncher.java

I copied a JettyLauncher.java from here to your own working one (along the path src/com/google/gwt/dev/shell/jetty/ naturally)

Only one local modification is required.

bootStrapOnlyClassLoader

 /* * Change suggested by: * https://groups.google.com/forum/#!topic/google-web-toolkit/SBFltI27ssk */ private final ClassLoader bootStrapOnlyClassLoader = Thread.currentThread().getContextClassLoader(); // private final ClassLoader bootStrapOnlyClassLoader = new ClassLoader(null) { // }; 

No need to change WebAppClassLoaderExtension - just use it as source code:

 private class WebAppClassLoaderExtension extends WebAppClassLoader { private static final String META_INF_SERVICES = "META-INF/services/"; public WebAppClassLoaderExtension() throws IOException { super(bootStrapOnlyClassLoader, WebAppContextWithReload.this); } 

pom.xml

 <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> 

VM arguments (in Eclipse )

 -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory 

WEB-INF / web.xml

Perhaps because I use the VM area in my jetty-web.xml , I did not need to change my web.xml

Program code to get JNDI named connection

 try { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("java:/comp/env/jdbc/datalakeDbcp2"); conn = ds.getConnection(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); }; 

References

  • Using Jetty v8.1.12.v20130726 code Jetty v8.1.12.v20130726 , but I could not find the appropriate documentation, so I used the document for "Jetty 9.3.x". This is still applicable:
  • GWT Problem 8507 is here
0
source

Source: https://habr.com/ru/post/982303/


All Articles