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