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.settings.building;
20
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * Signals one or more errors during settings building. The settings builder tries to collect as many problems as
28 * possible before eventually failing to provide callers with rich error information. Use {@link #getProblems()} to
29 * query the details of the failure.
30 *
31 * @deprecated since 4.0.0, use {@link org.apache.maven.api.services.SettingsBuilder} instead
32 */
33 @Deprecated(since = "4.0.0")
34 public class SettingsBuildingException extends Exception {
35
36 private final List<SettingsProblem> problems;
37
38 /**
39 * Creates a new exception with the specified problems.
40 *
41 * @param problems The problems that cause this exception, may be {@code null}.
42 */
43 public SettingsBuildingException(List<SettingsProblem> problems) {
44 super(toMessage(problems));
45
46 this.problems = new ArrayList<>();
47 if (problems != null) {
48 this.problems.addAll(problems);
49 }
50 }
51
52 /**
53 * Gets the problems that caused this exception.
54 *
55 * @return The problems that caused this exception, never {@code null}.
56 */
57 public List<SettingsProblem> getProblems() {
58 return problems;
59 }
60
61 private static String toMessage(List<SettingsProblem> problems) {
62 StringWriter buffer = new StringWriter(1024);
63
64 PrintWriter writer = new PrintWriter(buffer);
65
66 writer.print(problems.size());
67 writer.print((problems.size() == 1) ? " problem was " : " problems were ");
68 writer.print("encountered while building the effective settings");
69 writer.println();
70
71 for (SettingsProblem problem : problems) {
72 writer.print("[");
73 writer.print(problem.getSeverity());
74 writer.print("] ");
75 writer.print(problem.getMessage());
76 String location = problem.getLocation();
77 if (!location.isEmpty()) {
78 writer.print(" @ ");
79 writer.println(location);
80 }
81 }
82
83 return buffer.toString();
84 }
85 }