All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.floragunn.searchguard.auditlog.sink.AuditLogSink Maven / Gradle / Ivy

There is a newer version: 7.5.1-38.0.0
Show newest version
/*
 * Copyright 2016-2017 by floragunn GmbH - All rights reserved
 * 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed here is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * This software is free of charge for non-commercial and academic use. 
 * For commercial use in a production environment you have to obtain a license 
 * from https://floragunn.com
 * 
 */

package com.floragunn.searchguard.auditlog.sink;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.settings.Settings;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;

import com.floragunn.searchguard.auditlog.impl.AuditMessage;
import com.floragunn.searchguard.support.ConfigConstants;
import com.google.common.util.concurrent.Uninterruptibles;

public abstract class AuditLogSink {

    protected final Logger log = LogManager.getLogger(this.getClass());
    protected final Settings settings;
    protected final String settingsPrefix;
    private final String name;
    protected final AuditLogSink fallbackSink;
    private final int retryCount;
    private final long delayMs;
    
    protected AuditLogSink(String name, Settings settings, String settingsPrefix, AuditLogSink fallbackSink) {
        this.name = name.toLowerCase();
    	this.settings = Objects.requireNonNull(settings);
        this.settingsPrefix = settingsPrefix;
        this.fallbackSink = fallbackSink;
        
        retryCount = settings.getAsInt(ConfigConstants.SEARCHGUARD_AUDIT_RETRY_COUNT, 0);
        delayMs = settings.getAsLong(ConfigConstants.SEARCHGUARD_AUDIT_RETRY_DELAY_MS, 1000L);
    }
    
    public boolean isHandlingBackpressure() {
        return false;
    }
    
    public String getName() {
    	return name;
    }
    
    public AuditLogSink getFallbackSink() {
    	return fallbackSink;
    }
    
    public final void store(AuditMessage msg) {
		if (!doStoreWithRetry(msg) && !fallbackSink.doStoreWithRetry(msg)) {
			System.err.println(msg.toPrettyString());
		}
    }
    
    private boolean doStoreWithRetry(AuditMessage msg) {
        //retryCount of 0 means no retry (which is: try exactly once) - delayMs is ignored
        //retryCount of 1 means: try and if this fails wait delayMs and try once again
        
        if(doStore(msg)) {
            return true;
        }

        
        for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy