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 Jul 31, 2015
1 parent 37cf95d commit cf6913c
Show file tree
Hide file tree
Showing 6 changed files with 69 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 @@ -23,6 +23,7 @@
package org.jboss.as.controller.audit;

import java.net.InetAddress;
import java.util.Collections;
import java.util.List;

import org.jboss.as.controller.OperationContext;
Expand Down Expand Up @@ -80,6 +81,7 @@ public boolean isLogBoot() {
public void setLogBoot(boolean logBoot) {
}

@Override
public Status getLoggerStatus() {
return Status.DISABLED;
}
Expand Down Expand Up @@ -190,5 +192,10 @@ public void updateSyslogHandlerAppName(String name, String appName) {
@Override
public void updateSyslogHandlerReconnectTimeout(String name, int reconnectTimeout) {
}

@Override
public List<ModelNode> listLastEntries(String name) {
return Collections.emptyList();
}
};
}
Expand Up @@ -58,36 +58,34 @@ public final class InMemoryAuditLogHander extends AuditLogHandler {
}
private static final String IN_MEMORY_FORMATTER_NAME = "in-memory-formatter";
private final List<ModelNode> items;
private int maxHistory;
private volatile 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);
}
@Override
public void setMaxFailureCount(int maxHistory) {
super.setMaxFailureCount(maxHistory);
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 +101,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,10 @@
package org.jboss.as.controller.audit;


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 @@ -181,6 +183,14 @@ 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
*/
List<ModelNode> listLastEntries(String name);

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

@Override
public List<ModelNode> listLastEntries(String name) {
config.lock();
try {
return config.getConfiguredHandler(name).listLastEntries();
} 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 @@ -165,9 +163,7 @@ 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;
return new InMemoryAuditLogHander(name, maxOperationCount);
}

@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.updateHandlerMaxFailureCount(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.updateHandlerMaxFailureCount(name, resolvedValue.asInt());
}
return false;
}
Expand Down

0 comments on commit cf6913c

Please sign in to comment.