5

我正在尝试在 Jetty 服务器中定义一些 JNDI 条目(它们将在 JVM 范围内),但它们没有按预期自动绑定到“java:comp/env”命名空间。我正在关注以下文档:http ://wiki.eclipse.org/Jetty/Feature/JNDI#Configuring_env-entries

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

正如 Jetty 示例所暗示的那样,我期待它绑定到“java:comp/env/prop/someValue”,但似乎根本没有创建“java:comp/env”。

但是,查找“prop/someValue”确实有效。

注意-我没有 webapp,所以没有 WEB-INF/jetty-env.xml 也没有 web.xml。我刚刚启动一个 Jetty 服务器并尝试使用所需的 JNDI 条目对其进行配置。

还尝试使用“bindToENC”方法显式绑定我的条目:

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

但这会导致服务器启动失败:

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)

有谁知道如何让它工作?

4

1 回答 1

3

您可以通过两种方式绑定prop/someValuejava:comp/env/prop/someValue。第一个是引用 web.xml 中的 env-entry,如下所示:

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

但是,在您的情况下,您没有 WEB-INF/web.xml,根据 J2EE,有效的 Web 应用程序不需要它。您可以使用bindToENC方法将绑定保留到 Jetty。这可以这样做:

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

所以你的错误是将<Call>块留在 EnvEntry 之外。尽管请记住,调用bindToENC仅适用于jetty-env.xmlNOT incontext.xmljetty.xml.

此外,为了让 Jetty 读取jetty-env.xml文件并包含其内容,您需要java -jar [jetty]/start.jar --add-to-startd=jndi,plus在 Jetty 9 中启用 jndi 和 plus 模块(必须两者)(模块之间没有空格),在以前的版本中,您可以在此处找到说明:http:// wiki.eclipse.org/Jetty/Feature/JNDI#Detailed_Setup

于 2014-10-27T17:56:38.283 回答