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(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       * A Constructor that helps conformance to RFC 5424.
79       *
80       * @param name             The name portion of the id.
81       * @param enterpriseNumber The enterprise number.
82       * @param required         The list of keys that are required for this id.
83       * @param optional         The list of keys that are optional for this id.
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      * Creates an id using another id to supply default values.
108      * @param id The original StructuredDataId.
109      * @return the new StructuredDataId.
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      * Creates an id based on the current id.
120      * @param defaultId The default id to use if this StructuredDataId doesn't have a name.
121      * @param enterpriseNumber The enterprise number.
122      * @return a StructuredDataId.
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      * Returns a list of required keys.
146      * @return a List of required keys or null if none have been provided.
147      */
148     public String[] getRequired() {
149         return required;
150     }
151 
152     /**
153      * Returns a list of optional keys.
154      * @return a List of optional keys or null if none have been provided.
155      */
156     public String[] getOptional() {
157         return optional;
158     }
159 
160     /**
161      * Returns the StructuredDataId name.
162      * @return the StructuredDataId name.
163      */
164     public String getName() {
165         return name;
166     }
167 
168     /**
169      * Returns the enterprise number.
170      * @return the enterprise number.
171      */
172     public int getEnterpriseNumber() {
173         return enterpriseNumber;
174     }
175 
176     /**
177      * Indicates if the id is reserved.
178      * @return true if the id uses the reserved enterprise number, false otherwise.
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 }