From 03d8b4852f8cd003a2f4d4e127f97f866fb347e9 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Mon, 7 Mar 2016 12:27:02 -0500 Subject: [PATCH] Bugfix for Log4JLookup to ensure that it can find the Configuration (second attempt / Thanks Matt Sicker) --- .../log4j/core/config/AbstractConfiguration.java | 1 + .../log4j/core/config/ConfigurationAware.java | 26 ++++++++++ .../lookup/AbstractConfigurationAwareLookup.java | 33 ++++++++++++ .../logging/log4j/core/lookup/Interpolator.java | 7 ++- .../logging/log4j/core/lookup/Log4jLookup.java | 60 +++++++++++----------- .../logging/log4j/core/lookup/StrSubstitutor.java | 16 +++++- .../logging/log4j/core/lookup/Log4jLookupTest.java | 32 ++++++------ .../core/lookup/Log4jLookupWithSpacesTest.java | 16 +++--- 8 files changed, 136 insertions(+), 55 deletions(-) create mode 100644 log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java create mode 100644 log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractConfigurationAwareLookup.java diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java index e67e6ee..00cd397 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java @@ -177,6 +177,7 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement @Override public void initialize() { LOGGER.debug("Initializing configuration {}", this); + subst.setConfiguration(this); scriptManager = new ScriptManager(watchManager); pluginManager.collectPlugins(pluginPackages); final PluginManager levelPlugins = new PluginManager(Level.CATEGORY); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java new file mode 100644 index 0000000..b427b7f --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java @@ -0,0 +1,26 @@ +/* + * 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.core.config; + +/** + * + */ +public interface ConfigurationAware { + + void setConfiguration(Configuration configuration); +} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractConfigurationAwareLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractConfigurationAwareLookup.java new file mode 100644 index 0000000..901e2a6 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractConfigurationAwareLookup.java @@ -0,0 +1,33 @@ +/* + * 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.core.lookup; + +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; + +/** + * + */ +public abstract class AbstractConfigurationAwareLookup extends AbstractLookup implements ConfigurationAware { + protected Configuration configuration; + + @Override + public void setConfiguration(final Configuration configuration) { + this.configuration = configuration; + } +} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java index ec2fbc0..2cdc04b 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java @@ -22,6 +22,8 @@ import java.util.Map; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.core.config.plugins.util.PluginManager; import org.apache.logging.log4j.core.config.plugins.util.PluginType; import org.apache.logging.log4j.core.util.Loader; @@ -31,7 +33,7 @@ import org.apache.logging.log4j.status.StatusLogger; /** * Proxies all the other {@link StrLookup}s. */ -public class Interpolator extends AbstractLookup { +public class Interpolator extends AbstractConfigurationAwareLookup { private static final Logger LOGGER = StatusLogger.getLogger(); @@ -150,6 +152,9 @@ public class Interpolator extends AbstractLookup { final String prefix = var.substring(0, prefixPos); final String name = var.substring(prefixPos + 1); final StrLookup lookup = lookups.get(prefix); + if (lookup instanceof ConfigurationAware) { + ((ConfigurationAware) lookup).setConfiguration(configuration); + } String value = null; if (lookup != null) { value = event == null ? lookup.lookup(name) : lookup.lookup(event, name); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java index 4061900..2f1ef67 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java @@ -22,17 +22,15 @@ import java.net.URISyntaxException; import java.net.URL; import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.impl.ContextAnchor; import org.apache.logging.log4j.status.StatusLogger; /** * Lookup properties of Log4j */ @Plugin(name = "log4j", category = StrLookup.CATEGORY) -public class Log4jLookup extends AbstractLookup { +public class Log4jLookup extends AbstractConfigurationAwareLookup { public final static String KEY_CONFIG_LOCATION = "configLocation"; public final static String KEY_CONFIG_PARENT_LOCATION = "configParentLocation"; @@ -57,40 +55,42 @@ public class Log4jLookup extends AbstractLookup { @Override public String lookup(final LogEvent event, final String key) { - final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); - if (ctx == null) { - return null; - } - final ConfigurationSource configSrc = ctx.getConfiguration().getConfigurationSource(); - final File file = configSrc.getFile(); - if (file != null) { - switch (key) { - case KEY_CONFIG_LOCATION: - return file.getAbsolutePath(); + if (configuration != null) { + final ConfigurationSource configSrc = configuration.getConfigurationSource(); + final File file = configSrc.getFile(); + if (file != null) { + switch (key) { + case KEY_CONFIG_LOCATION: + return file.getAbsolutePath(); - case KEY_CONFIG_PARENT_LOCATION: - return file.getParentFile().getAbsolutePath(); + case KEY_CONFIG_PARENT_LOCATION: + return file.getParentFile().getAbsolutePath(); - default: - return null; + default: + return null; + } } - } - final URL url = configSrc.getURL(); - try { - switch (key) { - case KEY_CONFIG_LOCATION: - return asPath(url.toURI()); + final URL url = configSrc.getURL(); + if (url != null) { + try { + switch (key) { + case KEY_CONFIG_LOCATION: + return asPath(url.toURI()); - case KEY_CONFIG_PARENT_LOCATION: - return asPath(getParent(url.toURI())); + case KEY_CONFIG_PARENT_LOCATION: + return asPath(getParent(url.toURI())); - default: - return null; + default: + return null; + } + } catch (final URISyntaxException use) { + LOGGER.error(use); + return null; + } } - } catch (final URISyntaxException use) { - LOGGER.error(use); - return null; } + + return null; } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java index 0b9ead6..d732524 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java @@ -25,6 +25,8 @@ import java.util.Map; import java.util.Properties; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.util.Strings; /** @@ -136,7 +138,7 @@ import org.apache.logging.log4j.util.Strings; * property to true. *

*/ -public class StrSubstitutor { +public class StrSubstitutor implements ConfigurationAware { /** * Constant for the default escape character. @@ -184,6 +186,7 @@ public class StrSubstitutor { * The flag whether substitution in variable names is enabled. */ private boolean enableSubstitutionInVariables; + private Configuration configuration; //----------------------------------------------------------------------- /** @@ -1294,6 +1297,9 @@ public class StrSubstitutor { * @param variableResolver the VariableResolver */ public void setVariableResolver(final StrLookup variableResolver) { + if (variableResolver instanceof ConfigurationAware && this.configuration != null) { + ((ConfigurationAware) variableResolver).setConfiguration(this.configuration); + } this.variableResolver = variableResolver; } @@ -1352,4 +1358,12 @@ public class StrSubstitutor { public String toString() { return "StrSubstitutor(" + variableResolver.toString() + ')'; } + + @Override + public void setConfiguration(final Configuration configuration) { + this.configuration = configuration; + if (this.variableResolver instanceof ConfigurationAware) { + ((ConfigurationAware) this.variableResolver).setConfiguration(this.configuration); + } + } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java index fc1ca36..3059a9a 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java @@ -15,20 +15,12 @@ */ package org.apache.logging.log4j.core.lookup; -import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_LOCATION; -import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_PARENT_LOCATION; - -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; - -import static org.junit.Assert.assertEquals; - import java.io.File; import java.net.URISyntaxException; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; @@ -36,6 +28,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_LOCATION; +import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_PARENT_LOCATION; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.*; + /** * */ @@ -43,27 +42,26 @@ public class Log4jLookupTest { private final static File EXPECT = new File(System.getProperty("user.home"), "/a/b/c/d/e/log4j2.xml"); private LoggerContext mockCtx = null; + private Configuration config = null; + private ConfigurationSource configSrc = null; @Before public void setup() throws URISyntaxException { this.mockCtx = EasyMock.createMock(LoggerContext.class); ContextAnchor.THREAD_CONTEXT.set(mockCtx); - final Configuration config = EasyMock.createMock(Configuration.class); - expect(mockCtx.getConfiguration()).andReturn(config); + this.config = EasyMock.createMock(Configuration.class); - final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + this.configSrc = EasyMock.createMock(ConfigurationSource.class); expect(config.getConfigurationSource()).andReturn(configSrc); expect(configSrc.getFile()).andReturn(EXPECT); - replay(mockCtx); - replay(config); - replay(configSrc); + replay(mockCtx, config, configSrc); } @After public void cleanup() { - verify(mockCtx); + verify(mockCtx, config, configSrc); ContextAnchor.THREAD_CONTEXT.set(null); this.mockCtx = null; @@ -72,6 +70,7 @@ public class Log4jLookupTest { @Test public void lookupConfigLocation() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); assertEquals(EXPECT.getAbsolutePath(), value); } @@ -79,6 +78,7 @@ public class Log4jLookupTest { @Test public void lookupConfigParentLocation() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); assertEquals(EXPECT.getParentFile().getAbsolutePath(), value); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java index 3e20159..7b88ad4 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java @@ -21,6 +21,7 @@ import java.net.URISyntaxException; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; @@ -41,27 +42,26 @@ public class Log4jLookupWithSpacesTest { private final static File EXPECT = new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml"); private LoggerContext mockCtx = null; + private Configuration config = null; + private ConfigurationSource configSrc = null; @Before public void setup() throws URISyntaxException, MalformedURLException { this.mockCtx = EasyMock.createMock(LoggerContext.class); ContextAnchor.THREAD_CONTEXT.set(mockCtx); - final Configuration config = EasyMock.createMock(Configuration.class); - expect(mockCtx.getConfiguration()).andReturn(config); + this.config = EasyMock.createMock(Configuration.class); - final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + this.configSrc = EasyMock.createMock(ConfigurationSource.class); expect(config.getConfigurationSource()).andReturn(configSrc); expect(configSrc.getFile()).andReturn(EXPECT); - replay(mockCtx); - replay(config); - replay(configSrc); + replay(mockCtx, config, configSrc); } @After public void cleanup() { - verify(mockCtx); + verify(mockCtx, config, configSrc); ContextAnchor.THREAD_CONTEXT.set(null); this.mockCtx = null; @@ -70,6 +70,7 @@ public class Log4jLookupWithSpacesTest { @Test public void lookupConfigLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); assertEquals( new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml").getAbsolutePath(), @@ -79,6 +80,7 @@ public class Log4jLookupWithSpacesTest { @Test public void lookupConfigParentLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); assertEquals(new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e").getAbsolutePath(), value); } -- 2.5.1