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.helpers.Booleans;
028import org.apache.logging.log4j.core.helpers.KeyValuePair;
029import org.apache.logging.log4j.message.StructuredDataId;
030
031/**
032 * A LoggerFields container.
033 */
034@Plugin(name = "LoggerFields", category = "Core", printObject = true)
035public final class LoggerFields {
036
037    private final Map<String, String> map;
038    private final String sdId;
039    private final String enterpriseId;
040    private final boolean discardIfAllFieldsAreEmpty;
041
042    private LoggerFields(final Map<String, String> map, String sdId, String enterpriseId,
043            boolean discardIfAllFieldsAreEmpty) {
044        this.sdId = sdId;
045        this.enterpriseId = enterpriseId;
046        this.map = Collections.unmodifiableMap(map);
047        this.discardIfAllFieldsAreEmpty = discardIfAllFieldsAreEmpty;
048    }
049
050    public Map<String, String> getMap() {
051        return this.map;
052    }
053
054    @Override
055    public String toString() {
056        return this.map.toString();
057    }
058
059    /**
060     * Create a LoggerFields from KeyValuePairs.
061     * 
062     * @param keyValuePairs
063     *            An array of KeyValuePairs.
064     * @param sdId
065     *            The SD-ID in an SD-ELEMENT
066     * @param enterpriseId
067     *            The IANA assigned enterprise number
068     * @param discardIfAllFieldsAreEmpty
069     *            this SD-ELEMENT should be discarded if all fields are empty
070     * @return A LoggerFields instance containing a Map<String, String>.
071     */
072    @PluginFactory
073    public static LoggerFields createLoggerFields(@PluginElement("LoggerFields") final KeyValuePair[] keyValuePairs,
074            @PluginAttribute("sdId") final String sdId, @PluginAttribute("enterpriseId") String enterpriseId,
075            @PluginAttribute("discardIfAllFieldsAreEmpty") String discardIfAllFieldsAreEmpty) {
076        final Map<String, String> map = new HashMap<String, String>();
077
078        for (final KeyValuePair keyValuePair : keyValuePairs) {
079            map.put(keyValuePair.getKey(), keyValuePair.getValue());
080        }
081
082        final boolean discardIfEmpty = Booleans.parseBoolean(discardIfAllFieldsAreEmpty, false);
083        return new LoggerFields(map, sdId, enterpriseId, discardIfEmpty);
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}