001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.message;
018
019import java.io.Serializable;
020
021import org.apache.logging.log4j.util.Strings;
022
023/**
024 * The StructuredData identifier.
025 */
026public class StructuredDataId implements Serializable {
027
028    /**
029     * RFC 5424 Time Quality.
030     */
031    public static final StructuredDataId TIME_QUALITY = new StructuredDataId("timeQuality", null, new String[] {
032            "tzKnown", "isSynced", "syncAccuracy"});
033
034    /**
035     * RFC 5424 Origin.
036     */
037    public static final StructuredDataId ORIGIN = new StructuredDataId("origin", null, new String[] {"ip",
038            "enterpriseId", "software", "swVersion"});
039
040    /**
041     * RFC 5424 Meta.
042     */
043    public static final StructuredDataId META = new StructuredDataId("meta", null, new String[] {"sequenceId",
044            "sysUpTime", "language"});
045
046    /**
047     * Reserved enterprise number.
048     */
049    public static final int RESERVED = -1;
050
051    private static final long serialVersionUID = 9031746276396249990L;
052    private static final int MAX_LENGTH = 32;
053    private static final String AT_SIGN = "@";
054
055    private final String name;
056    private final int enterpriseNumber;
057    private final String[] required;
058    private final String[] optional;
059
060    protected StructuredDataId(final String name, final String[] required, final String[] optional) {
061        int index = -1;
062        if (name != null) {
063            if (name.length() > MAX_LENGTH) {
064                throw new IllegalArgumentException(String.format("Length of id %s exceeds maximum of %d characters",
065                        name, MAX_LENGTH));
066            }
067            index = name.indexOf(AT_SIGN);
068        }
069
070        if (index > 0) {
071            this.name = name.substring(0, index);
072            this.enterpriseNumber = Integer.parseInt(name.substring(index + 1));
073        } else {
074            this.name = name;
075            this.enterpriseNumber = RESERVED;
076        }
077        this.required = required;
078        this.optional = optional;
079    }
080
081    /**
082     * A Constructor that helps conformance to RFC 5424.
083     *
084     * @param name The name portion of the id.
085     * @param enterpriseNumber The enterprise number.
086     * @param required The list of keys that are required for this id.
087     * @param optional The list of keys that are optional for this id.
088     */
089    public StructuredDataId(final String name, final int enterpriseNumber, final String[] required,
090            final String[] optional) {
091        if (name == null) {
092            throw new IllegalArgumentException("No structured id name was supplied");
093        }
094        if (name.contains(AT_SIGN)) {
095            throw new IllegalArgumentException("Structured id name cannot contain an " + Strings.quote(AT_SIGN));
096        }
097        if (enterpriseNumber <= 0) {
098            throw new IllegalArgumentException("No enterprise number was supplied");
099        }
100        this.name = name;
101        this.enterpriseNumber = enterpriseNumber;
102        final String id = name + AT_SIGN + 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     * 
113     * @param id The original StructuredDataId.
114     * @return the new StructuredDataId.
115     */
116    public StructuredDataId makeId(final StructuredDataId id) {
117        if (id == null) {
118            return this;
119        }
120        return makeId(id.getName(), id.getEnterpriseNumber());
121    }
122
123    /**
124     * Creates an id based on the current id.
125     * 
126     * @param defaultId The default id to use if this StructuredDataId doesn't have a name.
127     * @param anEnterpriseNumber The enterprise number.
128     * @return a StructuredDataId.
129     */
130    public StructuredDataId makeId(final String defaultId, final int anEnterpriseNumber) {
131        String id;
132        String[] req;
133        String[] opt;
134        if (anEnterpriseNumber <= 0) {
135            return this;
136        }
137        if (this.name != null) {
138            id = this.name;
139            req = this.required;
140            opt = this.optional;
141        } else {
142            id = defaultId;
143            req = null;
144            opt = null;
145        }
146
147        return new StructuredDataId(id, anEnterpriseNumber, req, opt);
148    }
149
150    /**
151     * Returns a list of required keys.
152     * 
153     * @return a List of required keys or null if none have been provided.
154     */
155    public String[] getRequired() {
156        return required;
157    }
158
159    /**
160     * Returns a list of optional keys.
161     * 
162     * @return a List of optional keys or null if none have been provided.
163     */
164    public String[] getOptional() {
165        return optional;
166    }
167
168    /**
169     * Returns the StructuredDataId name.
170     * 
171     * @return the StructuredDataId name.
172     */
173    public String getName() {
174        return name;
175    }
176
177    /**
178     * Returns the enterprise number.
179     * 
180     * @return the enterprise number.
181     */
182    public int getEnterpriseNumber() {
183        return enterpriseNumber;
184    }
185
186    /**
187     * Indicates if the id is reserved.
188     * 
189     * @return true if the id uses the reserved enterprise number, false otherwise.
190     */
191    public boolean isReserved() {
192        return enterpriseNumber <= 0;
193    }
194
195    @Override
196    public String toString() {
197        return isReserved() ? name : name + AT_SIGN + enterpriseNumber;
198    }
199}