如何在Tomcat中将java.util.Properties配置为JNDI

时间:2018-04-24 12:47:58

标签: tomcat jndi

如何将属性文件内容配置到Tomcat以通过JNDI进行恢复?就像一个DataSource,但在这种情况下,它是Properties

Jetty服务器中我可以这样配置:

 <New id="props-users" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg>props/users</Arg>
  <Arg>
   <New class="java.util.Properties">
    <Put name="url">http://127.0.0.0:5984</Put>
    <Put name="schema">users</Put>
    <Put name="user">mary</Put>
    <Put name="password">secret</Put>
   </New>
  </Arg>
 </New>

Glassfish服务器中,我可以这样配置:

<custom-resource factory-class="org.glassfish.resources.custom.factory.PropertiesFactory" description="Properties to CouchDb enviroment" res-type="java.util.Properties" jndi-name="props/users">
   <property name="url" value="http://127.0.0.0:5984"></property>
   <property name="schema" value="users"></property>
   <property name="user" value="mary"></property>
   <property name="password" value="secret"></property>
 </custom-resource>

Tomcat,有自定义还是在框中实现?

1 个答案:

答案 0 :(得分:1)

因此,我创建了一个工厂,用于在tomcat中将Properties作为JNDI资源进行访问。 Properties as JNDI

<tomcat-install>/conf/server.xml

<GlobalNamingResources>

  <Resource auth="Container" description="Conexao clientes CouchDB" 
   name="props/myDS" 
   type="java.util.Properties"
   url="http://127.0.0.1:5984" 
   schema="mydatabase" 
   userName="admin" 
   password="secret123"
   factory="net.sf.jkniv.jaas.jndi.JndiPropertyFactory" />

web.xml文件中

<resource-ref>
  <description>properties for connection<description>
  <res-ref-name>props/myDS</res-ref-name>
  <res-type>java.util.Properties</res-type>
  <res-auth>Container</res-auth> 
  <mapped-name>props/myDS</mapped-name>
</resource-ref>

context.xml文件中

<Context path="/myapp" reloadable="true">

  <ResourceLink name="props/myDS"  global="props/myDS" type="java.util.Properties" />