UtilsMultiException.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.utils;

  21. import java.io.PrintStream;
  22. import java.io.PrintWriter;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;

  26. /**
  27.  * Contiene la definizione di una eccezione lanciata dalle classi del package org.openspcoop.utils
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */


  33. public class UtilsMultiException extends Exception {

  34.     private final List<Throwable> exceptions = new ArrayList<>();
  35.    
  36.     public List<Throwable> getExceptions() {
  37.         return this.exceptions;
  38.     }
  39.    
  40.     public UtilsMultiException(String message, Throwable cause)
  41.     {
  42.         super(message, cause);
  43.         this.exceptions.add(cause);
  44.     }
  45.     public UtilsMultiException(Throwable cause)
  46.     {
  47.         super(cause);
  48.         this.exceptions.add(cause);
  49.     }
  50.     public UtilsMultiException(String message, Throwable ... cause)
  51.     {
  52.         super(message, (cause!=null && cause.length>0) ? cause[0] :  null);
  53.         if(cause!=null && cause.length>0) {
  54.             for (int i = 0; i < cause.length; i++) {
  55.                 this.exceptions.add(cause[i]);      
  56.             }
  57.         }
  58.     }
  59.     public UtilsMultiException(Throwable ...cause)
  60.     {
  61.         super((cause!=null && cause.length>0) ? cause[0] :  null);
  62.         if(cause!=null && cause.length>0) {
  63.             for (int i = 0; i < cause.length; i++) {
  64.                 this.exceptions.add(cause[i]);      
  65.             }
  66.         }
  67.     }
  68.    
  69.     public String getMultiMessage() {
  70.         StringBuilder sb = new StringBuilder();
  71.         if(!this.exceptions.isEmpty()) {
  72.             for (int i = (this.exceptions.size()-1); i >= 0; i--) {
  73.                 if(sb.length()>0) {
  74.                     sb.append("\n");
  75.                 }
  76.                 sb.append("- error "+(i+1)+": ");
  77.                 sb.append(this.exceptions.get(i).getMessage());
  78.             }
  79.         }
  80.         return sb.toString();
  81.     }
  82.    
  83.     @Override
  84.     public void printStackTrace(PrintStream s) {
  85.         if(!this.exceptions.isEmpty()) {
  86.             for (int i = (this.exceptions.size()-1); i >= 0; i--) {
  87.                 if(i>0) {
  88.                     s.print("\n");
  89.                 }
  90.                 s.print("MultiException at position "+(i+1)+": \n");
  91.                 this.exceptions.get(i).printStackTrace(s);
  92.             }
  93.         }
  94.         else {
  95.             super.printStackTrace(s);
  96.         }
  97.     }

  98.     @Override
  99.     public void printStackTrace(PrintWriter s) {
  100.         if(!this.exceptions.isEmpty()) {
  101.             for (int i = (this.exceptions.size()-1); i >= 0; i--) {
  102.                 if(i>0) {
  103.                     s.print("\n");
  104.                 }
  105.                 s.print("MultiException at position "+(i+1)+": \n");
  106.                 this.exceptions.get(i).printStackTrace(s);
  107.             }
  108.         }
  109.         else {
  110.             super.printStackTrace(s);
  111.         }
  112.     }
  113.    
  114.     @Override
  115.     public StackTraceElement[] getStackTrace() {
  116.         if(!this.exceptions.isEmpty()) {
  117.             List<StackTraceElement> listStackTraceElement = new ArrayList<>();
  118.             for (int i = (this.exceptions.size()-1); i >= 0; i--) {
  119.                 StackTraceElement sElement = new StackTraceElement(UtilsMultiException.class.getName(), "Position_"+(i+1), "MultiException_"+(i+1), (i+1));
  120.                 listStackTraceElement.add(sElement);
  121.                 StackTraceElement[] tmp = this.exceptions.get(i).getStackTrace();
  122.                 if(tmp!=null && tmp.length>0) {
  123.                     listStackTraceElement.addAll(Arrays.asList(tmp));
  124.                 }
  125.             }
  126.             return listStackTraceElement.toArray(new StackTraceElement[1]);
  127.         }
  128.         else {
  129.             return super.getStackTrace();
  130.         }
  131.     }

  132.    
  133.     /**
  134.      * serialVersionUID
  135.      */
  136.     private static final long serialVersionUID = 1L;

  137.     public UtilsMultiException() {
  138.         super();
  139.     }
  140.     public UtilsMultiException(String msg) {
  141.         super(msg);
  142.     }
  143. }