View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2016 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.exception;
19  
20  import java.io.PrintStream;
21  import java.io.PrintWriter;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * A collection of several exceptions.
27   *
28   * @author Jeremy Long
29   */
30  public class ExceptionCollection extends Exception {
31  
32      /**
33       * Instantiates a new exception collection.
34       *
35       * @param exceptions a list of exceptions
36       */
37      public ExceptionCollection(List<Throwable> exceptions) {
38          super();
39          this.exceptions = exceptions;
40      }
41  
42      /**
43       * Instantiates a new exception collection.
44       *
45       * @param msg the exception message
46       * @param exceptions a list of exceptions
47       */
48      public ExceptionCollection(String msg, List<Throwable> exceptions) {
49          super(msg);
50          this.exceptions = exceptions;
51      }
52  
53      /**
54       * Instantiates a new exception collection.
55       *
56       * @param exceptions a list of exceptions
57       * @param fatal indicates if any of the exceptions that occurred is fatal - meaning
58       * that no analysis was performed.
59       */
60      public ExceptionCollection(List<Throwable> exceptions, boolean fatal) {
61          super();
62          this.exceptions = exceptions;
63          this.fatal = fatal;
64      }
65  
66      /**
67       * Instantiates a new exception collection.
68       *
69       * @param msg the exception message
70       * @param exceptions a list of exceptions
71       * @param fatal indicates if any of the exceptions that occurred is fatal - meaning
72       * that no analysis was performed.
73       */
74      public ExceptionCollection(String msg, List<Throwable> exceptions, boolean fatal) {
75          super(msg);
76          this.exceptions = exceptions;
77          this.fatal = fatal;
78      }
79  
80      /**
81       * Instantiates a new exception collection.
82       *
83       * @param exceptions a list of exceptions
84       * @param fatal indicates if the exception that occurred is fatal - meaning
85       * that no analysis was performed.
86       */
87      public ExceptionCollection(Throwable exceptions, boolean fatal) {
88          super();
89          this.exceptions = new ArrayList<Throwable>();
90          this.exceptions.add(exceptions);
91          this.fatal = fatal;
92      }
93  
94      /**
95       * Instantiates a new exception collection.
96       *
97       * @param msg the exception message
98       * @param exception a list of exceptions
99       */
100     public ExceptionCollection(String msg, Throwable exception) {
101         super(msg);
102         this.exceptions = new ArrayList<Throwable>();
103         this.exceptions.add(exception);
104         this.fatal = false;
105     }
106 
107     /**
108      * Instantiates a new exception collection.
109      */
110     public ExceptionCollection() {
111         super();
112         this.exceptions = new ArrayList<Throwable>();
113     }
114     /**
115      * The serial version uid.
116      */
117     private static final long serialVersionUID = 1L;
118 
119     /**
120      * A collection of exceptions.
121      */
122     private List<Throwable> exceptions;
123 
124     /**
125      * Get the value of exceptions.
126      *
127      * @return the value of exceptions
128      */
129     public List<Throwable> getExceptions() {
130         return exceptions;
131     }
132 
133     /**
134      * Adds an exception to the collection.
135      *
136      * @param ex the exception to add
137      */
138     public void addException(Throwable ex) {
139         this.exceptions.add(ex);
140     }
141 
142     /**
143      * Adds an exception to the collection.
144      *
145      * @param ex the exception to add
146      * @param fatal flag indicating if this is a fatal error
147      */
148     public void addException(Throwable ex, boolean fatal) {
149         addException(ex);
150         this.fatal = fatal;
151     }
152 
153     /**
154      * Flag indicating if a fatal exception occurred that would prevent the
155      * attempt at completing the analysis even if exceptions occurred.
156      */
157     private boolean fatal = false;
158 
159     /**
160      * Get the value of fatal.
161      *
162      * @return the value of fatal
163      */
164     public boolean isFatal() {
165         return fatal;
166     }
167 
168     /**
169      * Set the value of fatal.
170      *
171      * @param fatal new value of fatal
172      */
173     public void setFatal(boolean fatal) {
174         this.fatal = fatal;
175     }
176 
177     /**
178      * Prints the stack trace.
179      *
180      * @param s the writer to print to
181      */
182     @Override
183     public void printStackTrace(PrintWriter s) {
184         s.println("Multiple Exceptions Occurred");
185         super.printStackTrace(s);
186         for (Throwable t : this.exceptions) {
187             s.println("Next Exception:");
188             t.printStackTrace(s);
189         }
190     }
191 
192     /**
193      * Prints the stack trace.
194      *
195      * @param s the stream to write the stack trace to
196      */
197     @Override
198     public void printStackTrace(PrintStream s) {
199         s.println("Multiple Exceptions Occurred");
200         super.printStackTrace(s);
201         for (Throwable t : this.exceptions) {
202             s.println("Next Exception:");
203             t.printStackTrace(s);
204         }
205     }
206 
207     /**
208      * Returns the error message, including the message from all contained
209      * exceptions.
210      *
211      * @return the error message
212      */
213     @Override
214     public String getMessage() {
215         final StringBuilder sb = new StringBuilder();
216         final String msg = super.getMessage();
217         if (msg == null || msg.isEmpty()) {
218             sb.append("One or more exceptions occurred during analysis:");
219         } else {
220             sb.append(msg);
221         }
222         for (Throwable t : this.exceptions) {
223             sb.append("\n\t").append(t.getMessage());
224         }
225         return sb.toString();
226     }
227 }