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