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 import org.apache.logging.log4j.util.StringBuilderFormattable;
22 import org.apache.logging.log4j.util.Strings;
23
24
25
26
27 public class StructuredDataId implements Serializable, StringBuilderFormattable {
28
29
30
31
32 public static final StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null, new String[] {
33 "tzKnown", "isSynced", "syncAccuracy"});
34
35
36
37
38 public static final StructuredDataId ORIGIN = new StructuredDataId("origin", null, new String[] {"ip",
39 "enterpriseId", "software", "swVersion"});
40
41
42
43
44 public static final StructuredDataId META = new StructuredDataId("meta", null, new String[] {"sequenceId",
45 "sysUpTime", "language"});
46
47
48
49
50 public static final int RESERVED = -1;
51
52 private static final long serialVersionUID = 9031746276396249990L;
53 private static final int MAX_LENGTH = 32;
54 private static final String AT_SIGN = "@";
55
56 private final String name;
57 private final int enterpriseNumber;
58 private final String[] required;
59 private final String[] optional;
60
61
62
63
64
65
66 public StructuredDataId(final String name) {
67 this(name, null, null, MAX_LENGTH);
68 }
69
70
71
72
73
74
75
76 public StructuredDataId(final String name, final int maxLength) {
77 this(name, null, null, maxLength);
78 }
79
80
81
82
83
84
85
86 public StructuredDataId(final String name, final String[] required, final String[] optional) {
87 this(name, required, optional, MAX_LENGTH);
88 }
89
90
91
92
93
94
95
96
97
98 public StructuredDataId(final String name, final String[] required, final String[] optional, int maxLength) {
99 int index = -1;
100 if (name != null) {
101 if (maxLength <= 0) {
102 maxLength = MAX_LENGTH;
103 }
104 if (name.length() > maxLength) {
105 throw new IllegalArgumentException(String.format("Length of id %s exceeds maximum of %d characters",
106 name, maxLength));
107 }
108 index = name.indexOf(AT_SIGN);
109 }
110
111 if (index > 0) {
112 this.name = name.substring(0, index);
113 this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
114 } else {
115 this.name = name;
116 this.enterpriseNumber = RESERVED;
117 }
118 this.required = required;
119 this.optional = optional;
120 }
121
122
123
124
125
126
127
128
129
130 public StructuredDataId(final String name, final int enterpriseNumber, final String[] required,
131 final String[] optional) {
132 this(name, enterpriseNumber, required, optional, MAX_LENGTH);
133 }
134
135
136
137
138
139
140
141
142
143
144
145 public StructuredDataId(final String name, final int enterpriseNumber, final String[] required,
146 final String[] optional, final int maxLength) {
147 if (name == null) {
148 throw new IllegalArgumentException("No structured id name was supplied");
149 }
150 if (name.contains(AT_SIGN)) {
151 throw new IllegalArgumentException("Structured id name cannot contain an " + Strings.quote(AT_SIGN));
152 }
153 if (enterpriseNumber <= 0) {
154 throw new IllegalArgumentException("No enterprise number was supplied");
155 }
156 this.name = name;
157 this.enterpriseNumber = enterpriseNumber;
158 final String id = name + AT_SIGN + enterpriseNumber;
159 if (maxLength > 0 && id.length() > maxLength) {
160 throw new IllegalArgumentException("Length of id exceeds maximum of " + maxLength + " characters: " + id);
161 }
162 this.required = required;
163 this.optional = optional;
164 }
165
166
167
168
169
170
171
172 public StructuredDataId makeId(final StructuredDataId id) {
173 if (id == null) {
174 return this;
175 }
176 return makeId(id.getName(), id.getEnterpriseNumber());
177 }
178
179
180
181
182
183
184
185
186 public StructuredDataId makeId(final String defaultId, final int anEnterpriseNumber) {
187 String id;
188 String[] req;
189 String[] opt;
190 if (anEnterpriseNumber <= 0) {
191 return this;
192 }
193 if (this.name != null) {
194 id = this.name;
195 req = this.required;
196 opt = this.optional;
197 } else {
198 id = defaultId;
199 req = null;
200 opt = null;
201 }
202
203 return new StructuredDataId(id, anEnterpriseNumber, req, opt);
204 }
205
206
207
208
209
210
211 public String[] getRequired() {
212 return required;
213 }
214
215
216
217
218
219
220 public String[] getOptional() {
221 return optional;
222 }
223
224
225
226
227
228
229 public String getName() {
230 return name;
231 }
232
233
234
235
236
237
238 public int getEnterpriseNumber() {
239 return enterpriseNumber;
240 }
241
242
243
244
245
246
247 public boolean isReserved() {
248 return enterpriseNumber <= 0;
249 }
250
251 @Override
252 public String toString() {
253 final StringBuilder sb = new StringBuilder(name.length() + 10);
254 formatTo(sb);
255 return sb.toString();
256 }
257
258 @Override
259 public void formatTo(final StringBuilder buffer) {
260 if (isReserved()) {
261 buffer.append(name);
262 } else {
263 buffer.append(name).append(AT_SIGN).append(enterpriseNumber);
264 }
265 }
266 }