jetty:如何声明JVM作用域的jndi条目并绑定到java:comp / env

时间:2012-11-14 11:21:07

标签: java jetty jndi

我正在尝试在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>

我希望这个绑定到“java:comp / env / prop / someValue”,正如Jetty示例所示,但“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)

任何人都知道如何让它发挥作用?

1 个答案:

答案 0 :(得分:2)

您可以通过两种方式将prop/someValue绑定到java: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应用程序不需要WEB-INF / web.xml。您可以使用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.xml不在context.xmljetty.xml

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