use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.
the class ContextDataJsonAttributeConverterTest method testConvert01.
@Test
public void testConvert01() {
final StringMap map = new SortedArrayStringMap();
map.putValue("test1", "another1");
map.putValue("key2", "value2");
final String converted = this.converter.convertToDatabaseColumn(map);
assertNotNull("The converted value should not be null.", converted);
final ReadOnlyStringMap reversed = this.converter.convertToEntityAttribute(converted);
assertNotNull("The reversed value should not be null.", reversed);
assertEquals("The reversed value is not correct.", map, reversed);
}
use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.
the class ContextDataJsonAttributeConverter method convertToEntityAttribute.
@Override
public ReadOnlyStringMap convertToEntityAttribute(final String s) {
if (Strings.isEmpty(s)) {
return null;
}
try {
final StringMap result = ContextDataFactory.createContextData();
final ObjectNode root = (ObjectNode) OBJECT_MAPPER.readTree(s);
final Iterator<Map.Entry<String, JsonNode>> entries = root.fields();
while (entries.hasNext()) {
final Map.Entry<String, JsonNode> entry = entries.next();
// Don't know what to do with non-text values.
// Maybe users who need this need to provide custom converter?
final Object value = entry.getValue().textValue();
result.putValue(entry.getKey(), value);
}
return result;
} catch (final IOException e) {
throw new PersistenceException("Failed to convert JSON string to map.", e);
}
}
use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.
the class NoSqlDatabaseManager method writeInternal.
@Override
protected void writeInternal(final LogEvent event) {
if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
throw new AppenderLoggingException("Cannot write logging event; NoSQL manager not connected to the database.");
}
final NoSqlObject<W> entity = this.connection.createObject();
entity.set("level", event.getLevel());
entity.set("loggerName", event.getLoggerName());
entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
final StackTraceElement source = event.getSource();
if (source == null) {
entity.set("source", (Object) null);
} else {
entity.set("source", this.convertStackTraceElement(source));
}
final Marker marker = event.getMarker();
if (marker == null) {
entity.set("marker", (Object) null);
} else {
entity.set("marker", buildMarkerEntity(marker));
}
entity.set("threadId", event.getThreadId());
entity.set("threadName", event.getThreadName());
entity.set("threadPriority", event.getThreadPriority());
entity.set("millis", event.getTimeMillis());
entity.set("date", new java.util.Date(event.getTimeMillis()));
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable thrown = event.getThrown();
if (thrown == null) {
entity.set("thrown", (Object) null);
} else {
final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
NoSqlObject<W> exceptionEntity = originalExceptionEntity;
exceptionEntity.set("type", thrown.getClass().getName());
exceptionEntity.set("message", thrown.getMessage());
exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
while (thrown.getCause() != null) {
thrown = thrown.getCause();
final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
causingExceptionEntity.set("type", thrown.getClass().getName());
causingExceptionEntity.set("message", thrown.getMessage());
causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
exceptionEntity.set("cause", causingExceptionEntity);
exceptionEntity = causingExceptionEntity;
}
entity.set("thrown", originalExceptionEntity);
}
final ReadOnlyStringMap contextMap = event.getContextData();
if (contextMap == null) {
entity.set("contextMap", (Object) null);
} else {
final NoSqlObject<W> contextMapEntity = this.connection.createObject();
contextMap.forEach(new BiConsumer<String, String>() {
@Override
public void accept(final String key, final String val) {
contextMapEntity.set(key, val);
}
});
entity.set("contextMap", contextMapEntity);
}
final ThreadContext.ContextStack contextStack = event.getContextStack();
if (contextStack == null) {
entity.set("contextStack", (Object) null);
} else {
entity.set("contextStack", contextStack.asList().toArray());
}
this.connection.insertObject(entity);
}
use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.
the class ThreadContextMapFilter method filter.
private Result filter() {
boolean match = false;
if (useMap) {
ReadOnlyStringMap currentContextData = null;
final IndexedReadOnlyStringMap map = getStringMap();
for (int i = 0; i < map.size(); i++) {
if (currentContextData == null) {
currentContextData = currentContextData();
}
final String toMatch = currentContextData.getValue(map.getKeyAt(i));
match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch);
if ((!isAnd() && match) || (isAnd() && !match)) {
break;
}
}
} else {
match = value.equals(currentContextData().getValue(key));
}
return match ? onMatch : onMismatch;
}
use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.
the class OpenHashStringMap method equals.
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ReadOnlyStringMap)) {
return false;
}
final ReadOnlyStringMap other = (ReadOnlyStringMap) obj;
if (other.size() != size()) {
return false;
}
int pos = arraySize;
if (containsNullKey) {
if (!Objects.equals(getObjectValue(null), other.getValue(null))) {
return false;
}
}
--pos;
final K[] myKeys = this.keys;
for (; pos >= 0; pos--) {
K k;
if ((k = myKeys[pos]) != null) {
if (!Objects.equals(values[pos], other.getValue((String) k))) {
return false;
}
}
}
return true;
}
Aggregations