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.core.layout;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginElement;
026import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027import org.apache.logging.log4j.core.util.KeyValuePair;
028import org.apache.logging.log4j.message.StructuredDataId;
029
030/**
031 * A LoggerFields container.
032 */
033@Plugin(name = "LoggerFields", category = "Core", printObject = true)
034public final class LoggerFields {
035
036    private final Map<String, String> map;
037    private final String sdId;
038    private final String enterpriseId;
039    private final boolean discardIfAllFieldsAreEmpty;
040
041    private LoggerFields(final Map<String, String> map, final String sdId, final String enterpriseId,
042            final boolean discardIfAllFieldsAreEmpty) {
043        this.sdId = sdId;
044        this.enterpriseId = enterpriseId;
045        this.map = Collections.unmodifiableMap(map);
046        this.discardIfAllFieldsAreEmpty = discardIfAllFieldsAreEmpty;
047    }
048
049    public Map<String, String> getMap() {
050        return this.map;
051    }
052
053    @Override
054    public String toString() {
055        return this.map.toString();
056    }
057
058    /**
059     * Create a LoggerFields from KeyValuePairs.
060     *
061     * @param keyValuePairs
062     *            An array of KeyValuePairs.
063     * @param sdId
064     *            The SD-ID in an SD-ELEMENT
065     * @param enterpriseId
066     *            The IANA assigned enterprise number
067     * @param discardIfAllFieldsAreEmpty
068     *            this SD-ELEMENT should be discarded if all fields are empty
069     * @return A LoggerFields instance containing a Map<String, String>.
070     */
071    @PluginFactory
072    public static LoggerFields createLoggerFields(
073        @PluginElement("LoggerFields") final KeyValuePair[] keyValuePairs,
074        @PluginAttribute("sdId") final String sdId,
075        @PluginAttribute("enterpriseId") final String enterpriseId,
076        @PluginAttribute(value = "discardIfAllFieldsAreEmpty", defaultBoolean = false) final boolean discardIfAllFieldsAreEmpty) {
077        final Map<String, String> map = new HashMap<String, String>();
078
079        for (final KeyValuePair keyValuePair : keyValuePairs) {
080            map.put(keyValuePair.getKey(), keyValuePair.getValue());
081        }
082
083        return new LoggerFields(map, sdId, enterpriseId, discardIfAllFieldsAreEmpty);
084    }
085
086    public StructuredDataId getSdId() {
087        if (enterpriseId == null || sdId == null) {
088            return null;
089        }
090        final int eId = Integer.parseInt(enterpriseId);
091        return new StructuredDataId(sdId, eId, null, null);
092    }
093
094    public boolean getDiscardIfAllFieldsAreEmpty() {
095        return discardIfAllFieldsAreEmpty;
096    }
097}