diff --git a/slf4j-api/src/main/java/org/slf4j/StructuredData.java b/slf4j-api/src/main/java/org/slf4j/StructuredData.java index 7e3798023..6fb978c90 100644 --- a/slf4j-api/src/main/java/org/slf4j/StructuredData.java +++ b/slf4j-api/src/main/java/org/slf4j/StructuredData.java @@ -45,7 +45,7 @@ public interface StructuredData extends Serializable { * * @return The structured data id. */ - StructuredData.Id getId(); + StructuredDataId getId(); /** * A message String that is associated with the data. @@ -91,122 +91,5 @@ public interface StructuredData extends Serializable { * @param maps Additional data to include. * @return The formatted String. */ - String asString(String format, StructuredData.Id id, Map[] maps); - - /** - * The StructuredData identifier - */ - public class Id implements Serializable { - private static final long serialVersionUID = 9031746276396249990L; - - public static Id TIME_QUALITY = new Id("timeQuality", null, - new String[]{"tzKnown", "isSynced", "syncAccuracy"}); - public static Id ORIGIN = new Id("origin", null, - new String[]{"ip", "enterpriseId", "software", "swVersion"}); - public static Id META = new Id("meta", null, - new String[]{"sequenceId", "sysUpTime", "language"}); - - public static final int RESERVED = -1; - - private final String name; - private final int enterpriseNumber; - private final String[] required; - private final String[] optional; - - protected Id(String name, String[] required, String[] optional) { - int index = -1; - if (name != null) { - if (name.length() > 32) { - throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + name); - } - index = name.indexOf("@"); - } - - if (index > 0) { - this.name = name.substring(0, index); - this.enterpriseNumber = Integer.parseInt(name.substring(index+1)); - } else { - this.name = name; - this.enterpriseNumber = RESERVED; - } - this.required = required; - this.optional = optional; - } - - /** - * A Constructor that helps conformance to RFC 5424. - * - * @param name The name portion of the id. - * @param enterpriseNumber The enterprise number. - * @param required The list of keys that are required for this id. - * @param optional The list of keys that are optional for this id. - */ - public Id(String name, int enterpriseNumber, String[] required, String[] optional) { - if (name == null) { - throw new IllegalArgumentException("No structured id name was supplied"); - } - if (enterpriseNumber <= 0) { - throw new IllegalArgumentException("No enterprise number was supplied"); - } - this.name = name; - this.enterpriseNumber = enterpriseNumber; - String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber; - if (id.length() > 32) { - throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id); - } - this.required = required; - this.optional = optional; - } - - public Id makeId(StructuredData.Id id) { - if (id == null) { - return this; - } - return makeId(id.getName(), id.getEnterpriseNumber()); - } - - public Id makeId(String defaultId, int enterpriseNumber) { - String id; - String[] req; - String[] opt; - if (enterpriseNumber <= 0) { - return this; - } - if (this.name != null) { - id = this.name; - req = this.required; - opt = this.optional; - } else { - id = defaultId; - req = null; - opt = null; - } - - return new Id(id, enterpriseNumber, req, opt); - } - - public String[] getRequired() { - return required; - } - - public String[] getOptional() { - return optional; - } - - public String getName() { - return name; - } - - public int getEnterpriseNumber() { - return enterpriseNumber; - } - - public boolean isReserved() { - return enterpriseNumber <= 0; - } - - public String toString() { - return isReserved() ? name : name + "@" + enterpriseNumber; - } - } + String asString(String format, StructuredDataId id, Map[] maps); } diff --git a/slf4j-api/src/main/java/org/slf4j/StructuredDataId.java b/slf4j-api/src/main/java/org/slf4j/StructuredDataId.java new file mode 100644 index 000000000..ecaa19b55 --- /dev/null +++ b/slf4j-api/src/main/java/org/slf4j/StructuredDataId.java @@ -0,0 +1,120 @@ +package org.slf4j; + +import java.io.Serializable; + +/** + * The StructuredData identifier + */ +public class StructuredDataId implements Serializable { + private static final long serialVersionUID = 9031746276396249990L; + + public static StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null, + new String[]{"tzKnown", "isSynced", "syncAccuracy"}); + public static StructuredDataId ORIGIN = new StructuredDataId("origin", null, + new String[]{"ip", "enterpriseId", "software", "swVersion"}); + public static StructuredDataId META = new StructuredDataId("meta", null, + new String[]{"sequenceId", "sysUpTime", "language"}); + + public static final int RESERVED = -1; + + private final String name; + private final int enterpriseNumber; + private final String[] required; + private final String[] optional; + + protected StructuredDataId(String name, String[] required, String[] optional) { + int index = -1; + if (name != null) { + if (name.length() > 32) { + throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + name); + } + index = name.indexOf("@"); + } + + if (index > 0) { + this.name = name.substring(0, index); + this.enterpriseNumber = Integer.parseInt(name.substring(index + 1)); + } else { + this.name = name; + this.enterpriseNumber = RESERVED; + } + this.required = required; + this.optional = optional; + } + + /** + * A Constructor that helps conformance to RFC 5424. + * + * @param name The name portion of the id. + * @param enterpriseNumber The enterprise number. + * @param required The list of keys that are required for this id. + * @param optional The list of keys that are optional for this id. + */ + public StructuredDataId(String name, int enterpriseNumber, String[] required, String[] optional) { + if (name == null) { + throw new IllegalArgumentException("No structured id name was supplied"); + } + if (enterpriseNumber <= 0) { + throw new IllegalArgumentException("No enterprise number was supplied"); + } + this.name = name; + this.enterpriseNumber = enterpriseNumber; + String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber; + if (id.length() > 32) { + throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id); + } + this.required = required; + this.optional = optional; + } + + public StructuredDataId makeId(StructuredDataId id) { + if (id == null) { + return this; + } + return makeId(id.getName(), id.getEnterpriseNumber()); + } + + public StructuredDataId makeId(String defaultId, int enterpriseNumber) { + String id; + String[] req; + String[] opt; + if (enterpriseNumber <= 0) { + return this; + } + if (this.name != null) { + id = this.name; + req = this.required; + opt = this.optional; + } else { + id = defaultId; + req = null; + opt = null; + } + + return new StructuredDataId(id, enterpriseNumber, req, opt); + } + + public String[] getRequired() { + return required; + } + + public String[] getOptional() { + return optional; + } + + public String getName() { + return name; + } + + public int getEnterpriseNumber() { + return enterpriseNumber; + } + + public boolean isReserved() { + return enterpriseNumber <= 0; + } + + public String toString() { + return isReserved() ? name : name + "@" + enterpriseNumber; + } +} diff --git a/slf4j-api/src/main/java/org/slf4j/StructuredDataImpl.java b/slf4j-api/src/main/java/org/slf4j/StructuredDataImpl.java index 0cdb3d196..82ebd16f2 100644 --- a/slf4j-api/src/main/java/org/slf4j/StructuredDataImpl.java +++ b/slf4j-api/src/main/java/org/slf4j/StructuredDataImpl.java @@ -14,19 +14,19 @@ public class StructuredDataImpl implements StructuredData { private Map data = new HashMap(); - private StructuredData.Id id; + private StructuredDataId id; private String message; private String type; public StructuredDataImpl(final String id, final String msg, final String type) { - this.id = new Id(id, null, null); + this.id = new StructuredDataId(id, null, null); this.message = msg; this.type = type; } - public StructuredDataImpl(final StructuredData.Id id, final String msg, final String type) { + public StructuredDataImpl(final StructuredDataId id, final String msg, final String type) { this.id = id; this.message = msg; this.type = type; @@ -36,15 +36,15 @@ protected StructuredDataImpl() { } - public StructuredData.Id getId() { + public StructuredDataId getId() { return id; } protected void setId(String id) { - this.id = new StructuredData.Id(id, null, null); + this.id = new StructuredDataId(id, null, null); } - protected void setId(StructuredData.Id id) { + protected void setId(StructuredDataId id) { this.id = id; } @@ -96,7 +96,7 @@ public String asString(String format) { * @param maps Additional data to include. * @return The formatted String. */ - public final String asString(String format, StructuredData.Id structuredDataId, Map[] maps) { + public final String asString(String format, StructuredDataId structuredDataId, Map[] maps) { StringBuffer sb = new StringBuffer(); boolean full = FULL.equals(format); if (full) { @@ -106,7 +106,7 @@ public final String asString(String format, StructuredData.Id structuredDataId, } sb.append(getType()).append(" "); } - StructuredData.Id id = getId(); + StructuredDataId id = getId(); if (id != null) { id = id.makeId(structuredDataId); } else { diff --git a/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java b/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java index 254133c04..b24f88175 100755 --- a/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java +++ b/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java @@ -23,7 +23,7 @@ package org.slf4j.ext; import org.slf4j.StructuredDataImpl; -import org.slf4j.StructuredData; +import org.slf4j.StructuredDataId; import java.io.Serializable; import java.io.ByteArrayInputStream; @@ -386,7 +386,7 @@ public void putAll(Map map) { public Object remove(Object key) { Object oldValue = get(key); if (EVENT_ID.equals(key)) { - setId((StructuredData.Id) null); + setId((StructuredDataId) null); } else if (EVENT_MESSAGE.equals(key)) { setMessage(null); } else if (EVENT_TYPE.equals(key)) {