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