001package org.apache.maven.toolchain.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
027import org.apache.maven.building.Problem;
028
029/**
030 * @author Robert Scholte
031 * @since 3.3.0
032 */
033public class ToolchainsBuildingException
034    extends Exception
035{
036
037    private final List<Problem> problems;
038
039    /**
040     * Creates a new exception with the specified problems.
041     *
042     * @param problems The problems that causes this exception, must not be {@code null}.
043     */
044    public ToolchainsBuildingException( List<Problem> problems )
045    {
046        super( toMessage( problems ) );
047
048        this.problems = new ArrayList<>();
049        if ( problems != null )
050        {
051            this.problems.addAll( problems );
052        }
053    }
054
055    /**
056     * Gets the problems that caused this exception.
057     *
058     * @return The problems that caused this exception, never {@code null}.
059     */
060    public List<Problem> getProblems()
061    {
062        return problems;
063    }
064
065    private static String toMessage( List<Problem> problems )
066    {
067        StringWriter buffer = new StringWriter( 1024 );
068
069        PrintWriter writer = new PrintWriter( buffer );
070
071        writer.print( problems.size() );
072        writer.print( ( problems.size() == 1 ) ? " problem was " : " problems were " );
073        writer.print( "encountered while building the effective toolchains" );
074        writer.println();
075
076        for ( Problem problem : problems )
077        {
078            writer.print( "[" );
079            writer.print( problem.getSeverity() );
080            writer.print( "] " );
081            writer.print( problem.getMessage() );
082            String location = problem.getLocation();
083            if ( !location.isEmpty() )
084            {
085                writer.print( " @ " );
086                writer.print( location );
087            }
088            writer.println();
089        }
090
091        return buffer.toString();
092    }
093}