Search code examples
tomcatjndi

How to config java.util.Properties as JNDI in Tomcat


How to configure a properties file content into Tomcat to recover by JNDI? like a DataSource but in this case it's a Properties.

In Jetty server I can configure this way:

 <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>

In Glassfish server I can configure this way:

<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, there are a custom or in the box implementation?


Solution

  • So, I create an factory for to access a Properties as JNDI resource in tomcat. Properties as JNDI

    In <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" />
    

    in web.xml file

    <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>
    

    in context.xml file

    <Context path="/myapp" reloadable="true">
    
      <ResourceLink name="props/myDS"  global="props/myDS" type="java.util.Properties" />