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