@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ThreadContextMap)) { return false; } final ThreadContextMap other = (ThreadContextMap) obj; final Map<String, String> map = this.localMap.get(); final Map<String, String> otherMap = other.getImmutableMapOrNull(); if (map == null) { if (otherMap != null) { return false; } } else if (!map.equals(otherMap)) { return false; } return true; }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { for (int i = 0; i < columnMappings.size(); i++) { final ColumnMapping columnMapping = columnMappings.get(i); if (ThreadContextMap.class.isAssignableFrom(columnMapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(columnMapping.getType())) { values[i] = event.getContextData().toMap(); } else if (ThreadContextStack.class.isAssignableFrom(columnMapping.getType())) { values[i] = event.getContextStack().asList(); } else if (Date.class.isAssignableFrom(columnMapping.getType())) { values[i] = DateTypeConverter.fromMillis(event.getTimeMillis(), columnMapping.getType().asSubclass(Date.class)); } else { values[i] = TypeConverters.convert(columnMapping.getLayout().toSerializable(event), columnMapping.getType(), null); } } final BoundStatement boundStatement = preparedStatement.bind(values); if (batchStatement == null) { session.execute(boundStatement); } else { batchStatement.add(boundStatement); } }
@Override public ColumnMapping build() { if (pattern != null) { layout = PatternLayout.newBuilder() .withPattern(pattern) .withConfiguration(configuration) .build(); } if (!(layout == null || literal == null || Date.class.isAssignableFrom(type) || ReadOnlyStringMap.class.isAssignableFrom(type) || ThreadContextMap.class.isAssignableFrom(type) || ThreadContextStack.class.isAssignableFrom(type))) { LOGGER.error("No 'layout' or 'literal' value specified and type ({}) is not compatible with ThreadContextMap, ThreadContextStack, or java.util.Date for the mapping", type, this); return null; } if (literal != null && parameter != null) { LOGGER.error("Only one of 'literal' or 'parameter' can be set on the column mapping {}", this); return null; } return new ColumnMapping(name, source, layout, literal, parameter, type); }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { StringReader reader = null; try { if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null || this.statement.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; JDBC manager not connected to the database."); } if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable); } int i = 1; // JDBC indices start at 1 for (final ColumnMapping mapping : this.columnMappings) { if (ThreadContextMap.class.isAssignableFrom(mapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextData().toMap()); } else if (ThreadContextStack.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextStack().asList()); } else if (Date.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, DateTypeConverter.fromMillis(event.getTimeMillis(), mapping.getType().asSubclass(Date.class))); } else { StringLayout layout = mapping.getLayout(); if (layout != null) { if (Clob.class.isAssignableFrom(mapping.getType())) { this.statement.setClob(i++, new StringReader(layout.toSerializable(event))); } else if (NClob.class.isAssignableFrom(mapping.getType())) { this.statement.setNClob(i++, new StringReader(layout.toSerializable(event))); } else { final Object value = TypeConverters.convert(layout.toSerializable(event), mapping.getType(), null); if (value == null) { this.statement.setNull(i++, Types.NULL); } else { this.statement.setObject(i++, value); } } } } } for (final ColumnConfig column : this.columnConfigs) { if (column.isEventTimestamp()) { this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis())); } else if (column.isClob()) { reader = new StringReader(column.getLayout().toSerializable(event)); if (column.isUnicode()) { this.statement.setNClob(i++, reader); } else { this.statement.setClob(i++, reader); } } else if (column.isUnicode()) { this.statement.setNString(i++, column.getLayout().toSerializable(event)); } else { this.statement.setString(i++, column.getLayout().toSerializable(event)); } } if (this.isBatchSupported) { this.statement.addBatch(); } else if (this.statement.executeUpdate() == 0) { throw new AppenderLoggingException( "No records inserted in database table for log event in JDBC manager."); } } catch (final SQLException e) { throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " + e.getMessage(), e); } finally { Closer.closeSilently(reader); } }