使基于上下文的JNDI变量可用于Eclipse中的Tomcat [英] Make per-context JNDI variable available to Tomcat in Eclipse

查看:125
本文介绍了使基于上下文的JNDI变量可用于Eclipse中的Tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse 4.6.1中使用Tomcat 8.5.6。我有一个Web应用程序项目/上下文 foo ,它的JAX-RS(使用RESTEasy 3.1.0.CR3)端点为 bar ,因此我可以在Eclipse中启动Tomcat并访问:

  http:// localhost:8080 / foo / bar 

我有一个名为 foobar 的变量我想使用JNDI在我的JAX-RS实现内部进行访问:

  final String foobar =(String)new InitialContext()。 lookup( java:comp / env / foobar);我计划使用Tomcat自动部署在生产环境中部署生产的WAR。我想在WAR外部为Tomcat配置 foobar 变量。我该怎么做才能在Eclipse中进行测试?



经过大量阅读后,我发现我认为是 $ Eclipse的CATALINA_HOME …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\ 。所以我在 foo 创建了一个上下文文件... \.metadata\.plugins\org.eclipse.wst.server.core\ tmp0\conf\Catalina\localhost\foo.xml 对应我的项目/上下文,并在其中添加以下内容:

 <?xml version = 1.0 encoding = UTF-8?> 
< Context>
<环境名称= foobar type = java.lang.String value = 123 />
< / Context>是的,我知道无论何时我重建时,Eclipse都会擦除此目录。但是在构建之后,我至少保存到文件中要查看它是否有效。没错我收到一个错误:

  javax.naming.NameNotFoundException:名称[foobar]在此上下文中未绑定。找不到[foobar]。 

我至少要使其正常工作,这样我才能知道如何在生产中做到这一点,并且省心稍后有关Eclipse中上下文文件删除的内容。那我做错了什么?为什么Tomcat在Eclipse中找不到此JNDI变量?



注意:我没有使用 web.xml 提出申请,并且不愿意这样做;此外,该变量应在生产部署中的WAR之外 定义。



更新:好消息是(在Windows 10 Professional Anniversary上64位版本)使用相同的Tomcat,但在独立模式下,我将相同的 foobar.xml 文件放入独立的Tomcat的 conf\Catalina\ \localhost\foo.xml ,然后我的JAX-RS应用程序将其选中。那么,如何在Eclipse中的Tomcat中定义JNDI变量进行测试?

解决方案

看来是为了获得Eclipse + Tomcat要识别每个模块的上下文文件,您必须进入服务器配置(在服务器上双击)并打开发布模块上下文以分离XML文件。这样,Tomcat将使用您创建的特定上下文XML文件。否则,显然会将它们放在 conf / server.xml 中,并忽略您创建的上下文特定文件。



在那里仍然是Eclipse每次进行重建时都会重新生成此文件的问题,这会破坏放置在该文件中的所有JNDI变量。我正在尝试在 https://stackoverflow.com/a/22380248/421049 中解决该问题,但是尚未成功。有人有更好的主意吗?



至少我现在能够复制生产环境---尽管是暂时的,直到下一次重建。


I'm using Tomcat 8.5.6 inside Eclipse 4.6.1. I have my web-app project/context foo, which has a JAX-RS (using RESTEasy 3.1.0.CR3) endpoint of bar, so I can fire up Tomcat inside Eclipse and access:

http://localhost:8080/foo/bar

I have a variable named foobar which I want to access inside my JAX-RS implementation using JNDI:

final String foobar = (String) new InitialContext().lookup("java:comp/env/foobar");

I plan on deploying the produced WAR in production using Tomcat autodeploy. I want to configure the foobar variable for Tomcat externally to the WAR. How can I do that so that I can test it in Eclipse?

After a lot of reading, I found what I thought to be the $CATALINA_HOME of Eclipse: …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\. So I created a context file for foo at …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\foo.xml to correspond to my project/context, and put the following inside it:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Environment name="foobar" type="java.lang.String" value="123"/>
</Context>

Yes, I know that Eclipse erases this directory whenever I rebuild. But after building, I saved to file at least want to see if it works. It doesn't. I get an error:

javax.naming.NameNotFoundException: Name [foobar] is not bound in this Context. Unable to find [foobar].

I want to at least get it working so I can know how to do this in production, and worry later about the context file deletion thing in Eclipse. So what did I do wrong? Why can't Tomcat in Eclipse find this JNDI variable?

Note: I am not using a web.xml file and have no desire to do so; besides, this variable should be defined outside the WAR in the production deployment.

Update: The good news is that (on Windows 10 Professional Anniversary Edition 64-bit) using the same Tomcat but in standalone mode, I put the same foobar.xml file inside the standalone Tomcat's conf\Catalina\localhost\foo.xml, and my JAX-RS application picked it up just fine. So how can I define a JNDI variable in Tomcat inside Eclipse for testing?

解决方案

It appears that in order to get Eclipse+Tomcat to recognize the per-module context files, you have to go into the server configuration (double-click on the server) and turn on the Publish module contexts to separate XML files. This way Tomcat will use the specific context XML file you created. Otherwise it apparently puts them in conf/server.xml and ignores the context-specific file you created.

There is still the problem that Eclipse regenerates this file each time you do a rebuild, destroying whatever JNDI variables you placed there. I'm trying to get the workaround in https://stackoverflow.com/a/22380248/421049 to work, but not yet succeeding. Anyone have any better ideas?

At least I'm able to reproduce a production environment now --- albeit temporarily, until the next rebuild.

这篇关于使基于上下文的JNDI变量可用于Eclipse中的Tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆