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.model.building;
20  
21  import java.io.PrintWriter;
22  import java.io.StringWriter;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.model.Model;
27  
28  /**
29   * Signals one ore more errors during model building. The model builder tries to collect as many problems as possible
30   * before eventually failing to provide callers with rich error information. Use {@link #getProblems()} to query the
31   * details of the failure.
32   *
33   * @author Benjamin Bentmann
34   */
35  public class ModelBuildingException extends Exception {
36  
37      private final ModelBuildingResult result;
38  
39      /**
40       * Creates a new exception with the specified problems.
41       *
42       * @param model The model that could not be built, may be {@code null}.
43       * @param modelId The identifier of the model that could not be built, may be {@code null}.
44       * @param problems The problems that causes this exception, may be {@code null}.
45       * @deprecated Use {@link #ModelBuildingException(ModelBuildingResult)} instead.
46       */
47      @Deprecated
48      public ModelBuildingException(Model model, String modelId, List<ModelProblem> problems) {
49          super(toMessage(modelId, problems));
50  
51          if (model != null) {
52              DefaultModelBuildingResult tmp = new DefaultModelBuildingResult();
53              if (modelId == null) {
54                  modelId = "";
55              }
56              tmp.addModelId(modelId);
57              tmp.setRawModel(modelId, model);
58              tmp.setProblems(problems);
59              result = tmp;
60          } else {
61              result = null;
62          }
63      }
64  
65      /**
66       * Creates a new exception from the specified interim result and its associated problems.
67       *
68       * @param result The interim result, may be {@code null}.
69       */
70      public ModelBuildingException(ModelBuildingResult result) {
71          super(toMessage(result));
72          this.result = result;
73      }
74  
75      /**
76       * Gets the interim result of the model building up to the point where it failed.
77       *
78       * @return The interim model building result or {@code null} if not available.
79       */
80      public ModelBuildingResult getResult() {
81          return result;
82      }
83  
84      /**
85       * Gets the model that could not be built properly.
86       *
87       * @return The erroneous model or {@code null} if not available.
88       */
89      public Model getModel() {
90          if (result == null) {
91              return null;
92          }
93          if (result.getEffectiveModel() != null) {
94              return result.getEffectiveModel();
95          }
96          return result.getRawModel();
97      }
98  
99      /**
100      * Gets the identifier of the POM whose effective model could not be built. The general format of the identifier is
101      * {@code <groupId>:<artifactId>:<version>} but some of these coordinates may still be unknown at the point the
102      * exception is thrown so this information is merely meant to assist the user.
103      *
104      * @return The identifier of the POM or an empty string if not known, never {@code null}.
105      */
106     public String getModelId() {
107         if (result == null || result.getModelIds().isEmpty()) {
108             return "";
109         }
110         return result.getModelIds().get(0);
111     }
112 
113     /**
114      * Gets the problems that caused this exception.
115      *
116      * @return The problems that caused this exception, never {@code null}.
117      */
118     public List<ModelProblem> getProblems() {
119         if (result == null) {
120             return Collections.emptyList();
121         }
122         return Collections.unmodifiableList(result.getProblems());
123     }
124 
125     private static String toMessage(ModelBuildingResult result) {
126         if (result != null && !result.getModelIds().isEmpty()) {
127             return toMessage(result.getModelIds().get(0), result.getProblems());
128         }
129         return null;
130     }
131 
132     private static String toMessage(String modelId, List<ModelProblem> problems) {
133         StringWriter buffer = new StringWriter(1024);
134 
135         PrintWriter writer = new PrintWriter(buffer);
136 
137         writer.print(problems.size());
138         writer.print((problems.size() == 1) ? " problem was " : " problems were ");
139         writer.print("encountered while building the effective model");
140         if (modelId != null && modelId.length() > 0) {
141             writer.print(" for ");
142             writer.print(modelId);
143         }
144         writer.println();
145 
146         for (ModelProblem problem : problems) {
147             writer.print("[");
148             writer.print(problem.getSeverity());
149             writer.print("] ");
150             writer.print(problem.getMessage());
151             writer.print(" @ ");
152             writer.println(ModelProblemUtils.formatLocation(problem, modelId));
153         }
154 
155         return buffer.toString();
156     }
157 }