View Javadoc
1   package org.apache.maven.toolchain.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.building.Problem;
28  
29  /**
30   * @author Robert Scholte
31   * @since 3.3.0
32   */
33  public class ToolchainsBuildingException
34      extends Exception
35  {
36  
37      private final List<Problem> problems;
38  
39      /**
40       * Creates a new exception with the specified problems.
41       *
42       * @param problems The problems that causes this exception, must not be {@code null}.
43       */
44      public ToolchainsBuildingException( List<Problem> problems )
45      {
46          super( toMessage( problems ) );
47  
48          this.problems = new ArrayList<Problem>();
49          if ( problems != null )
50          {
51              this.problems.addAll( problems );
52          }
53      }
54  
55      /**
56       * Gets the problems that caused this exception.
57       *
58       * @return The problems that caused this exception, never {@code null}.
59       */
60      public List<Problem> getProblems()
61      {
62          return problems;
63      }
64  
65      private static String toMessage( List<Problem> problems )
66      {
67          StringWriter buffer = new StringWriter( 1024 );
68  
69          PrintWriter writer = new PrintWriter( buffer );
70  
71          writer.print( problems.size() );
72          writer.print( ( problems.size() == 1 ) ? " problem was " : " problems were " );
73          writer.print( "encountered while building the effective toolchains" );
74          writer.println();
75  
76          for ( Problem problem : problems )
77          {
78              writer.print( "[" );
79              writer.print( problem.getSeverity() );
80              writer.print( "] " );
81              writer.print( problem.getMessage() );
82              String location = problem.getLocation();
83              if ( !location.isEmpty() )
84              {
85                  writer.print( " @ " );
86                  writer.print( location );
87              }
88              writer.println();
89          }
90  
91          return buffer.toString();
92      }
93  }