简体   繁体   English

无法获得码头来阅读jetty-env.xml

[英]Can't get jetty to read jetty-env.xml

I have the following: 我有以下内容:

Startup code: 启动代码:

    Server server = new Server(8080);

    WebAppContext context = new WebAppContext();

    context.setDescriptor("/WEB-INF/web.xml");
    context.setResourceBase("/home/webapp);
    context.setContextPath("/");
    context.setParentLoaderPriority(true);

    server.setHandler(context);

    server.start();

jetty-env.xml in /home/webapp/WEB-INF: / home / webapp / WEB-INF中的jetty-env.xml:

 <?xml version="1.0"?> 
 <Configure class="org.mortbay.jetty.webapp.WebAppContext">
   <New id="properties"  class="org.mortbay.jetty.plus.naming.EnvEntry">
     <Arg>property_file</Arg>
     <Arg>/home/webapp/web.properties</Arg>
   </New>  
 </Configure>

jndi.properties in classpath: 类路径中的jndi.properties:

java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory

The initial context is created OK, but attempting to lookup the property_file key throws name not found exception. 初始上下文创建成功,但是尝试查找property_file键会引发未找到名称的异常。 Enumeration only returns the keys defined in jndi.properties. 枚举仅返回jndi.properties中定义的键。

Context initContext = new InitialContext();
Hashtable<?,?> ht = initContext.getEnvironment();
Enumeration<?> keys = ht.keys();
while(keys.hasMoreElements()) {
    Object key = keys.nextElement();
    System.out.println(key.toString() + "=" + ht.get(key).toString());
}

String props=(String)initContext.lookup("java:comp/env/property_file");

What am I doing wrong? 我究竟做错了什么?

You have a bad mix of jetty versions. 您的码头版本混合使用不当。

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="properties"  class="org.mortbay.jetty.plus.naming.EnvEntry">

That points to Jetty 6 use, and ... 这表明了Jetty 6的使用,并且...

java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory

points to Jetty 9 use... 指向Jetty 9使用...

This is what is messing you up. 这就是让你烦恼的事情。 The XML you are using will not work on Jetty 9 (Only Jetty 6), and the properties file you are defining should never be defined manually for Jetty 9 (its present in the jetty-jndi-{ver}.jar ) 您使用的XML在Jetty 9(仅Jetty 6)上不起作用,并且永远不要为Jetty 9手动定义您定义的属性文件(在jetty-jndi-{ver}.jar

$ jar -tvf lib/jetty-jndi-9.2.9.v20150224.jar | grep jndi.properties
125 Tue Feb 24 10:49:42 MST 2015 jndi.properties

The instructions for Jetty 9 are found at 有关Jetty 9的说明,请参见

https://www.eclipse.org/jetty/documentation/current/jndi-embedded.html https://www.eclipse.org/jetty/documentation/current/jndi-embedded.html

This is how you setup Embedded Jetty with JNDI support. 这就是设置具有JNDI支持的Embedded Jetty的方式。 There's some parts you need to enable in the WebAppContext configuration, and that should enable the jetty-env.xml to be used. 您需要在WebAppContext配置中启用某些部分,并且应该启用jetty-env.xml

Namely ... 即...

// Enable parsing of jndi-related parts of web.xml and jetty-env.xml
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
    "org.eclipse.jetty.plus.webapp.EnvConfiguration",
    "org.eclipse.jetty.plus.webapp.PlusConfiguration");

Finally, the jetty-env.xml structure and syntax are documented here 最后,在此处记录了jetty-env.xml结构和语法

https://www.eclipse.org/jetty/documentation/current/jndi-configuration.html https://www.eclipse.org/jetty/documentation/current/jndi-configuration.html

So your example would look like this ... 所以你的例子看起来像这样...

<?xml version="1.0"?> 
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="properties" class="org.eclipse.jetty.plus.jndi.EnvEntry">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>property_file</Arg>
    <Arg>/home/webapp/web.properties</Arg>
    <Arg type="boolean">true</Arg>
  </New>
</Configure>

Yes, the DOCTYPE is important. 是的,DOCTYPE很重要。

As for the 4 parameters on EnvEntry, those are documented at the APIDOC site for EnvEntry . 关于EnvEntry上的4个参数,这些参数在EnvEntry的APIDOC站点上记录

One last piece of advice, DO NOT USE jetty-all.jar for your project, it exists only to allow some basic command line experimentation with Jetty, it is not meant to be used as a dependency in your project. 最后一条建议,不要在您的项目中使用jetty-all.jar ,它的存在只是允许对Jetty进行一些基本的命令行试验,而不是要在您的项目中用作依赖项。

My apologies, I'm new to jetty and I forgot to mention that I was using embedded mode. 抱歉,我是Jetty的新手,但我忘了提到我使用的是嵌入式模式。 By now I've figured out that in the embedded mode jetty does NOT automatically read jetty-env.xml. 到目前为止,我已经发现,在嵌入式模式下,jetty不会自动读取jetty-env.xml。 You have to do it explicitly, for example: 您必须明确地执行此操作,例如:

    Resource jettyEnv = Resource.newSystemResource("jetty-env.xml");
    XmlConfiguration conf = new XmlConfiguration(jettyEnv.getInputStream());
    Object obj = conf.configure();
    WebAppContext context = (WebAppContext)obj;

The response about mixing different jetty versions does still apply. 关于混合不同码头版本的响应仍然适用。

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

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