fi.evolver.basics.spring.log.MessageLogMetadataAttribute Maven / Gradle / Ivy
package fi.evolver.basics.spring.log;
import fi.evolver.basics.spring.log.entity.MessageLogMetadata;
import fi.evolver.utils.ContextUtils;
import fi.evolver.utils.attribute.ContextRegistrableAttribute;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
/**
* Sets a single metadata to use for all message logs in the current context.
* The value is saved within the current context.
*
* @see LogMetadataAttribute
*/
public class MessageLogMetadataAttribute implements ContextRegistrableAttribute {
static final ThreadLocal CONTEXT_LOG_METADATA = ThreadLocal.withInitial(MetadataMap::new);
private final String name;
/**
* Create a new message log metadata attribute.
*
* @param name The name of the message log metadata.
*/
public MessageLogMetadataAttribute(String name) {
this.name = name;
}
@Override
public String name() {
return name;
}
@Override
public Optional get() {
return Optional.ofNullable(CONTEXT_LOG_METADATA.get().get(name));
}
@Override
public void set(String value) {
if (value != null) {
CONTEXT_LOG_METADATA.get().put(name, value);
ContextUtils.register(this);
}
else {
CONTEXT_LOG_METADATA.get().remove(name);
}
}
static List listMetadata() {
return CONTEXT_LOG_METADATA.get().entrySet().stream()
.filter(e -> e.getKey() != null && e.getValue() != null)
.map(e -> new MessageLogMetadata(e.getKey(), e.getValue()))
.toList();
}
@Override
public Class type() {
return String.class;
}
static class MetadataMap extends LinkedHashMap {
private static final long serialVersionUID = 1L;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy