无法获得码头来阅读jetty-env.xml [英] Can't get jetty to read jetty-env.xml
问题描述
我有以下内容:
启动代码:
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();
/p/home/webapp/WEB-INF中的
jetty-env.xml:
jetty-env.xml in /home/webapp/WEB-INF:
<?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>
classpath中的jndi.properties:
jndi.properties in classpath:
java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory
初始上下文创建成功,但是尝试查找property_file键会引发未找到名称的异常.枚举仅返回jndi.properties中定义的键.
The initial context is created OK, but attempting to lookup the property_file key throws name not found exception. Enumeration only returns the keys defined in 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");
我在做什么错了?
推荐答案
您的码头版本混在一起.
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">
这表明Jetty 6的用途,并且...
That points to Jetty 6 use, and ...
java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory
使用Jetty 9的要点...
points to Jetty 9 use...
这就是让您感到困惑的原因.
您使用的XML在Jetty 9上不起作用(仅Jetty 6),并且永远不要为Jetty 9手动定义所定义的属性文件(它出现在jetty-jndi-{ver}.jar
中)
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
)
$ jar -tvf lib/jetty-jndi-9.2.9.v20150224.jar | grep jndi.properties
125 Tue Feb 24 10:49:42 MST 2015 jndi.properties
Jetty 9的说明位于
The instructions for Jetty 9 are found at
https://www.eclipse.org/jetty/documentation/current/jndi-embedded.html
这是设置具有JNDI支持的Embedded Jetty的方式.
您需要在WebAppContext
配置中启用某些部分,并且应该可以使用jetty-env.xml
.
This is how you setup Embedded Jetty with JNDI support.
There's some parts you need to enable in the WebAppContext
configuration, and that should enable the jetty-env.xml
to be used.
即...
// 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");
最后,此处记录了jetty-env.xml
的结构和语法
Finally, the jetty-env.xml
structure and syntax are documented here
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>
是的,DOCTYPE很重要.
Yes, the DOCTYPE is important.
As for the 4 parameters on EnvEntry, those are documented at the APIDOC site for EnvEntry.
最后一条建议,不要在您的项目中使用jetty-all.jar
,它的存在只是允许对Jetty进行一些基本的命令行试验,并不意味着可以在您的项目中将其用作依赖项.
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-env.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!