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.SQLException;
21  import java.util.Objects;
22  
23  import javax.naming.NamingException;
24  import javax.sql.DataSource;
25  
26  import org.apache.logging.log4j.Logger;
27  import org.apache.logging.log4j.core.config.plugins.Plugin;
28  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
30  import org.apache.logging.log4j.core.net.JndiManager;
31  import org.apache.logging.log4j.status.StatusLogger;
32  import org.apache.logging.log4j.util.Strings;
33  
34  /**
35   * A {@link JdbcAppender} connection source that uses a {@link DataSource} to connect to the database.
36   */
37  @Plugin(name = "DataSource", category = "Core", elementType = "connectionSource", printObject = true)
38  public final class DataSourceConnectionSource implements ConnectionSource {
39      private static final Logger LOGGER = StatusLogger.getLogger();
40  
41      private final DataSource dataSource;
42      private final String description;
43  
44      private DataSourceConnectionSource(final String dataSourceName, final DataSource dataSource) {
45          this.dataSource = Objects.requireNonNull(dataSource, "dataSource");
46          this.description = "dataSource{ name=" + dataSourceName + ", value=" + dataSource + " }";
47      }
48  
49      @Override
50      public Connection getConnection() throws SQLException {
51          return this.dataSource.getConnection();
52      }
53  
54      @Override
55      public String toString() {
56          return this.description;
57      }
58  
59      /**
60       * Factory method for creating a connection source within the plugin manager.
61       *
62       * @param jndiName The full JNDI path where the data source is bound. Must start with java:/comp/env or environment-equivalent.
63       * @return the created connection source.
64       */
65      @PluginFactory
66      public static DataSourceConnectionSource createConnectionSource(@PluginAttribute("jndiName") final String jndiName) {
67          if (!JndiManager.isJndiJdbcEnabled()) {
68              LOGGER.error("JNDI must be enabled by setting log4j2.enableJndiJdbc=true");
69              return null;
70          }
71          if (Strings.isEmpty(jndiName)) {
72              LOGGER.error("No JNDI name provided.");
73              return null;
74          }
75          try {
76              @SuppressWarnings("resource")
77              final DataSource dataSource = JndiManager.getDefaultManager(DataSourceConnectionSource.class.getCanonicalName()).lookup(jndiName);
78              if (dataSource == null) {
79                  LOGGER.error("No DataSource found with JNDI name [" + jndiName + "].");
80                  return null;
81              }
82              return new DataSourceConnectionSource(jndiName, dataSource);
83          } catch (final NamingException e) {
84              LOGGER.error(e.getMessage(), e);
85              return null;
86          }
87      }
88  }