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

时间:2015-02-11 03:29:42

标签: gwt jetty jndi

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

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

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

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

抛出错误:

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)

我正在运行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

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

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

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

但不是为了:

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

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>

这是我的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>

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

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

结果

javax.naming.NameNotFoundException; remaining name 'env'

编辑2:

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

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

doctype:

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

我找不到更适合v8.X的.dtd 还是同样的问题。

4 个答案:

答案 0 :(得分:5)

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

  1. 运行DevMode时,将-Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory添加到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>
    
  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>
    
  4. 就是这样。

答案 1 :(得分:1)

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

您必须使用相同版本的Jetty依赖项,并GWT is using 8.1.12

答案 2 :(得分:1)

You need to have a <resource-ref> declared for this resource in your WEB-INF/web.xml too.

问题是,java:comp/env不会存在,直到:

  • 创建了一个引用(在您的webapp的特定jetty-env.xml或顶级服务器xml文件中)
  • 该引用绑定到webapp实例(不是服务器实例)。请参阅org.eclipse.jetty.plus.jndi.Resource声明中的第一个参数。
  • 您的网络应用程序WEB-INF/web.xml声明<resource-ref>指向此资源。

Jetty只是遵循这里的servlet规范。

对编辑#1的响应

Jetty 8 is EOL (End of Life),升级到Jetty 9.

这些说明适用于Jetty 9。

两件事......(就像上面原始答案中的链接一样)

  • 您的DOCTYPE错了。更新它。
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
  • 您没有绑定对webapp的引用,事实上,您的XML代码段已配置为绑定到JVM上下文。 (参数#1未指定/ 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>

答案 3 :(得分:0)

感谢Thomas提出的问题和答案。我有一个非常相似的情况。获得正确的配置组合非常困难,但我终于得到了它的工作。我想我也可以在这里记录我的发现。

我这样做是为了设置使用GWT 2.6的{​​{1}}。我的 目标是为Jetty 8.1.12v20130726连接池设置JNDI。我在跑步 位于DBCP2

GWT中的Eclipse

<强> WEB-INF /码头-web.xml中

我使用DevMode范围,但将值设置在JVM

另请注意,使用的名称为WebAppContext(即前缀为java:comp/env/jdbc/datalakeDbcp2

java:comp/env/

<强> JettyLauncher.java

<?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> here复制到您自己的工作中 目录(自然地在JettyLauncher.java的路径下)

只需要一次本地修改。

src/com/google/gwt/dev/shell/jetty/

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) { // }; - 只需使用它即可 作为原始代码:

WebAppClassLoaderExtension

<强>的pom.xml

private class WebAppClassLoaderExtension extends WebAppClassLoader {

  private static final String META_INF_SERVICES = "META-INF/services/";

  public WebAppClassLoaderExtension() throws IOException {
    super(bootStrapOnlyClassLoader, WebAppContextWithReload.this);
  }

虚拟机参数(<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>

Eclipse

<强> WEB-INF / web.xml中

也许是因为我在-Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory 中使用了VM范围,我做到了 无需更改我的jetty-web.xml

程序代码以获取名为连接的JNDI

web.xml

<强>参考

  1. 代码使用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); }; ,但我找不到匹配项 文档,所以我一直在使用#Jet; 9.3.x&#39;的文档。它 似乎仍然适用:
  2. Jetty v8.1.12.v20130726问题8507 here
相关问题