    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 *      https://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.commons.lang3.exception;
    018
    019import java.io.Serializable;
    020import java.util.ArrayList;
    021import java.util.List;
    022import java.util.Objects;
    023import java.util.Set;
    024import java.util.stream.Collectors;
    025import java.util.stream.Stream;
    026
    027import org.apache.commons.lang3.Strings;
    028import org.apache.commons.lang3.tuple.ImmutablePair;
    029import org.apache.commons.lang3.tuple.Pair;
    030
    031/**
    032 * Default implementation of the context storing the label-value pairs for contexted exceptions.
    033 * <p>
    034 * This implementation is serializable, however this is dependent on the values that
    035 * are added also being serializable.
    036 * </p>
    037 *
    038 * @see ContextedException
    039 * @see ContextedRuntimeException
    040 * @since 3.0
    041 */
    042public class DefaultExceptionContext implements ExceptionContext, Serializable {
    043
    044    /** The serialization version. */
    045    private static final long serialVersionUID = 20110706L;
    046
    047    /** The list storing the label-data pairs. */
    048    private final List<Pair<String, Object>> contextValues = new ArrayList<>();
    049
    050    /**
    051     * Constructs a new instance.
    052     */
    053    public DefaultExceptionContext() {
    054        // empty
    055    }
    056
    057    /**
    058     * {@inheritDoc}
    059     */
    060    @Override
    061    public DefaultExceptionContext addContextValue(final String label, final Object value) {
    062        contextValues.add(new ImmutablePair<>(label, value));
    063        return this;
    064    }
    065
    066    /**
    067     * {@inheritDoc}
    068     */
    069    @Override
    070    public List<Pair<String, Object>> getContextEntries() {
    071        return contextValues;
    072    }
    073
    074    /**
    075     * {@inheritDoc}
    076     */
    077    @Override
    078    public Set<String> getContextLabels() {
    079        return stream().map(Pair::getKey).collect(Collectors.toSet());
    080    }
    081
    082    /**
    083     * {@inheritDoc}
    084     */
    085    @Override
    086    public List<Object> getContextValues(final String label) {
    087        return stream().filter(pair -> Strings.CS.equals(label, pair.getKey())).map(Pair::getValue).collect(Collectors.toList());
    088    }
    089
    090    /**
    091     * {@inheritDoc}
    092     */
    093    @Override
    094    public Object getFirstContextValue(final String label) {
    095        return stream().filter(pair -> Strings.CS.equals(label, pair.getKey())).findFirst().map(Pair::getValue).orElse(null);
    096    }
    097
    098    /**
    099     * Builds the message containing the contextual information.
    100     *
    101     * @param baseMessage  the base exception message <strong>without</strong> context information appended
    102     * @return the exception message <strong>with</strong> context information appended, never null
    103     */
    104    @Override
    105    public String getFormattedExceptionMessage(final String baseMessage) {
    106        final StringBuilder buffer = new StringBuilder(256);
    107        if (baseMessage != null) {
    108            buffer.append(baseMessage);
    109        }
    110        if (!contextValues.isEmpty()) {
    111            if (buffer.length() > 0) {
    112                buffer.append('\n');
    113            }
    114            buffer.append("Exception Context:\n");
    115            int i = 0;
    116            for (final Pair<String, Object> pair : contextValues) {
    117                buffer.append("\t[");
    118                buffer.append(++i);
    119                buffer.append(':');
    120                buffer.append(pair.getKey());
    121                buffer.append("=");
    122                final Object value = pair.getValue();
    123                try {
    124                    buffer.append(Objects.toString(value));
    125                } catch (final Exception e) {
    126                    buffer.append("Exception thrown on toString(): ");
    127                    buffer.append(ExceptionUtils.getStackTrace(e));
    128                }
    129                buffer.append("]\n");
    130            }
    131            buffer.append("---------------------------------");
    132        }
    133        return buffer.toString();
    134    }
    135
    136    /**
    137     * {@inheritDoc}
    138     */
    139    @Override
    140    public DefaultExceptionContext setContextValue(final String label, final Object value) {
    141        contextValues.removeIf(p -> Strings.CS.equals(label, p.getKey()));
    142        addContextValue(label, value);
    143        return this;
    144    }
    145
    146    private Stream<Pair<String, Object>> stream() {
    147        return contextValues.stream();
    148    }
    149
    150}