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(String name, String[] required, String[] optional) {
57 int index = -1;
58 if (name != null) {
59 if (name.length() > MAX_LENGTH) {
60 throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + name);
61 }
62 index = name.indexOf("@");
63 }
64
65 if (index > 0) {
66 this.name = name.substring(0, index);
67 this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
68 } else {
69 this.name = name;
70 this.enterpriseNumber = RESERVED;
71 }
72 this.required = required;
73 this.optional = optional;
74 }
75
76
77
78
79
80
81
82
83
84 public StructuredDataId(String name, int enterpriseNumber, String[] required, String[] optional) {
85 if (name == null) {
86 throw new IllegalArgumentException("No structured id name was supplied");
87 }
88 if (name.contains("@")) {
89 throw new IllegalArgumentException("Structured id name cannot contain an '@");
90 }
91 if (enterpriseNumber <= 0) {
92 throw new IllegalArgumentException("No enterprise number was supplied");
93 }
94 this.name = name;
95 this.enterpriseNumber = enterpriseNumber;
96 String id = enterpriseNumber < 0 ? name : name + "@" + enterpriseNumber;
97 if (id.length() > MAX_LENGTH) {
98 throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id);
99 }
100 this.required = required;
101 this.optional = optional;
102 }
103
104
105
106
107
108
109 public StructuredDataId makeId(StructuredDataId id) {
110 if (id == null) {
111 return this;
112 }
113 return makeId(id.getName(), id.getEnterpriseNumber());
114 }
115
116
117
118
119
120
121
122 public StructuredDataId makeId(String defaultId, int enterpriseNumber) {
123 String id;
124 String[] req;
125 String[] opt;
126 if (enterpriseNumber <= 0) {
127 return this;
128 }
129 if (this.name != null) {
130 id = this.name;
131 req = this.required;
132 opt = this.optional;
133 } else {
134 id = defaultId;
135 req = null;
136 opt = null;
137 }
138
139 return new StructuredDataId(id, enterpriseNumber, req, opt);
140 }
141
142
143
144
145
146 public String[] getRequired() {
147 return required;
148 }
149
150
151
152
153
154 public String[] getOptional() {
155 return optional;
156 }
157
158
159
160
161
162 public String getName() {
163 return name;
164 }
165
166
167
168
169
170 public int getEnterpriseNumber() {
171 return enterpriseNumber;
172 }
173
174
175
176
177
178 public boolean isReserved() {
179 return enterpriseNumber <= 0;
180 }
181
182 @Override
183 public String toString() {
184 return isReserved() ? name : name + "@" + enterpriseNumber;
185 }
186 }