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 Lomg
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 the exception 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 the exception 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       * Instantiates a new exception collection.
95       *
96       * @param msg the exception message
97       * @param exception a list of exceptions
98       */
99      public ExceptionCollection(String msg, Throwable exception) {
100         super(msg);
101         this.exceptions = new ArrayList<Throwable>();
102         this.exceptions.add(exception);
103         this.fatal = false;
104     }
105 
106     /**
107      * Instantiates a new exception collection.
108      */
109     public ExceptionCollection() {
110         super();
111         this.exceptions = new ArrayList<Throwable>();
112     }
113     /**
114      * The serial version uid.
115      */
116     private static final long serialVersionUID = 1L;
117 
118     /**
119      * A collection of exceptions.
120      */
121     private List<Throwable> exceptions;
122 
123     /**
124      * Get the value of exceptions.
125      *
126      * @return the value of exceptions
127      */
128     public List<Throwable> getExceptions() {
129         return exceptions;
130     }
131 
132     /**
133      * Adds an exception to the collection.
134      *
135      * @param ex the exception to add
136      */
137     public void addException(Throwable ex) {
138         this.exceptions.add(ex);
139     }
140 
141     /**
142      * Adds an exception to the collection.
143      *
144      * @param ex the exception to add
145      * @param fatal flag indicating if this is a fatal error
146      */
147     public void addException(Throwable ex, boolean fatal) {
148         addException(ex);
149         this.fatal = fatal;
150     }
151 
152     /**
153      * Flag indicating if a fatal exception occurred that would prevent the
154      * attempt at completing the analysis even if exceptions occurred.
155      */
156     private boolean fatal = false;
157 
158     /**
159      * Get the value of fatal.
160      *
161      * @return the value of fatal
162      */
163     public boolean isFatal() {
164         return fatal;
165     }
166 
167     /**
168      * Set the value of fatal.
169      *
170      * @param fatal new value of fatal
171      */
172     public void setFatal(boolean fatal) {
173         this.fatal = fatal;
174     }
175 
176     /**
177      * Prints the stack trace.
178      *
179      * @param s the writer to print to
180      */
181     @Override
182     public void printStackTrace(PrintWriter s) {
183         s.println("Multiple Exceptions Occured");
184         super.printStackTrace(s);
185         for (Throwable t : this.exceptions) {
186             s.println("Next Exception:");
187             t.printStackTrace(s);
188         }
189     }
190 
191     /**
192      * Prints the stack trace.
193      *
194      * @param s the stream to write the stack trace to
195      */
196     @Override
197     public void printStackTrace(PrintStream s) {
198         s.println("Multiple Exceptions Occured");
199         super.printStackTrace(s);
200         for (Throwable t : this.exceptions) {
201             s.println("Next Exception:");
202             t.printStackTrace(s);
203         }
204     }
205 
206     /**
207      * Prints the stack trace to standard error.
208      */
209     @Override
210     public void printStackTrace() {
211         this.printStackTrace(System.err);
212     }
213 
214 }