Author: rgoers
Date: Fri Oct 28 00:02:43 2011
New Revision: 1190099
URL: http://svn.apache.org/viewvc?rev=1190099&view=rev
Log:
Add MapMessage
Added:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
- copied, changed from r1188547,
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
- copied, changed from r1188547,
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/ParameterizedMessageTest.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
Copied:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
(from r1188547,
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java)
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java&r1=1188547&r2=1190099&rev=1190099&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
Fri Oct 28 00:02:43 2011
@@ -26,59 +26,33 @@ import java.util.TreeMap;
/**
* Represents a Message that conforms to RFC 5424
(http://tools.ietf.org/html/rfc5424).
*/
-public class StructuredDataMessage implements FormattedMessage, Serializable {
- /**
- * Full message format includes the type and message.
- */
- public static final String FULL = "full";
+public class MapMessage implements FormattedMessage, Serializable {
- private static final long serialVersionUID = 1703221292892071920L;
- private static final int MAX_LENGTH = 32;
+ public static final String XML = "XML";
private static final int HASHVAL = 31;
+ private static final long serialVersionUID = -5031471831131487120L;
- private Map<String, String> data = new HashMap<String, String>();
-
- private StructuredDataId id;
-
- private String message;
-
- private String type;
+ private final Map<String, String> data;
private String format = null;
/**
- * Constructor based on a String id.
- * @param id The String id.
- * @param msg The message.
- * @param type The message type.
+ * Constructor.
*/
- public StructuredDataMessage(final String id, final String msg, final
String type) {
- this.id = new StructuredDataId(id, null, null);
- this.message = msg;
- this.type = type;
+ public MapMessage() {
+ data = new HashMap<String, String>();
}
/**
- * Constructor based on a StructuredDataId.
- * @param id The StructuredDataId.
- * @param msg The message.
- * @param type The message type.
+ * Constructor based on an existing Map.
+ * @param map The Map.
*/
- public StructuredDataMessage(final StructuredDataId id, final String msg,
final String type) {
- this.id = id;
- this.message = msg;
- this.type = type;
+ public MapMessage(Map<String, String> map) {
+ this.data = map;
}
/**
- * Basic constructor.
- */
- protected StructuredDataMessage() {
-
- }
-
- /**
- * The format String. Specifying "full" will cause the type and message to
be included.
+ * The format String. Specifying "xml" will cause the message to be XML.
* @param format The message format.
*/
public void setFormat(String format) {
@@ -93,44 +67,6 @@ public class StructuredDataMessage imple
return this.format;
}
- /**
- * Return the id.
- * @return the StructuredDataId.
- */
- public StructuredDataId getId() {
- return id;
- }
-
- /**
- * Set the id from a String.
- * @param id The String id.
- */
- protected void setId(String id) {
- this.id = new StructuredDataId(id, null, null);
- }
-
- /**
- * Set the id.
- * @param id The StructuredDataId.
- */
- protected void setId(StructuredDataId id) {
- this.id = id;
- }
-
- /**
- * Set the type.
- * @return the type.
- */
- public String getType() {
- return type;
- }
-
- protected void setType(String type) {
- if (type.length() > MAX_LENGTH) {
- throw new IllegalArgumentException("Structured data type exceeds
maximum length of 32 characters: " + type);
- }
- this.type = type;
- }
/**
* Return the data elements as if they were parameters on the logging
event.
@@ -145,11 +81,7 @@ public class StructuredDataMessage imple
* @return the message.
*/
public String getMessageFormat() {
- return message;
- }
-
- protected void setMessageFormat(String msg) {
- this.message = msg;
+ return "";
}
/**
@@ -176,18 +108,19 @@ public class StructuredDataMessage imple
if (value == null) {
throw new IllegalArgumentException("No value provided for key " +
key);
}
- if (value.length() > MAX_LENGTH) {
- throw new IllegalArgumentException("Structured data values are
limited to 32 characters. key: " + key +
- " value: " + value);
- }
+ validate(key, value);
data.put(key, value);
}
+ protected void validate(String key, String value) {
+
+ }
+
/**
* Add all the elements from the specified Map.
* @param map The Map to add.
*/
- public void putAll(Map map) {
+ public void putAll(Map<String, String> map) {
data.putAll(map);
}
@@ -214,8 +147,8 @@ public class StructuredDataMessage imple
*
* @return The formatted String.
*/
- public final String asString() {
- return asString(FULL, null);
+ public String asString() {
+ return asString("");
}
/**
@@ -225,68 +158,41 @@ public class StructuredDataMessage imple
* @return The formatted String.
*/
public String asString(String format) {
- return asString(format, null);
- }
-
- /**
- * Format the Structured data as described in RFC 5424.
- *
- * @param format "full" will include the type and message. null
will return only the STRUCTURED-DATA as
- * described in RFC 5424
- * @param structuredDataId The SD-ID as described in RFC 5424. If null the
value in the StructuredData
- * will be used.
- * @return The formatted String.
- */
- public final String asString(String format, StructuredDataId
structuredDataId) {
- StringBuffer sb = new StringBuffer();
- boolean full = FULL.equals(format);
- if (full) {
- String type = getType();
- if (type == null) {
- return sb.toString();
- }
- sb.append(getType()).append(" ");
- }
- StructuredDataId id = getId();
- if (id != null) {
- id = id.makeId(structuredDataId);
+ StringBuilder sb = new StringBuilder();
+ if (format.equalsIgnoreCase(XML)) {
+ asXML(sb);
} else {
- id = structuredDataId;
- }
- if (id == null || id.getName() == null) {
- return sb.toString();
- }
- sb.append("[");
- sb.append(id);
- appendMap(getData(), sb);
- sb.append("]");
- if (full) {
- String msg = getMessageFormat();
- if (msg != null) {
- sb.append(" ").append(msg);
- }
+ appendMap(sb);
}
return sb.toString();
}
+ public void asXML(StringBuilder sb) {
+
+ }
+
/**
* Format the message and return it.
* @return the formatted message.
*/
public String getFormattedMessage() {
- return asString(FULL, null);
+ return asString();
}
- private void appendMap(Map map, StringBuffer sb) {
- SortedMap<String, Object> sorted = new TreeMap<String, Object>(map);
- for (Map.Entry<String, Object> entry : sorted.entrySet()) {
- sb.append(" ");
+ protected void appendMap(StringBuilder sb) {
+ SortedMap<String, String> sorted = new TreeMap<String, String>(data);
+ boolean first = true;
+ for (Map.Entry<String, String> entry : sorted.entrySet()) {
+ if (!first) {
+ sb.append(" ");
+ }
+ first = false;
sb.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
}
}
public String toString() {
- return asString((String) null);
+ return asString();
}
public boolean equals(Object o) {
@@ -297,29 +203,12 @@ public class StructuredDataMessage imple
return false;
}
- StructuredDataMessage that = (StructuredDataMessage) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) {
- 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;
- }
+ MapMessage that = (MapMessage) o;
- return true;
+ return this.data.equals(that.data);
}
public int hashCode() {
- int result = data != null ? data.hashCode() : 0;
- result = HASHVAL * result + (type != null ? type.hashCode() : 0);
- result = HASHVAL * result + (id != null ? id.hashCode() : 0);
- result = HASHVAL * result + (message != null ? message.hashCode() : 0);
- return result;
+ return data.hashCode();
}
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java?rev=1190099&r1=1190098&r2=1190099&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
Fri Oct 28 00:02:43 2011
@@ -17,16 +17,12 @@
package org.apache.logging.log4j.message;
import java.io.Serializable;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
/**
* Represents a Message that conforms to RFC 5424
(http://tools.ietf.org/html/rfc5424).
*/
-public class StructuredDataMessage implements FormattedMessage, Serializable {
+public class StructuredDataMessage extends MapMessage implements
FormattedMessage, Serializable {
/**
* Full message format includes the type and message.
*/
@@ -36,16 +32,12 @@ public class StructuredDataMessage imple
private static final int MAX_LENGTH = 32;
private static final int HASHVAL = 31;
- private Map<String, String> data = new HashMap<String, String>();
-
private StructuredDataId id;
private String message;
private String type;
- private String format = null;
-
/**
* Constructor based on a String id.
* @param id The String id.
@@ -58,6 +50,14 @@ public class StructuredDataMessage imple
this.type = type;
}
+ public StructuredDataMessage(final String id, final String msg, final
String type,
+ Map<String, String> data) {
+ super(data);
+ this.id = new StructuredDataId(id, null, null);
+ this.message = msg;
+ this.type = type;
+ }
+
/**
* Constructor based on a StructuredDataId.
* @param id The StructuredDataId.
@@ -78,22 +78,6 @@ public class StructuredDataMessage imple
}
/**
- * The format String. Specifying "full" will cause the type and message to
be included.
- * @param format The message format.
- */
- public void setFormat(String format) {
- this.format = format;
- }
-
- /**
- * Return the format String.
- * @return the format String.
- */
- public String getFormat() {
- return this.format;
- }
-
- /**
* Return the id.
* @return the StructuredDataId.
*/
@@ -131,15 +115,6 @@ public class StructuredDataMessage imple
}
this.type = type;
}
-
- /**
- * Return the data elements as if they were parameters on the logging
event.
- * @return the data elements.
- */
- public Object[] getParameters() {
- return data.values().toArray();
- }
-
/**
* Return the message.
* @return the message.
@@ -152,61 +127,13 @@ public class StructuredDataMessage imple
this.message = msg;
}
- /**
- * Return the message data as an unmodifiable Map.
- * @return the message data as an unmodifiable map.
- */
- public Map<String, String> getData() {
- return Collections.unmodifiableMap(data);
- }
-
- /**
- * Clear the data.
- */
- public void clear() {
- data.clear();
- }
- /**
- * Add an item to the data Map.
- * @param key The name of the data item.
- * @param value The value of the data item.
- */
- public void put(String key, String value) {
- if (value == null) {
- throw new IllegalArgumentException("No value provided for key " +
key);
- }
+ @Override
+ protected void validate(String key, String value) {
if (value.length() > MAX_LENGTH) {
throw new IllegalArgumentException("Structured data values are
limited to 32 characters. key: " + key +
" value: " + value);
}
- data.put(key, value);
- }
-
- /**
- * Add all the elements from the specified Map.
- * @param map The Map to add.
- */
- public void putAll(Map map) {
- data.putAll(map);
- }
-
- /**
- * Retrieve the value of the element with the specified key or null if the
key is not present.
- * @param key The name of the element.
- * @return The value of the element or null if the key is not present.
- */
- public String get(String key) {
- return data.get(key);
- }
-
- /**
- * Remove the element with the specified name.
- * @param key The name of the element.
- * @return The previous value of the element.
- */
- public String remove(String key) {
- return data.remove(key);
}
/**
@@ -214,7 +141,8 @@ public class StructuredDataMessage imple
*
* @return The formatted String.
*/
- public final String asString() {
+ @Override
+ public String asString() {
return asString(FULL, null);
}
@@ -224,6 +152,7 @@ public class StructuredDataMessage imple
* @param format The format identifier. Ignored in this implementation.
* @return The formatted String.
*/
+
public String asString(String format) {
return asString(format, null);
}
@@ -238,7 +167,7 @@ public class StructuredDataMessage imple
* @return The formatted String.
*/
public final String asString(String format, StructuredDataId
structuredDataId) {
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
boolean full = FULL.equals(format);
if (full) {
String type = getType();
@@ -258,7 +187,8 @@ public class StructuredDataMessage imple
}
sb.append("[");
sb.append(id);
- appendMap(getData(), sb);
+ sb.append(" ");
+ appendMap(sb);
sb.append("]");
if (full) {
String msg = getMessageFormat();
@@ -273,20 +203,14 @@ public class StructuredDataMessage imple
* Format the message and return it.
* @return the formatted message.
*/
+ @Override
public String getFormattedMessage() {
return asString(FULL, null);
}
- private void appendMap(Map map, StringBuffer sb) {
- SortedMap<String, Object> sorted = new TreeMap<String, Object>(map);
- for (Map.Entry<String, Object> entry : sorted.entrySet()) {
- sb.append(" ");
-
sb.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
- }
- }
-
+ @Override
public String toString() {
- return asString((String) null);
+ return asString(null);
}
public boolean equals(Object o) {
@@ -299,7 +223,7 @@ public class StructuredDataMessage imple
StructuredDataMessage that = (StructuredDataMessage) o;
- if (data != null ? !data.equals(that.data) : that.data != null) {
+ if (!super.equals(o)) {
return false;
}
if (type != null ? !type.equals(that.type) : that.type != null) {
@@ -316,7 +240,7 @@ public class StructuredDataMessage imple
}
public int hashCode() {
- int result = data != null ? data.hashCode() : 0;
+ int result = super.hashCode();
result = HASHVAL * result + (type != null ? type.hashCode() : 0);
result = HASHVAL * result + (id != null ? id.hashCode() : 0);
result = HASHVAL * result + (message != null ? message.hashCode() : 0);
Copied:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
(from r1188547,
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/ParameterizedMessageTest.java)
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/ParameterizedMessageTest.java&r1=1188547&r2=1190099&rev=1190099&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/ParameterizedMessageTest.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
Fri Oct 28 00:02:43 2011
@@ -19,24 +19,20 @@ package org.apache.logging.log4j.message
import org.junit.Test;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
/**
*
*/
-public class ParameterizedMessageTest {
+public class MapMessageTest {
@Test
- public void testNoArgs() {
+ public void testMap() {
String testMsg = "Test message {}";
- ParameterizedMessage msg = new ParameterizedMessage(testMsg, null);
+ MapMessage msg = new MapMessage();
+ msg.put("message", testMsg);
+ msg.put("project", "Log4j");
String result = msg.getFormattedMessage();
- assertEquals(testMsg, result);
- Object[] array = null;
- msg = new ParameterizedMessage(testMsg, array, null);
- result = msg.getFormattedMessage();
- assertEquals(testMsg, result);
+ String expected = "message=\"Test message {}\" project=\"Log4j\"";
+ assertEquals(expected, result);
}
}
Added:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java?rev=1190099&view=auto
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java
(added)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java
Fri Oct 28 00:02:43 2011
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.message;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ *
+ */
+public class StructuredDataMessageTest {
+
+ @Test
+ public void testMsg() {
+ String testMsg = "Test message {}";
+ StructuredDataMessage msg = new StructuredDataMessage("MsgId@12345",
testMsg, "Alert");
+ msg.put("message", testMsg);
+ msg.put("project", "Log4j");
+ String result = msg.getFormattedMessage();
+ String expected = "Alert [MsgId@12345 message=\"Test message {}\"
project=\"Log4j\"] Test message {}";
+ assertEquals(expected, result);
+ }
+}