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  package org.apache.logging.log4j.core.appender.db.jdbc;
18  
19  import java.sql.Connection;
20  import java.sql.Driver;
21  import java.sql.DriverManager;
22  import java.sql.SQLException;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  import org.apache.logging.log4j.core.helpers.NameUtil;
29  import org.apache.logging.log4j.status.StatusLogger;
30  
31  /**
32   * A {@link JDBCAppender} connection source that uses a standard JDBC URL, username, and password to connect to the
33   * database.
34   */
35  @Plugin(name = "DriverManager", category = "Core", elementType = "connectionSource", printObject = true)
36  public final class DriverManagerConnectionSource implements ConnectionSource {
37      private static final Logger LOGGER = StatusLogger.getLogger();
38  
39      private final String databasePassword;
40      private final String databaseUrl;
41      private final String databaseUsername;
42      private final String description;
43  
44      private DriverManagerConnectionSource(final String databaseUrl, final String databaseUsername,
45                                            final String databasePassword) {
46          this.databaseUrl = databaseUrl;
47          this.databaseUsername = databaseUsername;
48          this.databasePassword = databasePassword;
49          this.description = "driverManager{ url=" + this.databaseUrl + ", username=" + this.databaseUsername
50                  + ", passwordHash=" + NameUtil.md5(this.databasePassword + this.getClass().getName()) + " }";
51      }
52  
53      @Override
54      public Connection getConnection() throws SQLException {
55          if (this.databaseUsername == null) {
56              return DriverManager.getConnection(this.databaseUrl);
57          }
58          return DriverManager.getConnection(this.databaseUrl, this.databaseUsername, this.databasePassword);
59      }
60  
61      @Override
62      public String toString() {
63          return this.description;
64      }
65  
66      /**
67       * Factory method for creating a connection source within the plugin manager.
68       *
69       * @param url The JDBC URL to use to connect to the logging database. A driver that can accept this URL must be on
70       *            the classpath.
71       * @param username The username with which to log in to the database, if applicable.
72       * @param password The password with which to log in to the database, if applicable.
73       * @return the created connection source.
74       */
75      @PluginFactory
76      public static DriverManagerConnectionSource createConnectionSource(@PluginAttr("url") final String url,
77                                                                         @PluginAttr("username") String username,
78                                                                         @PluginAttr("password") String password) {
79          if (url == null || url.length() == 0) {
80              LOGGER.error("No JDBC URL specified for the database.", url);
81              return null;
82          }
83  
84          Driver driver;
85          try {
86              driver = DriverManager.getDriver(url);
87          } catch (final SQLException e) {
88              LOGGER.error("No matching driver found for database URL [" + url + "].", e);
89              return null;
90          }
91  
92          if (driver == null) {
93              LOGGER.error("No matching driver found for database URL [" + url + "].");
94              return null;
95          }
96  
97          if (username == null || username.trim().length() == 0) {
98              username = null;
99              password = null;
100         }
101 
102         return new DriverManagerConnectionSource(url, username, password);
103     }
104 }