001package org.apache.maven.settings.building;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.PrintWriter;
023import java.io.StringWriter;
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * Signals one ore more errors during settings building. The settings builder tries to collect as many problems as
029 * possible before eventually failing to provide callers with rich error information. Use {@link #getProblems()} to
030 * query the details of the failure.
031 *
032 * @author Benjamin Bentmann
033 */
034public class SettingsBuildingException
035    extends Exception
036{
037
038    private final List<SettingsProblem> problems;
039
040    /**
041     * Creates a new exception with the specified problems.
042     *
043     * @param problems The problems that causes this exception, may be {@code null}.
044     */
045    public SettingsBuildingException( List<SettingsProblem> problems )
046    {
047        super( toMessage( problems ) );
048
049        this.problems = new ArrayList<>();
050        if ( problems != null )
051        {
052            this.problems.addAll( problems );
053        }
054    }
055
056    /**
057     * Gets the problems that caused this exception.
058     *
059     * @return The problems that caused this exception, never {@code null}.
060     */
061    public List<SettingsProblem> getProblems()
062    {
063        return problems;
064    }
065
066    private static String toMessage( List<SettingsProblem> problems )
067    {
068        StringWriter buffer = new StringWriter( 1024 );
069
070        PrintWriter writer = new PrintWriter( buffer );
071
072        writer.print( problems.size() );
073        writer.print( ( problems.size() == 1 ) ? " problem was " : " problems were " );
074        writer.print( "encountered while building the effective settings" );
075        writer.println();
076
077        for ( SettingsProblem problem : problems )
078        {
079            writer.print( "[" );
080            writer.print( problem.getSeverity() );
081            writer.print( "] " );
082            writer.print( problem.getMessage() );
083            writer.print( " @ " );
084            writer.println( problem.getLocation() );
085        }
086
087        return buffer.toString();
088    }
089
090}