Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-2995] When properties change on log4j appenders activate should be invoked #5943

Merged
merged 1 commit into from Feb 23, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions logging/src/main/java/org/jboss/as/logging/HandlerOperations.java
Expand Up @@ -272,14 +272,17 @@ protected HandlerConfiguration createHandlerConfiguration(final String className
try {
final Class<?> actualClass = Class.forName(className, false, moduleLoader.loadModule(id).getClassLoader());
if (Appender.class.isAssignableFrom(actualClass)) {
final PojoConfiguration pojoConfiguration;
// Check for construction parameters
if (constructionProperties == null) {
logContextConfiguration.addPojoConfiguration(moduleName, className, name);
pojoConfiguration = logContextConfiguration.addPojoConfiguration(moduleName, className, name);
} else {
logContextConfiguration.addPojoConfiguration(moduleName, className, name, constructionProperties);
pojoConfiguration = logContextConfiguration.addPojoConfiguration(moduleName, className, name, constructionProperties);
}
// Set the name on the appender
pojoConfiguration.setPropertyValueString("name", name);
configuration = logContextConfiguration.addHandlerConfiguration("org.jboss.as.logging", Log4jAppenderHandler.class.getName(), name);
configuration.addPostConfigurationMethod("activate");
configuration.addPostConfigurationMethod(Log4jAppenderHandler.ACTIVATE_OPTIONS_METHOD_NAME);
configuration.setPropertyValueString("appender", name);
} else {
// Check for construction parameters
Expand Down Expand Up @@ -348,6 +351,10 @@ protected boolean applyUpdate(final OperationContext context, final String attri
propertyConfigurable = configuration;
} else {
propertyConfigurable = pojoConfiguration;
// A log4j appender may be an OptionHandler which requires the invocation of activateOptions(). Setting
// a dummy property on the Log4jAppenderHandler is required to invoke this method as all properties are
// set on the POJO which is the actual appender
configuration.setPropertyValueString(Log4jAppenderHandler.ACTIVATOR_PROPERTY_METHOD_NAME, "");
}
if (value.isDefined()) {
for (Property property : value.asPropertyList()) {
Expand Down Expand Up @@ -561,6 +568,7 @@ private static void handleProperty(final AttributeDefinition attribute, final Op
private static void handleProperty(final AttributeDefinition attribute, final OperationContext context, final ModelNode model,
final LogContextConfiguration logContextConfiguration, final HandlerConfiguration configuration, final boolean resolveValue)
throws OperationFailedException {

if (attribute.getName().equals(ENABLED.getName())) {
final boolean value = ((resolveValue ? ENABLED.resolveModelAttribute(context, model).asBoolean() : model.asBoolean()));
if (value) {
Expand Down Expand Up @@ -643,6 +651,10 @@ private static void handleProperty(final AttributeDefinition attribute, final Op
propertyConfigurable = configuration;
} else {
propertyConfigurable = pojoConfiguration;
// A log4j appender may be an OptionHandler which requires the invocation of activateOptions(). Setting
// a dummy property on the Log4jAppenderHandler is required to invoke this method as all properties are
// set on the POJO which is the actual appender
configuration.setPropertyValueString(Log4jAppenderHandler.ACTIVATOR_PROPERTY_METHOD_NAME, "");
}
// Should be safe here to only process defined properties. The write-attribute handler handles removing
// undefined properties
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.OptionHandler;
import org.jboss.as.logging.LoggingLogger;
import org.jboss.as.logging.LoggingMessages;
import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -40,6 +41,8 @@
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class Log4jAppenderHandler extends ExtHandler {
public static final String ACTIVATE_OPTIONS_METHOD_NAME = "activate";
public static final String ACTIVATOR_PROPERTY_METHOD_NAME = "dummy";
private volatile Appender appender = null;
private final boolean applyLayout;

Expand Down Expand Up @@ -82,12 +85,25 @@ public Appender getAppender() {
return appender;
}

/**
* This method does nothing. It's only purpose is to be invoked so the {@link #activate()} method will be invoked
* when log4j appenders are also {@link org.apache.log4j.spi.OptionHandler option handlers}.
*
* @param ignore any string value or {@code null}
*/
public void setDummy(final String ignore) {
// does nothing
}

/**
* Activates the appender only if it's an {@link OptionHandler option handler}.
*/
public void activate() {
if (appender instanceof OptionHandler) {
((OptionHandler) appender).activateOptions();
if (LoggingLogger.ROOT_LOGGER.isDebugEnabled()) {
LoggingLogger.ROOT_LOGGER.debugf("Invoking OptionHandler.activateOptions() on appender %s (%s)", appender.getName(), appender.getClass().getCanonicalName());
}
}
}

Expand Down