Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion log4j-jdbc-dbcp2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>

<artifactId>log4j-jdbc-dbcp2</artifactId>
<name>Apache Log4j JDBC DBCP 2</name>
<name>Apache Log4j JDBC: DBCP 2 datasource</name>
<description>Connection source for the JDBC Appender using Apache Commons DBCP2.</description>

<properties>
Expand Down
1 change: 1 addition & 0 deletions log4j-jdbc-jndi/.log4j-plugin-processing-activator
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is here to activate the `plugin-processing` Maven profile.
109 changes: 109 additions & 0 deletions log4j-jdbc-jndi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>${revision}</version>
<relativePath>../log4j-parent</relativePath>
</parent>

<artifactId>log4j-jdbc-jndi</artifactId>
<name>Apache Log4j JDBC: JNDI datasource</name>
<description>Connection source for the JDBC Appender using JNDI.</description>

<dependencies>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jndi</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-plugins</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jndi-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

<!-- Mocking framework for use with JUnit -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.jdbc.appender.internal.JndiUtil;
import org.apache.logging.log4j.jdbc.appender.AbstractConnectionSource;
import org.apache.logging.log4j.jndi.JndiManager;
import org.apache.logging.log4j.plugins.Configurable;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.PluginAttribute;
Expand All @@ -29,11 +31,14 @@
import org.apache.logging.log4j.util.Strings;

/**
* A {@link JdbcAppender} connection source that uses a {@link DataSource} to connect to the database.
* A {@link org.apache.logging.log4j.jdbc.appender.JdbcAppender} connection source that uses a {@link DataSource} to connect to the database.
*/
@Configurable(elementType = "connectionSource", printObject = true)
@Plugin("DataSource")
public final class DataSourceConnectionSource extends AbstractConnectionSource {
private static final String DOC_URL =
"https://logging.staged.apache.org/log4j/3.x/manual/systemproperties.html#properties-jndi-support";
static final String JNDI_MANAGER_NAME = "org.apache.logging.log4j.jdbc.jndi.DataSourceConnectionSource";
private static final Logger LOGGER = StatusLogger.getLogger();

private final DataSource dataSource;
Expand Down Expand Up @@ -62,20 +67,27 @@ public String toString() {
*/
@PluginFactory
public static DataSourceConnectionSource createConnectionSource(@PluginAttribute final String jndiName) {
if (!JndiUtil.isJndiJdbcEnabled()) {
LOGGER.error("JNDI must be enabled by setting log4j2.enableJndiJdbc=true");
if (!JndiManager.isJndiJdbcEnabled()) {
LOGGER.error(
"JNDI must be enabled by setting `log4j.jndi.enableJdbc=\"true\"`\nSee {} for more details.",
DOC_URL);
return null;
}
if (Strings.isEmpty(jndiName)) {
LOGGER.error("No JNDI name provided.");
return null;
}

final DataSource dataSource = JndiUtil.getDataSource(jndiName);
if (dataSource == null) {
return null;
try {
final DataSource dataSource =
JndiManager.getDefaultManager(JNDI_MANAGER_NAME).lookup(jndiName);
if (dataSource != null) {
return new DataSourceConnectionSource(jndiName, dataSource);
}
LOGGER.error("Failed to retrieve JNDI data source with name {}.", jndiName);
} catch (final NamingException e) {
LOGGER.error("Failed to retrieve JNDI data source with name {}.", jndiName, e);
}

return new DataSourceConnectionSource(jndiName, dataSource);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

import static org.apache.logging.log4j.core.test.TestConstants.JNDI_ENABLE_JDBC;
import static org.junit.Assert.assertEquals;
Expand All @@ -34,14 +34,12 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.util.Throwables;
import org.apache.logging.log4j.jdbc.appender.internal.JndiUtil;
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
import org.h2.util.IOUtils;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
Expand All @@ -62,23 +60,19 @@ public static void beforeClass() {
protected AbstractJdbcAppenderDataSourceTest(final JdbcRule jdbcRule) {
this.rules = RuleChain.emptyRuleChain()
.around(new JndiRule(
JndiUtil.JNDI_MANAGER_NAME,
DataSourceConnectionSource.JNDI_MANAGER_NAME,
"java:/comp/env/jdbc/TestDataSourceAppender",
createMockDataSource()))
.around(jdbcRule)
.around(new LoggerContextRule("org/apache/logging/log4j/jdbc/appender/log4j2-data-source.xml"));
.around(new LoggerContextRule("AbstractJdbcAppenderDataSourceTest.xml"));
this.jdbcRule = jdbcRule;
}

private DataSource createMockDataSource() {
try {
final DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willAnswer(new Answer<Connection>() {
@Override
public Connection answer(final InvocationOnMock invocation) throws Throwable {
return jdbcRule.getConnectionSource().getConnection();
}
});
given(dataSource.getConnection()).willAnswer((Answer<Connection>)
invocation -> jdbcRule.getConnectionSource().getConnection());
return dataSource;
} catch (final SQLException e) {
Throwables.rethrow(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

import static org.apache.logging.log4j.core.test.TestConstants.JNDI_ENABLE_JDBC;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.BDDMockito.mock;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.jdbc.appender.internal.JndiUtil;
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -45,7 +44,7 @@ public static Object[][] data() {
return new Object[][] {{"java:/comp/env/jdbc/Logging01"}, {"java:/comp/env/jdbc/Logging02"}};
}

private static final String CONFIG = "log4j-fatalOnly.xml";
private static final String CONFIG = "DataSourceConnectionSourceTest.xml";

@Rule
public final RuleChain rules;
Expand All @@ -54,7 +53,8 @@ public static Object[][] data() {
private final String jndiURL;

public DataSourceConnectionSourceTest(final String jndiURL) {
this.rules = RuleChain.outerRule(new JndiRule(JndiUtil.JNDI_MANAGER_NAME, jndiURL, dataSource))
this.rules = RuleChain.outerRule(
new JndiRule(DataSourceConnectionSource.JNDI_MANAGER_NAME, jndiURL, dataSource))
.around(new LoggerContextRule(CONFIG));
this.jndiURL = jndiURL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

/**
*
*/
public class JdbcAppenderH2DataSourceTest extends AbstractJdbcAppenderDataSourceTest {

public JdbcAppenderH2DataSourceTest() {
super(new JdbcRule(
JdbcH2TestHelper.TEST_CONFIGURATION_SOURCE_MEM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.logging.log4j.jdbc.appender.AbstractConnectionSource;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.appender;
package org.apache.logging.log4j.jdbc.jndi;

import static org.apache.logging.log4j.core.test.TestConstants.JNDI_ENABLE_JDBC;
import static org.junit.Assert.assertFalse;
Expand All @@ -34,7 +34,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.util.Throwables;
import org.apache.logging.log4j.jdbc.appender.internal.JndiUtil;
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
import org.apache.logging.log4j.message.MapMessage;
import org.junit.Assert;
Expand Down Expand Up @@ -73,12 +72,11 @@ protected JdbcAppenderMapMessageDataSourceTest(final JdbcRule jdbcRule) {
// @formatter:off
this.rules = RuleChain.emptyRuleChain()
.around(new JndiRule(
JndiUtil.JNDI_MANAGER_NAME,
DataSourceConnectionSource.JNDI_MANAGER_NAME,
"java:/comp/env/jdbc/TestDataSourceAppender",
createMockDataSource()))
.around(jdbcRule)
.around(new LoggerContextRule(
"org/apache/logging/log4j/jdbc/appender/log4j2-data-source-map-message.xml"));
.around(new LoggerContextRule("JdbcAppenderMapMessageDataSourceTest.xml"));
// @formatter:on
this.jdbcRule = jdbcRule;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jdbc.jndi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.logging.log4j.jdbc.appender.AbstractConnectionSource;
import org.apache.logging.log4j.jdbc.appender.ConnectionSource;

public class JdbcH2TestHelper {

private static final String CONNECTION_STRING = "jdbc:h2:mem:Log4j";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";

public static ConnectionSource TEST_CONFIGURATION_SOURCE_MEM = new AbstractConnectionSource() {
@Override
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(CONNECTION_STRING, USERNAME, PASSWORD);
}
};
}
Loading
Loading