简体   繁体   English

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

[英]jetty: how to declare JVM scoped jndi entries and bind to java:comp/env

I'm trying to define some JNDI entries in the Jetty server (they will be JVM scoped) but they aren't getting automatically bound to the "java:comp/env" namespace as expected. 我试图在Jetty服务器中定义一些JNDI条目(它们将在JVM范围内),但是它们并没有像预期的那样自动绑定到“ java:comp / env”命名空间。 I'm following the documentation at: http://wiki.eclipse.org/Jetty/Feature/JNDI#Configuring_env-entries 我在以下位置关注文档: http : //wiki.eclipse.org/Jetty/Feature/JNDI#Configuring_env-entries

Here's my jetty.xml: 这是我的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>

I was expecting this to get bound to "java:comp/env/prop/someValue", as the Jetty example suggests, but "java:comp/env" doesn't appear to get created at all. 正如Jetty示例所建议的那样,我期望将其绑定到“ java:comp / env / prop / someValue”,但是似乎根本没有创建“ java:comp / env”。

However, a lookup on "prop/someValue" does work. 但是,可以在“ prop / someValue”上进行查找。

Note- I don't have a webapp, so no WEB-INF/jetty-env.xml nor web.xml. 注意-我没有Web应用程序,因此没有WEB-INF / jetty-env.xml或web.xml。 Am just starting a Jetty server and trying to configure it with required JNDI entries. 我只是启动Jetty服务器并尝试使用必需的JNDI条目对其进行配置。

Have also tried to explicitly bind my entry to using the "bindToENC" method: 还尝试使用“ 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>

But this results in server startup failure: 但这导致服务器启动失败:

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)

Anyone know how to get this to work? 任何人都知道如何使它工作吗?

You can bind prop/someValue to java:comp/env/prop/someValue in two ways. 您可以通过两种方式将prop/someValue绑定到java:comp/env/prop/someValue The first one is to refer to the env-entry in the web.xml like this: 第一个是这样引用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>

BUT, as in your case you don't have WEB-INF/web.xml which is not required for a valid web application according to J2EE. 但是,根据您的情况,您没有WEB-INF / web.xml,根据J2EE,有效的Web应用程序不需要WEB-INF / web.xml。 You can use bindToENC method to leave binding to Jetty. 您可以使用bindToENC方法将绑定保留到Jetty。 This can be done like that: 可以这样做:

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

So your mistake was leaving <Call> block outside EnvEntry. 因此,您的错误是将<Call>块保留在EnvEntry之外。 Although keep in mind that calling bindToENC will work only in jetty-env.xml NOT in context.xml or jetty.xml . 尽管要记住,调用bindToENC仅在jetty-env.xmlcontext.xmljetty.xml jetty-env.xml

MOREOVER, in order to make Jetty read jetty-env.xml file and include its contents you need to enable jndi and plus modules (necessarily both) like this java -jar [jetty]/start.jar --add-to-startd=jndi,plus (no spaces between modules) in Jetty 9, in previous versions you can find instruction here: http://wiki.eclipse.org/Jetty/Feature/JNDI#Detailed_Setup 此外,为了使Jetty读取jetty-env.xml文件并包含其内容,您需要启用jndi和plus模块(必要时都启用),例如java -jar [jetty]/start.jar --add-to-startd=jndi,plus在以前的版本中,Jetty 9中的java -jar [jetty]/start.jar --add-to-startd=jndi,plus (模块之间没有空格)在以前的版本中可以找到说明: http : //wiki.eclipse.org/Jetty/Feature/JNDI#Detailed_Setup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM