Skip to content

Commit

Permalink
[WFCORE-839] : Don't store state in InMemoryAuditLogHandlerResourceDe…
Browse files Browse the repository at this point in the history
…finition.

Updating ManagedAuditLogger to be able to access the last log entries with only a real implemntation in the InMemoryAuditLogHander.
  • Loading branch information
ehsavoie committed Aug 17, 2015
1 parent 89176da commit fec40cb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 62 deletions.
Expand Up @@ -22,11 +22,14 @@
package org.jboss.as.controller.audit;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.logging.ControllerLogger;
import org.jboss.dmr.ModelNode;

/**
* All methods on this class should be called with {@link ManagedAuditLoggerImpl}'s lock taken.
Expand Down Expand Up @@ -130,6 +133,10 @@ FailureCountHandler getFailureCountHandler() {
abstract void stop();
abstract void writeLogItem(String formattedItem) throws IOException;

List<ModelNode> listLastEntries() {
return Collections.emptyList();
}

interface FailureCountHandler {
void success();
void failure(Throwable t);
Expand Down
Expand Up @@ -190,5 +190,9 @@ public void updateSyslogHandlerAppName(String name, String appName) {
@Override
public void updateSyslogHandlerReconnectTimeout(String name, int reconnectTimeout) {
}

@Override
public void updateInMemoryHandlerMaxHistory(String name, int maxHistory) {
}
};
}
Expand Up @@ -61,33 +61,29 @@ public final class InMemoryAuditLogHander extends AuditLogHandler {
private int maxHistory;
private final AuditLogItemFormatter myFormatter = new InMemoryFormatter();


public InMemoryAuditLogHander(String name, int maxHistory) {
super(name, IN_MEMORY_FORMATTER_NAME, maxHistory);
items = new ArrayList<>(maxHistory);
this.items = new ArrayList<>(maxHistory);
this.maxHistory = maxHistory;
setFormatter(myFormatter);
}

public List<ModelNode> getItems() {
@Override
public List<ModelNode> listLastEntries() {
return Collections.unmodifiableList(items);
}

public void setMaxHistory(int maxHistory) {
synchronized (items) {
this.maxHistory = maxHistory;
while (maxHistory < items.size()) {
items.remove(0);
}
this.maxHistory = maxHistory;
while (maxHistory < items.size()) {
items.remove(0);
}
}

@Override
void setFormatter(AuditLogItemFormatter formatter) {
if (formatter != null) {
super.setFormatter(formatter);
} else {
super.setFormatter(myFormatter);
}
super.setFormatter(myFormatter);
}

public AuditLogItemFormatter getFormatter() {
Expand All @@ -103,12 +99,10 @@ boolean isDifferent(AuditLogHandler other) {
}

private void addItem(ModelNode item) {
synchronized (items) {
if (items.size() == maxHistory) {
items.remove(0);
}
items.add(item);
if (items.size() == maxHistory) {
items.remove(0);
}
items.add(item);
}

@Override
Expand Down
Expand Up @@ -25,8 +25,11 @@
package org.jboss.as.controller.audit;


import java.util.Collections;
import java.util.List;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.audit.SyslogAuditLogHandler.Facility;
import org.jboss.dmr.ModelNode;

/**
* Abstract base class for {@link AuditLogger} implementations.
Expand Down Expand Up @@ -149,7 +152,13 @@ public interface ManagedAuditLogger extends AuditLogger {
void updateSyslogHandlerReconnectTimeout(String name, int reconnectTimeout);
//ImmediateUpdates - end


/**
* Update the handler history size.
*
* @param name the name of the handler
* @param maxHistory the history size of the handler
*/
void updateInMemoryHandlerMaxHistory(String name, int maxHistory);
/**
* Remove a formatter
*
Expand Down Expand Up @@ -181,6 +190,16 @@ public interface ManagedAuditLogger extends AuditLogger {
*/
JsonAuditLogItemFormatter getJsonFormatter(String name);

/**
* Gets the last log entries
*
* @param name the name of the handler
* @return the last log entries of the handler
*/
default List<ModelNode> listLastEntries(String name){
return Collections.emptyList();
}

/**
* Callback for the controller to call before the controller is booted
*/
Expand Down
Expand Up @@ -507,6 +507,27 @@ public JsonAuditLogItemFormatter getJsonFormatter(String name) {
}
}

@Override
public List<ModelNode> listLastEntries(String name) {
config.lock();
try {
return config.getConfiguredHandler(name).listLastEntries();
} finally {
config.unlock();
}
}

@Override
public void updateInMemoryHandlerMaxHistory(String name, int maxHistory) {
config.lock();
try {
InMemoryAuditLogHander handler = (InMemoryAuditLogHander)config.getConfiguredHandler(name);
handler.setMaxHistory(maxHistory);
} finally {
config.unlock();
}
}


/**
* Abstract base class for core and new configuration
Expand Down
Expand Up @@ -33,10 +33,7 @@
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.as.controller.AbstractAddStepHandler;
import org.jboss.as.controller.AbstractRuntimeOnlyHandler;
import org.jboss.as.controller.AbstractWriteAttributeHandler;
Expand Down Expand Up @@ -88,12 +85,11 @@ public class InMemoryAuditLogHandlerResourceDefinition extends AuditLogHandlerRe

public static final String OPERATION_NAME = "show-logs";
protected static final AttributeDefinition[] ATTRIBUTES = new AttributeDefinition[]{MAX_OPERATION_COUNT};
private static final Map<String, InMemoryAuditLogHander> histories = Collections.synchronizedMap(new HashMap<String, InMemoryAuditLogHander>());

public InMemoryAuditLogHandlerResourceDefinition(ManagedAuditLogger auditLogger) {
super(auditLogger, null, PathElement.pathElement(IN_MEMORY_HANDLER),
DomainManagementResolver.getResolver("core.management.in-memory-handler"),
new InMemoryAuditLogHandlerAddHandler(auditLogger), new InMemoryAuditLogHandlerRemoveHandler(auditLogger));
new InMemoryAuditLogHandlerAddHandler(auditLogger), new HandlerRemoveHandler(auditLogger));
}

@Override
Expand All @@ -105,13 +101,13 @@ public void registerOperations(ManagementResourceRegistration resourceRegistrati
.setRuntimeOnly()
.setReplyType(ModelType.LIST)
.setReplyValueType(ModelType.STRING)
.build(), new ShowInMemoryLogsHandler());
.build(), new ShowInMemoryLogsHandler(auditLogger));
}

@Override
public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
for (AttributeDefinition def : ATTRIBUTES) {
resourceRegistration.registerReadWriteAttribute(def, null, new InMemoryMaxHistoryWriteHandler());
resourceRegistration.registerReadWriteAttribute(def, null, new InMemoryMaxHistoryWriteHandler(auditLogger));
}
}

Expand All @@ -126,19 +122,21 @@ public static ModelNode createServerAddOperation(final PathAddress address, fina
}

protected static class ShowInMemoryLogsHandler extends AbstractRuntimeOnlyHandler {
private final ManagedAuditLogger auditLogger;

public ShowInMemoryLogsHandler(ManagedAuditLogger auditLogger) {
this.auditLogger = auditLogger;
}

@Override
protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
final String name = Util.getNameFromAddress(operation.require(OP_ADDR));
InMemoryAuditLogHander loghandler = InMemoryAuditLogHandlerResourceDefinition.histories.get(name);
if (loghandler != null) {
ModelNode result = context.getResult().setEmptyList();
List<ModelNode> items = loghandler.getItems();
for (int i = (items.size() - 1); i >= 0; i--) {
ModelNode entry = items.get(i);
ModelNode configurationChange = entry.clone();
result.add(configurationChange);
}
ModelNode result = context.getResult().setEmptyList();
List<ModelNode> items = this.auditLogger.listLastEntries(name);
for (int i = (items.size() - 1); i >= 0; i--) {
ModelNode entry = items.get(i);
ModelNode configurationChange = entry.clone();
result.add(configurationChange);
}
}
}
Expand All @@ -164,10 +162,8 @@ protected void populateModel(ModelNode operation, ModelNode model) throws Operat

protected InMemoryAuditLogHander createHandler(final OperationContext context, final ModelNode operation) throws OperationFailedException {
final String name = Util.getNameFromAddress(operation.require(OP_ADDR));
final int maxOperationCount = MAX_OPERATION_COUNT.resolveModelAttribute(context, operation).asInt();
InMemoryAuditLogHander handler = new InMemoryAuditLogHander(name, maxOperationCount);
InMemoryAuditLogHandlerResourceDefinition.histories.put(name, handler);
return handler;
final int maxHistory = MAX_OPERATION_COUNT.resolveModelAttribute(context, operation).asInt();
return new InMemoryAuditLogHander(name, maxHistory);
}

@Override
Expand All @@ -184,24 +180,12 @@ protected void rollbackRuntime(OperationContext context, ModelNode operation, Re
}
}

protected static class InMemoryAuditLogHandlerRemoveHandler extends HandlerRemoveHandler {

public InMemoryAuditLogHandlerRemoveHandler(ManagedAuditLogger auditLogger) {
super(auditLogger);
}

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
super.performRuntime(context, operation, model);
final String name = Util.getNameFromAddress(operation.require(OP_ADDR));
InMemoryAuditLogHandlerResourceDefinition.histories.remove(name);
}
}

protected static class InMemoryMaxHistoryWriteHandler extends AbstractWriteAttributeHandler<Void> {
private final ManagedAuditLogger auditLogger;

public InMemoryMaxHistoryWriteHandler() {
public InMemoryMaxHistoryWriteHandler(ManagedAuditLogger auditLogger) {
super(MAX_OPERATION_COUNT);
this.auditLogger = auditLogger;
}

@Override
Expand All @@ -213,22 +197,15 @@ protected boolean requiresRuntime(OperationContext context) {
protected void revertUpdateToRuntime(OperationContext context, ModelNode operation, String attributeName, ModelNode valueToRestore, ModelNode valueToRevert, Void handback) throws OperationFailedException {
if (MAX_HISTORY.equals(attributeName)) {
final String name = Util.getNameFromAddress(operation.require(OP_ADDR));
InMemoryAuditLogHander loghandler = InMemoryAuditLogHandlerResourceDefinition.histories.get(name);
if (loghandler != null) {
loghandler.setMaxHistory(valueToRevert.asInt());
}
auditLogger.updateInMemoryHandlerMaxHistory(name, valueToRevert.asInt());
}
}

@Override
protected boolean applyUpdateToRuntime(OperationContext context, ModelNode operation, String attributeName, ModelNode resolvedValue, ModelNode currentValue, HandbackHolder<Void> handbackHolder) throws OperationFailedException {
if (MAX_HISTORY.equals(attributeName)) {
final String name = Util.getNameFromAddress(operation.require(OP_ADDR));
InMemoryAuditLogHander loghandler = InMemoryAuditLogHandlerResourceDefinition.histories.get(name);
if (loghandler != null) {
loghandler.setMaxHistory(resolvedValue.asInt());
return true;
}
auditLogger.updateInMemoryHandlerMaxHistory(name, resolvedValue.asInt());
}
return false;
}
Expand Down

0 comments on commit fec40cb

Please sign in to comment.