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.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.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.CATEGORY_NAME, elementType = "connectionSource", printObject = true)
38  public final class DataSourceConnectionSource extends AbstractConnectionSource {
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 = 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. Should start with java:/comp/env or
63       *                 environment-equivalent.
64       * @return the created connection source.
65       */
66      @PluginFactory
67      public static DataSourceConnectionSource createConnectionSource(@PluginAttribute("jndiName") final String jndiName) {
68          if (Strings.isEmpty(jndiName)) {
69              LOGGER.error("No JNDI name provided.");
70              return null;
71          }
72  
73          try {
74              final InitialContext context = new InitialContext();
75              final DataSource dataSource = (DataSource) context.lookup(jndiName);
76              if (dataSource == null) {
77                  LOGGER.error("No data source found with JNDI name [" + jndiName + "].");
78                  return null;
79              }
80  
81              return new DataSourceConnectionSource(jndiName, dataSource);
82          } catch (final NamingException e) {
83              LOGGER.error(e.getMessage(), e);
84              return null;
85          }
86      }
87  }