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     */
017    package org.apache.logging.log4j.core.appender.db.jdbc;
018    
019    import java.sql.Connection;
020    import java.sql.SQLException;
021    import javax.naming.InitialContext;
022    import javax.naming.NamingException;
023    import javax.sql.DataSource;
024    
025    import org.apache.logging.log4j.Logger;
026    import org.apache.logging.log4j.core.config.plugins.Plugin;
027    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
028    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
029    import org.apache.logging.log4j.status.StatusLogger;
030    
031    /**
032     * A {@link JDBCAppender} connection source that uses a {@link DataSource} to connect to the database.
033     */
034    @Plugin(name = "DataSource", category = "Core", elementType = "connectionSource", printObject = true)
035    public final class DataSourceConnectionSource implements ConnectionSource {
036        private static final Logger LOGGER = StatusLogger.getLogger();
037    
038        private final DataSource dataSource;
039        private final String description;
040    
041        private DataSourceConnectionSource(final String dataSourceName, final DataSource dataSource) {
042            this.dataSource = dataSource;
043            this.description = "dataSource{ name=" + dataSourceName + ", value=" + dataSource + " }";
044        }
045    
046        @Override
047        public Connection getConnection() throws SQLException {
048            return this.dataSource.getConnection();
049        }
050    
051        @Override
052        public String toString() {
053            return this.description;
054        }
055    
056        /**
057         * Factory method for creating a connection source within the plugin manager.
058         *
059         * @param jndiName The full JNDI path where the data source is bound. Should start with java:/comp/env or
060         *                 environment-equivalent.
061         * @return the created connection source.
062         */
063        @PluginFactory
064        public static DataSourceConnectionSource createConnectionSource(@PluginAttr("jndiName") final String jndiName) {
065            if (jndiName == null || jndiName.length() == 0) {
066                LOGGER.error("No JNDI name provided.");
067                return null;
068            }
069    
070            try {
071                final InitialContext context = new InitialContext();
072                final DataSource dataSource = (DataSource) context.lookup(jndiName);
073                if (dataSource == null) {
074                    LOGGER.error("No data source found with JNDI name [" + jndiName + "].");
075                    return null;
076                }
077    
078                return new DataSourceConnectionSource(jndiName, dataSource);
079            } catch (final NamingException e) {
080                LOGGER.error(e.getMessage(), e);
081                return null;
082            }
083        }
084    }