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      private static final String AT = "@";
27  
28      /**
29       * RFC 5424 Time Quality.
30       */
31      public static final StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null,
32          new String[]{"tzKnown", "isSynced", "syncAccuracy"});
33      
34      /**
35       * RFC 5424 Origin.
36       */
37      public static final StructuredDataId ORIGIN = new StructuredDataId("origin", null,
38          new String[]{"ip", "enterpriseId", "software", "swVersion"});
39      
40      /**
41       * RFC 5424 Meta.
42       */
43      public static final StructuredDataId META = new StructuredDataId("meta", null,
44          new String[]{"sequenceId", "sysUpTime", "language"});
45  
46      /**
47       * Reserved enterprise number.
48       */
49      public static final int RESERVED = -1;
50  
51      private static final long serialVersionUID = 9031746276396249990L;
52      private static final int MAX_LENGTH = 32;
53  
54      private final String name;
55      private final int enterpriseNumber;
56      private final String[] required;
57      private final String[] optional;
58  
59  
60      protected StructuredDataId(final String name, final String[] required, final String[] optional) {
61          int index = -1;
62          if (name != null) {
63              if (name.length() > MAX_LENGTH) {
64                  throw new IllegalArgumentException(String.format("Length of id %s exceeds maximum of %d characters",
65                          name, MAX_LENGTH));
66              }
67              index = name.indexOf(AT);
68          }
69  
70          if (index > 0) {
71              this.name = name.substring(0, index);
72              this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
73          } else {
74              this.name = name;
75              this.enterpriseNumber = RESERVED;
76          }
77          this.required = required;
78          this.optional = optional;
79      }
80  
81      /**
82       * A Constructor that helps conformance to RFC 5424.
83       *
84       * @param name             The name portion of the id.
85       * @param enterpriseNumber The enterprise number.
86       * @param required         The list of keys that are required for this id.
87       * @param optional         The list of keys that are optional for this id.
88       */
89      public StructuredDataId(final String name, final int enterpriseNumber, final String[] required,
90                              final String[] optional) {
91          if (name == null) {
92              throw new IllegalArgumentException("No structured id name was supplied");
93          }
94          if (name.contains(AT)) {
95              throw new IllegalArgumentException("Structured id name cannot contain an '" + AT + "'");
96          }
97          if (enterpriseNumber <= 0) {
98              throw new IllegalArgumentException("No enterprise number was supplied");
99          }
100         this.name = name;
101         this.enterpriseNumber = enterpriseNumber;
102         final String id = enterpriseNumber < 0 ? name : name + AT + enterpriseNumber;
103         if (id.length() > MAX_LENGTH) {
104             throw new IllegalArgumentException("Length of id exceeds maximum of 32 characters: " + id);
105         }
106         this.required = required;
107         this.optional = optional;
108     }
109 
110     /**
111      * Creates an id using another id to supply default values.
112      * @param id The original StructuredDataId.
113      * @return the new StructuredDataId.
114      */
115     public StructuredDataId makeId(final StructuredDataId id) {
116         if (id == null) {
117             return this;
118         }
119         return makeId(id.getName(), id.getEnterpriseNumber());
120     }
121 
122     /**
123      * Creates an id based on the current id.
124      * @param defaultId The default id to use if this StructuredDataId doesn't have a name.
125      * @param enterpriseNumber The enterprise number.
126      * @return a StructuredDataId.
127      */
128     public StructuredDataId makeId(final String defaultId, final int enterpriseNumber) {
129         String id;
130         String[] req;
131         String[] opt;
132         if (enterpriseNumber <= 0) {
133             return this;
134         }
135         if (this.name != null) {
136             id = this.name;
137             req = this.required;
138             opt = this.optional;
139         } else {
140             id = defaultId;
141             req = null;
142             opt = null;
143         }
144 
145         return new StructuredDataId(id, enterpriseNumber, req, opt);
146     }
147 
148     /**
149      * Returns a list of required keys.
150      * @return a List of required keys or null if none have been provided.
151      */
152     public String[] getRequired() {
153         return required;
154     }
155 
156     /**
157      * Returns a list of optional keys.
158      * @return a List of optional keys or null if none have been provided.
159      */
160     public String[] getOptional() {
161         return optional;
162     }
163 
164     /**
165      * Returns the StructuredDataId name.
166      * @return the StructuredDataId name.
167      */
168     public String getName() {
169         return name;
170     }
171 
172     /**
173      * Returns the enterprise number.
174      * @return the enterprise number.
175      */
176     public int getEnterpriseNumber() {
177         return enterpriseNumber;
178     }
179 
180     /**
181      * Indicates if the id is reserved.
182      * @return true if the id uses the reserved enterprise number, false otherwise.
183      */
184     public boolean isReserved() {
185         return enterpriseNumber <= 0;
186     }
187 
188     @Override
189     public String toString() {
190         return isReserved() ? name : name + AT + enterpriseNumber;
191     }
192 }