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