在GWT 2.7中运行的Jetty中设置上下文java:comp/env

use*_*921 7 gwt jndi jetty

/ WEB-INF中jetty-web.xml的内容:

<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>
Run Code Online (Sandbox Code Playgroud)

/ WEB-INF中jetty-plus.xml的内容:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我在Servlet的init()方法中的调用代码:

Context _initCtx = new InitialContext();
Context _envCtx  = (Context) _initCtx.lookup("java:comp/env");
Run Code Online (Sandbox Code Playgroud)

抛出错误:

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)
Run Code Online (Sandbox Code Playgroud)

我正在运行vanilla GWT 2.7,我正在尝试在我的服务器代码中使用JNDI查找.我还将jetty-all-8.1.9.v20130131.jar和jetty-plus-8.1.9.v20130131.jar附加到项目中,因为GWT jar中缺少一些Context和JNDI类.我试图在GWT中验证jetty的版本,我得到了这个:

Server server = new Server(7070);
System.out.println(server.getVersion());

8.y.z-SNAPSHOT
Run Code Online (Sandbox Code Playgroud)

我看到了可以在其他方式配置JNDI的解决方案,但是由于我在GWT中运行,我只能使用jetty-web.xml和jetty-plus.xml文件.

在调试servlet时,我得到的上下文是:

Context _envCtx  = (Context) _initCtx.lookup("java:comp");
Run Code Online (Sandbox Code Playgroud)

但不适用于:

Context _envCtx  = (Context) _initCtx.lookup("java:comp/env");
Run Code Online (Sandbox Code Playgroud)

Stacktrace就在上面.

我真的被困在这里了.那里有任何码头大师吗?


编辑1:

添加了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>
Run Code Online (Sandbox Code Playgroud)

这到我的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>
Run Code Online (Sandbox Code Playgroud)

仍然_envCtx为null,下面有例外._initCtx.lookup("java:comp")确实返回org.eclipse.jetty.jndi.NamingContext实例.

Context           _initCtx          = new InitialContext();
Context           _envCtx           = (Context) _initCtx.lookup("java:comp/env");
Run Code Online (Sandbox Code Playgroud)

结果是

javax.naming.NameNotFoundException; remaining name 'env'
Run Code Online (Sandbox Code Playgroud)

编辑2:

我一定是在遗漏别的东西(基本的).... ??!?我将jetty-env.xml中的那一行更改为:

<Arg><Ref refid="portal"/>jdbc/DSTest</Arg>
Run Code Online (Sandbox Code Playgroud)

和doctype:

<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
Run Code Online (Sandbox Code Playgroud)

我找不到更合适的.dtd for v8.X仍然是同样的问题.

Tho*_*mas 6

什么对我有用(假设你没有使用SuperDevMode):

  1. -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory运行DevMode时添加到JVM参数.(extraJvmArgs用于gwt-maven-plugin)

  2. 将Jetty Plus和JNDI的依赖项添加到您的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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. jetty-web.xml以下位置配置JNDI条目:

    <?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>
    
    Run Code Online (Sandbox Code Playgroud)

而已.


Tho*_*yer 1

这不是受支持的用法,但它应该可以工作:https://code.google.com/p/google-web-toolkit/issues/detail ?id=8526

不过,您必须使用完全相同版本的 Jetty 依赖项,并且GWT 使用的是 8.1.12