Jetty中使用JNDI设置环境变量
【目的】
Production使用WebSphere作为App Server,但主包方开发团队在Dev环境使用Tomcat binding to Eclipse,我更倾向于使用Mave Jetty Plugin——项目本身就是使用Maven进行依赖管理的!
【问题】
主包方开发团队要求在Tomcat的Server.xml中定义Naming Resource,用于访问WS-资源,如下:
* - server.xml
* Add the following inside XML Tag <GlobalNamingResources> :
<Environment name="wsPathPhq" override="false" type="java.lang.String" value="http://localhost:8080/cis2_ws/"/>
<Environment name="wsPathNtn" override="false" type="java.lang.String" value="http://localhost:8080/cis2_ws/"/>
<Environment name="defaultSite" override="false" type="java.lang.String" value="PHQ"/> <!-- PHQ or NTN -->
<Environment name="uiOffShorePath" override="false" type="java.lang.String" value="http://localhost:8080/cis2_ui/"/>
<Environment name="uiOffShoreName" override="false" type="java.lang.String" value="NTN"/>
然后在代码中使用javax.naming.InitialContext.lookup(String)进行查找。 但Jetty如何支持?
【解决方案】
在Jetty.xml中,加入如下部分:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg></Arg>
<Arg>wsPathPhq</Arg>
<Arg type="java.lang.String">http://localhost:8080/cis2_ws/</Arg>
<Arg type="boolean">true</Arg>
</New>
...
【参考文档】https://wiki.eclipse.org/Jetty/Feature/JNDI
Binding Objects into Jetty JNDI
You can bind four types of object into Jetty JNDI:
- An ordinary POJO instance.
- A java.naming.Reference instance.
- An object instance that implements the java.naming.Referenceable interface.
- A link between a name as referenced in
web.xmland as referenced in the environment.
The binding for all of these object types follows the same pattern:
<New class=type of naming entry>
<Arg>scope</Arg>
<Arg>name to bind as</Arg>
<Arg>the object to bind</Arg>
</New>
Defining Naming Entries
The type of naming entry can be:
"org.eclipse.jetty.plus.jndi.EnvEntry"for <env-entry>s."org.eclipse.jetty.plus.jndi.Resource"for all other type of resources."org.eclipse.jetty.plus.jndi.Transaction"for a JTA manager. For detailed information, see the Configuring XA Transactions section."org.eclipse.jetty.plus.jndi.Link"for link between aweb.xmlresource name and a NamingEntry. For more information, see the Configuring Links section.
You can define naming entries in three places:
jetty.xmlWEB-INF/jetty-env.xml- context xml file