/** * Constructs a new instance based on an existing Map. * @param map The Map. * @return A new MapMessage */ @SuppressWarnings("unchecked") public M newInstance(final Map<String, V> map) { return (M) new MapMessage<>(map); }
/** * Adds an item to the data Map in fluent style. * @param key The name of the data item. * @param value The value of the data item. * @return {@code this} */ @SuppressWarnings("unchecked") public M with(final String key, final String value) { put(key, value); return (M) this; }
/** * Formats the Structured data as described in <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>. * * @param format The format identifier. * @return The formatted String. */ private StringBuilder format(final MapFormat format, final StringBuilder sb) { if (format == null) { appendMap(sb); } else { switch (format) { case XML : { asXml(sb); break; } case JSON : { asJson(sb); break; } case JAVA : { asJava(sb); break; } default : { appendMap(sb); } } } return sb; }
/** * Formats the Structured data as described in <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>. * * @param format The format identifier. * @return The formatted String. */ public String asString(final String format) { try { return format(EnglishEnums.valueOf(MapFormat.class, format), new StringBuilder()).toString(); } catch (final IllegalArgumentException ex) { return asString(); } }
public static void main(String args[]) { MapMessage mapMessage = new MapMessage(); mapMessage.put("Mic", "Mic123"); log4j2Tester.logger.debug(mapMessage,new Exception("Okies")); log4j2Tester.logger.debug(mapMessage); }
@Test public void testDataSourceConfig() throws Exception { try (final Connection connection = jdbcRule.getConnectionSource().getConnection()) { final Error exception = new Error("Final error massage is fatal!"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final PrintWriter writer = new PrintWriter(outputStream); exception.printStackTrace(writer); writer.close(); final Logger logger = LogManager.getLogger(this.getClass().getName() + ".testDataSourceConfig"); MapMessage mapMessage = new MapMessage(); mapMessage.with("Id", 1); mapMessage.with("ColumnA", "ValueA"); mapMessage.with("ColumnB", "ValueB"); logger.info(mapMessage); try (final Statement statement = connection.createStatement(); final ResultSet resultSet = statement .executeQuery("SELECT Id, ColumnA, ColumnB FROM dsLogEntry ORDER BY Id")) { assertTrue("There should be at least one row.", resultSet.next()); Assert.assertEquals(1, resultSet.getInt("Id")); assertFalse("There should not be two rows.", resultSet.next()); } } } }
/** * Formats the message and return it. * @return the formatted message. */ @Override public String getFormattedMessage() { return asString(); }
@Override public void formatTo(final StringBuilder buffer) { format((MapFormat) null, buffer); }
/** * Removes the element with the specified name. * @param key The name of the element. * @return The previous value of the element. */ public String remove(final String key) { final String result = get(key); data.remove(key); return result; }
private void asXml(StructuredDataId structuredDataId, StringBuilder sb) { sb.append("<StructuredData>\n"); sb.append("<type>").append(type).append("</type>\n"); sb.append("<id>").append(structuredDataId).append("</id>\n"); super.asXml(sb); sb.append("\n</StructuredData>\n"); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final StructuredDataMessage that = (StructuredDataMessage) o; if (!super.equals(o)) { return false; } if (type != null ? !type.equals(that.type) : that.type != null) { return false; } if (id != null ? !id.equals(that.id) : that.id != null) { return false; } if (message != null ? !message.equals(that.message) : that.message != null) { return false; } return true; }
/** * Formats the Structured data as described in <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>. * * @param format The format identifier. * @return The formatted String. */ public String asString(final String format) { try { return format(EnglishEnums.valueOf(MapFormat.class, format), new StringBuilder()).toString(); } catch (final IllegalArgumentException ex) { return asString(); } }
/** * * @param formats * An array of Strings that provide extra information about how to format the message. MapMessage uses * the first format specifier it recognizes. The supported formats are XML, JSON, and JAVA. The default * format is key1="value1" key2="value2" as required by <a href="https://tools.ietf.org/html/rfc5424">RFC * 5424</a> messages. * * @return The formatted message. */ @Override public String getFormattedMessage(final String[] formats) { return format(getFormat(formats), new StringBuilder()).toString(); }
@Override public String toString() { return asString(); }
/** * Formats the Structured data as described in <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>. * * @return The formatted String. */ public String asString() { return format((MapFormat) null, new StringBuilder()).toString(); }
protected boolean filter(final MapMessage<?, ?> mapMessage) { boolean match = false; for (int i = 0; i < map.size(); i++) { final String toMatch = mapMessage.get(map.getKeyAt(i)); match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch); if ((!isAnd && match) || (isAnd && !match)) { break; } } return match; }
private String asXml() { final StringBuilder sb = new StringBuilder(); final StructuredDataId sdId = getId(); if (sdId == null || sdId.getName() == null || type == null) { return sb.toString(); } sb.append("<StructuredData>\n"); sb.append("<type>").append(type).append("</type>\n"); sb.append("<id>").append(sdId).append("</id>\n"); super.asXml(sb); sb.append("</StructuredData>\n"); return sb.toString(); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final StructuredDataMessage that = (StructuredDataMessage) o; if (!super.equals(o)) { return false; } if (type != null ? !type.equals(that.type) : that.type != null) { return false; } if (id != null ? !id.equals(that.id) : that.id != null) { return false; } if (message != null ? !message.equals(that.message) : that.message != null) { return false; } return true; }
/** * Formats the Structured data as described in <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>. * * @param format The format identifier. * @return The formatted String. */ private StringBuilder format(final MapFormat format, final StringBuilder sb) { if (format == null) { appendMap(sb); } else { switch (format) { case XML : { asXml(sb); break; } case JSON : { asJson(sb); break; } case JAVA : { asJava(sb); break; } default : { appendMap(sb); } } } return sb; }