1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.message;
18
19 import java.io.Serializable;
20
21
22
23
24 public class StructuredDataId implements Serializable {
25
26
27
28
29 public static final StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null,
30 new String[]{"tzKnown", "isSynced", "syncAccuracy"});
31
32
33
34 public static final StructuredDataId ORIGIN = new StructuredDataId("origin", null,
35 new String[]{"ip", "enterpriseId", "software", "swVersion"});
36
37
38
39 public static final StructuredDataId META = new StructuredDataId("meta", null,
40 new String[]{"sequenceId", "sysUpTime", "language"});
41
42
43
44
45 public static final int RESERVED = -1;
46
47 private static final long serialVersionUID = 9031746276396249990L;
48 private static final int MAX_LENGTH = 32;
49
50 private final String name;
51 private final int enterpriseNumber;
52 private final String[] required;
53 private final String[] optional;
54
55
56 protected StructuredDataId(final String name, final String[] required, final String[] optional) {
57 int index = -1;
58 if (name != null) {
59 if (name.length() > MAX_LENGTH) {
60 throw new IllegalArgumentException(String.format("Length of id %s exceeds maximum of %d characters",
61 MAX_LENGTH, name));
62 }
63 index = name.indexOf("@");
64 }
65
66 if (index > 0) {
67 this.name = name.substring(0, index);
68 this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
69 } else {
70 this.name = name;
71 this.enterpriseNumber = RESERVED;
72 }
73 this.required = required;
74 this.optional = optional;
75 }
76
77
78
79
80
81
82
83
84
85 public StructuredDataId(final String name, final int enterpriseNumber, final String[] required,
86 final String[] optional) {
87 if (name == null) {
88 throw new IllegalArgumentException("No structured id name was supplied");
89 }
90 if (name.contains("@")) {
91 throw new IllegalArgumentException("Structured id name cannot contain an '@");
92 }
93 if (enterpriseNumber <= 0) {
94 throw new IllegalArgumentException("No enterprise number was supplied");
95 }
96 this.name = name;
97 this.enterpriseNumber = enterpriseNumber;
98 final String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber;
99 if (id.length() > MAX_LENGTH) {
100 throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id);
101 }
102 this.required = required;
103 this.optional = optional;
104 }
105
106
107
108
109
110
111 public StructuredDataId makeId(final StructuredDataId id) {
112 if (id == null) {
113 return this;
114 }
115 return makeId(id.getName(), id.getEnterpriseNumber());
116 }
117
118
119
120
121
122
123
124 public StructuredDataId makeId(final String defaultId, final int enterpriseNumber) {
125 String id;
126 String[] req;
127 String[] opt;
128 if (enterpriseNumber <= 0) {
129 return this;
130 }
131 if (this.name != null) {
132 id = this.name;
133 req = this.required;
134 opt = this.optional;
135 } else {
136 id = defaultId;
137 req = null;
138 opt = null;
139 }
140
141 return new StructuredDataId(id, enterpriseNumber, req, opt);
142 }
143
144
145
146
147
148 public String[] getRequired() {
149 return required;
150 }
151
152
153
154
155
156 public String[] getOptional() {
157 return optional;
158 }
159
160
161
162
163
164 public String getName() {
165 return name;
166 }
167
168
169
170
171
172 public int getEnterpriseNumber() {
173 return enterpriseNumber;
174 }
175
176
177
178
179
180 public boolean isReserved() {
181 return enterpriseNumber <= 0;
182 }
183
184 @Override
185 public String toString() {
186 return isReserved() ? name : name + "@" + enterpriseNumber;
187 }
188 }