glenn       01/03/04 10:59:22

  Modified:    jdbc/src/org/apache/taglibs/jdbc/connection
                        ConnectionTag.java
  Log:
  Add support for JNDI named JDBC DataSource, contributed by Rich Catlett.
  
  Revision  Changes    Path
  1.3       +35 -2     
jakarta-taglibs/jdbc/src/org/apache/taglibs/jdbc/connection/ConnectionTag.java
  
  Index: ConnectionTag.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-taglibs/jdbc/src/org/apache/taglibs/jdbc/connection/ConnectionTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConnectionTag.java        2001/01/31 00:52:20     1.2
  +++ ConnectionTag.java        2001/03/04 18:59:21     1.3
  @@ -66,6 +66,9 @@
   import javax.servlet.jsp.tagext.BodyTagSupport;
   import javax.servlet.jsp.JspTagException;
   import javax.sql.DataSource;
  +import javax.naming.InitialContext;
  +import javax.naming.Context;
  +import javax.naming.NamingException;
   
   /**
    * <p>JSP tag connection, used to get a
  @@ -88,6 +91,11 @@
    *     &lt;rtexprvalue>false&lt;/rtexprvalue>
    *   &lt;/attribute>
    *   &lt;attribute>
  + *     &lt;name>jndiName&lt;/name>
  + *     &lt;required>false&lt;/required>
  + *     &lt;rtexprvalue>false&lt;/rtexprvalue>
  + *   &lt;/attribute>
  + *   &lt;attribute>
    *     &lt;name>dataSource</name>
    *     &lt;required>false</required>
    *     &lt;rtexprvalue>false</rtexprvalue>
  @@ -103,6 +111,7 @@
     private String _driver   = null;
     private String _userId   = null;
     private String _password = null;
  +  private String _jndiName = null;
   
     /**
      * Set the name of a javax.sql.DataSource page attribute
  @@ -136,6 +145,15 @@
     }
   
     /**
  +   * jndi named datasource used for connecting to the database via a jndi lookup
  +   *
  +   * @param jndiName jndi name for the jdbc datasourcer
  +   */
  +  public void setjndiName(String jndiName) {
  +    _jndiName = jndiName;
  +  } 
  +
  +  /**
      * User id for the database.  Optional if the user id
      * is already encoded in the URL, or if it is not
      * required by the database.
  @@ -174,14 +192,29 @@
   
     public int doEndTag() throws JspTagException{
   
  +    DataSource dataSource = null;
  +
       try {
         if (_driver != null) {
           Class.forName(_driver);
         }
   
         Connection conn = null;
  -      if (_dataSourceName != null) {
  -        DataSource dataSource = (DataSource) 
pageContext.findAttribute(_dataSourceName);
  +
  +      if (_jndiName != null) {
  +     try {
  +          Context ctx = new InitialContext();
  +       dataSource = (DataSource)ctx.lookup(_jndiName);
  +     } catch (NamingException ne) {
  +         throw new JspTagException(ne.toString());
  +     }
  +     if (_userId != null) {
  +          conn = dataSource.getConnection(_userId, _password);
  +        } else {
  +          conn = dataSource.getConnection();
  +        }
  +      } else if (_dataSourceName != null) {
  +        dataSource = (DataSource) pageContext.findAttribute(_dataSourceName);
           if (_userId != null) {
             conn = dataSource.getConnection(_userId, _password);
           } else {
  
  
  

Reply via email to