io.bdeploy.bhive.audit.AuditParameterExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
Public API including dependencies, ready to be used for integrations and plugins.
package io.bdeploy.bhive.audit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.bdeploy.bhive.BHive;
import io.bdeploy.bhive.BHive.Operation;
/**
* Extracts field values from Hive {@link Operation}s for auditing purposes.
*/
public class AuditParameterExtractor {
private static final Logger log = LoggerFactory.getLogger(AuditParameterExtractor.class);
/**
* Prevents auditing of the given Field.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NoAudit {
}
/**
* Determines how a field should be audited
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AuditWith {
AuditParameterExtractor.AuditStrategy value() default AuditStrategy.TO_STRING;
}
/**
* Defines the strategy used to extract a fields value. This directly defines the way a value is represented in the audit
* logs.
*/
public enum AuditStrategy {
TO_STRING(Object::toString),
COLLECTION_SIZE(x -> Integer.toString(((Collection>) x).size())),
COLLECTION_PEEK(x -> {
List items = new ArrayList<>();
if (x instanceof Collection) {
Collection> coll = ((Collection>) x);
coll.stream().limit(3).map(Object::toString).forEach(items::add);
if (coll.size() > 3) {
items.add("...");
}
} else if (x instanceof Map) {
Map, ?> m = ((Map, ?>) x);
m.entrySet().stream().limit(3).map(Object::toString).forEach(items::add);
if (m.size() > 3) {
items.add("...");
}
}
return items.toString();
});
private final Function
© 2015 - 2024 Weber Informatics LLC | Privacy Policy