View Javadoc

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