001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.db.jdbc;
018
019import java.sql.Connection;
020import java.sql.SQLException;
021
022import javax.naming.InitialContext;
023import javax.naming.NamingException;
024import javax.sql.DataSource;
025
026import org.apache.logging.log4j.Logger;
027import org.apache.logging.log4j.core.config.plugins.Plugin;
028import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
029import org.apache.logging.log4j.core.config.plugins.PluginFactory;
030import org.apache.logging.log4j.status.StatusLogger;
031import org.apache.logging.log4j.util.Strings;
032
033/**
034 * A {@link JdbcAppender} connection source that uses a {@link DataSource} to connect to the database.
035 */
036@Plugin(name = "DataSource", category = "Core", elementType = "connectionSource", printObject = true)
037public final class DataSourceConnectionSource implements ConnectionSource {
038    private static final Logger LOGGER = StatusLogger.getLogger();
039
040    private final DataSource dataSource;
041    private final String description;
042
043    private DataSourceConnectionSource(final String dataSourceName, final DataSource dataSource) {
044        this.dataSource = dataSource;
045        this.description = "dataSource{ name=" + dataSourceName + ", value=" + dataSource + " }";
046    }
047
048    @Override
049    public Connection getConnection() throws SQLException {
050        return this.dataSource.getConnection();
051    }
052
053    @Override
054    public String toString() {
055        return this.description;
056    }
057
058    /**
059     * Factory method for creating a connection source within the plugin manager.
060     *
061     * @param jndiName The full JNDI path where the data source is bound. Should start with java:/comp/env or
062     *                 environment-equivalent.
063     * @return the created connection source.
064     */
065    @PluginFactory
066    public static DataSourceConnectionSource createConnectionSource(@PluginAttribute("jndiName") final String jndiName) {
067        if (Strings.isEmpty(jndiName)) {
068            LOGGER.error("No JNDI name provided.");
069            return null;
070        }
071
072        try {
073            final InitialContext context = new InitialContext();
074            final DataSource dataSource = (DataSource) context.lookup(jndiName);
075            if (dataSource == null) {
076                LOGGER.error("No data source found with JNDI name [" + jndiName + "].");
077                return null;
078            }
079
080            return new DataSourceConnectionSource(jndiName, dataSource);
081        } catch (final NamingException e) {
082            LOGGER.error(e.getMessage(), e);
083            return null;
084        }
085    }
086}