View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.message;
18  
19  import java.io.Serializable;
20  
21  /**
22   * The StructuredData identifier.
23   */
24  public class StructuredDataId implements Serializable {
25  
26      /**
27       * RFC 5424 Time Quality.
28       */
29      public static final StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null,
30          new String[]{"tzKnown", "isSynced", "syncAccuracy"});
31      /**
32       * RFC 5424 Origin.
33       */
34      public static final StructuredDataId ORIGIN = new StructuredDataId("origin", null,
35          new String[]{"ip", "enterpriseId", "software", "swVersion"});
36      /**
37       * RFC 5424 Meta.
38       */
39      public static final StructuredDataId META = new StructuredDataId("meta", null,
40          new String[]{"sequenceId", "sysUpTime", "language"});
41  
42      /**
43       * Reserved enterprise number.
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       * A Constructor that helps conformance to RFC 5424.
78       *
79       * @param name             The name portion of the id.
80       * @param enterpriseNumber The enterprise number.
81       * @param required         The list of keys that are required for this id.
82       * @param optional         The list of keys that are optional for this id.
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      * Creates an id using another id to supply default values.
106      * @param id The original StructuredDataId.
107      * @return the new StructuredDataId.
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      * Creates an id based on the current id.
118      * @param defaultId The default id to use if this StructuredDataId doesn't have a name.
119      * @param enterpriseNumber The enterprise number.
120      * @return a StructuredDataId.
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      * Returns a list of required keys.
144      * @return a List of required keys or null if none have been provided.
145      */
146     public String[] getRequired() {
147         return required;
148     }
149 
150     /**
151      * Returns a list of optional keys.
152      * @return a List of optional keys or null if none have been provided.
153      */
154     public String[] getOptional() {
155         return optional;
156     }
157 
158     /**
159      * Returns the StructuredDataId name.
160      * @return the StructuredDataId name.
161      */
162     public String getName() {
163         return name;
164     }
165 
166     /**
167      * Returns the enterprise number.
168      * @return the enterprise number.
169      */
170     public int getEnterpriseNumber() {
171         return enterpriseNumber;
172     }
173 
174     /**
175      * Indicates if the id is reserved.
176      * @return true if the id uses the reserved enterprise number, false otherwise.
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 }