<?xml version="1.0" encoding="ISO-8859-1"?>
 <!--
   Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
  -->
<document>

 <properties>
  <title>JNDI Howto</title>
  <author email="dev@commons.apache.org">Commons Documentation Team</author>
 </properties>

 <body>

<section name="JNDI Howto">
<p>
  The <a href="http://java.sun.com/products/jndi/">Java Naming and Directory Interface</a> 
  (JNDI) is part of the Java platform, 
  providing applications based on Java technology with a unified interface to 
  multiple naming and directory services. You can build powerful and portable 
  directory-enabled applications using this industry standard.
</p>
<p>
  When you deploy your application inside an application server, the container will setup
  the JNDI tree for you. But if you are writing a framework or just a standalone application,
  then the following examples will show you how to construct and bind references to DBCP 
  datasources.
</p>
<p>
  The following examples are using the sun filesystem JNDI service provider.
  You can download it from the 
  <a href="http://java.sun.com/products/jndi/downloads/index.html">JNDI software download</a> page.
</p>
</section>

<section name="BasicDataSource">
<source><![CDATA[
  System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  System.setProperty(Context.PROVIDER_URL, "file:///tmp");
  InitialContext ic = new InitialContext();

  // Construct BasicDataSource reference
  Reference ref = new Reference("javax.sql.DataSource",
    "org.apache.commons.dbcp.BasicDataSourceFactory", null);
  ref.add(new StringRefAddr("driverClassName", "org.apache.commons.dbcp.TesterDriver"));
  ref.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
  ref.add(new StringRefAddr("username", "username"));
  ref.add(new StringRefAddr("password", "password"));
  ic.rebind("jdbc/basic", ref);
   
  // Use
  InitialContext ic2 = new InitialContext();
  DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
  assertNotNull(ds);
  Connection conn = ds.getConnection();
  assertNotNull(conn);
  conn.close();
]]></source>
</section>

<section name="PerUserPoolDataSource">
<source><![CDATA[
  System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  System.setProperty(Context.PROVIDER_URL, "file:///tmp");
  InitialContext ic = new InitialContext();

  // Construct DriverAdapterCPDS reference
  Reference cpdsRef = new Reference("org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS",
    "org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS", null);
  cpdsRef.add(new StringRefAddr("driver", "org.apache.commons.dbcp.TesterDriver"));
  cpdsRef.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
  cpdsRef.add(new StringRefAddr("user", "foo"));
  cpdsRef.add(new StringRefAddr("password", "bar"));
  ic.rebind("jdbc/cpds", cpdsRef);
     
  // Construct PerUserPoolDataSource reference
  Reference ref = new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
    "org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory", null);
  ref.add(new StringRefAddr("dataSourceName", "jdbc/cpds"));
  ref.add(new StringRefAddr("defaultMaxActive", "100"));
  ref.add(new StringRefAddr("defaultMaxIdle", "30"));
  ref.add(new StringRefAddr("defaultMaxWait", "10000"));
  ic.rebind("jdbc/peruser", ref);
     
  // Use
  InitialContext ic2 = new InitialContext();
  DataSource ds = (DataSource) ic2.lookup("jdbc/peruser");
  assertNotNull(ds);
  Connection conn = ds.getConnection("foo","bar");
  assertNotNull(conn);
  conn.close();
]]></source>
</section>

</body>
</document>
