View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.db;
    19
    20  import java.sql.Connection;
    21  import java.sql.DriverManager;
    22  import java.sql.SQLException;
    23
    24
    25  /**
    26   * The DriverManagerConnectionSource is an implementation of {@link ConnectionSource}
    27   * that obtains the Connection in the traditional JDBC manner based on the
    28   * connection URL.
    29   * <p>
    30   * Note that this class will establish a new Connection for each call to
    31   * {@link #getConnection()}.  It is recommended that you either use a JDBC
    32   * driver that natively supported Connection pooling or that you create
    33   * your own implementation of {@link ConnectionSource} that taps into whatever
    34   * pooling mechanism you are already using.  (If you have access to a JNDI
    35   * implementation that supports {@link javax.sql.DataSource}s, e.g. within
    36   * a J2EE application server, see {@link JNDIConnectionSource}).  See
    37   * <a href="#dbcp">below</a> for a configuration example that uses the
    38   * <a href="http://jakarta.apache.org/commons/dbcp/index.html">commons-dbcp</a>
    39   * package from Apache.
    40   * <p>
    41   * Sample configuration:<br>
    42   * <pre>
    43   *     &lt;connectionSource class="org.apache.log4j.jdbc.DriverManagerConnectionSource"&gt;
    44   *        &lt;param name="driver" value="com.mysql.jdbc.Driver" /&gt;
    45   *        &lt;param name="url" value="jdbc:mysql://localhost:3306/mydb" /&gt;
    46   *        &lt;param name="username" value="myUser" /&gt;
    47   *        &lt;param name="password" value="myPassword" /&gt;
    48   *     &lt;/connectionSource&gt;
    49   *  </pre>
    50   * <p>
    51   * <a name="dbcp">If</a> you do not have another connection pooling mechanism
    52   * built into your application, you can use  the
    53   * <a href="http://jakarta.apache.org/commons/dbcp/index.html">commons-dbcp</a>
    54   * package from Apache:<br>
    55   * <pre>
    56   *     &lt;connectionSource class="org.apache.log4j.jdbc.DriverManagerConnectionSource"&gt;
    57   *        &lt;param name="driver" value="org.apache.commons.dbcp.PoolingDriver" /&gt;
    58   *        &lt;param name="url" value="jdbc:apache:commons:dbcp:/myPoolingDriver" /&gt;
    59   *     &lt;/connectionSource&gt;
    60   *  </pre>
    61   * Then the configuration information for the commons-dbcp package goes into
    62   * the file myPoolingDriver.jocl and is placed in the classpath.  See the
    63   * <a href="http://jakarta.apache.org/commons/dbcp/index.html">commons-dbcp</a>
    64   * documentation for details.
    65   *
    66   * @author <a href="mailto:rdecampo@twcny.rr.com">Ray DeCampo</a>
    67   */
    68  public class DriverManagerConnectionSource extends ConnectionSourceSkeleton {
    69      private String driverClass = null;
    70      private String url = null;
    71
    72      public void activateOptions() {
    73          try {
    74              if (driverClass != null) {
    75                  Class.forName(driverClass);
    76                  discoverConnnectionProperties();
    77              } else {
    78                  getLogger().error(
    79                      "WARNING: No JDBC driver specified for log4j DriverManagerConnectionSource.");
    80              }
    81          } catch (final ClassNotFoundException cnfe) {
    82              getLogger().error("Could not load JDBC driver class: " + driverClass, cnfe);
    83          }
    84      }
    85
    86
    87      /**
    88       * @see org.apache.log4j.db.ConnectionSource#getConnection()
    89       */
    90      public Connection getConnection() throws SQLException {
    91          if (getUser() == null) {
    92              return DriverManager.getConnection(url);
    93          } else {
    94              return DriverManager.getConnection(url, getUser(), getPassword());
    95          }
    96      }
    97
    98
    99      /**
    100      * Returns the url.
    101      *
    102      * @return String
    103      */
    104     public String getUrl() {
    105         return url;
    106     }
    107
    108
    109     /**
    110      * Sets the url.
    111      *
    112      * @param url The url to set
    113      */
    114     public void setUrl(String url) {
    115         this.url = url;
    116     }
    117
    118
    119     /**
    120      * Returns the name of the driver class.
    121      *
    122      * @return String
    123      */
    124     public String getDriverClass() {
    125         return driverClass;
    126     }
    127
    128
    129     /**
    130      * Sets the driver class.
    131      *
    132      * @param driverClass The driver class to set
    133      */
    134     public void setDriverClass(String driverClass) {
    135         this.driverClass = driverClass;
    136     }
    137 }