buildframework/helium/sf/java/core/src/com/nokia/helium/core/MultiCauseBuildException.java
changeset 628 7c4a911dc066
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 package com.nokia.helium.core;
       
    18 
       
    19 import java.io.PrintStream;
       
    20 import java.io.PrintWriter;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Arrays;
       
    23 import java.util.List;
       
    24 import java.util.Vector;
       
    25 
       
    26 import org.apache.tools.ant.BuildException;
       
    27 
       
    28 /**
       
    29  * A MultiCauseBuildException class used by the HeliumExecutor plugin to store
       
    30  * the failures caused in the course of a build.
       
    31  * 
       
    32  */
       
    33 public class MultiCauseBuildException extends BuildException {
       
    34 
       
    35     private static final long serialVersionUID = -4843675216704377447L;
       
    36 
       
    37     private Vector<Throwable> causes = new Vector<Throwable>();
       
    38 
       
    39     /**
       
    40      * Construct a new multicause build exception with no detail message and no
       
    41      * cause.
       
    42      */
       
    43     public MultiCauseBuildException() {
       
    44         super();
       
    45     }
       
    46 
       
    47     /**
       
    48      * Construct a new multi-cause build exception with the given detail message
       
    49      * and no cause.
       
    50      * 
       
    51      * @param msg
       
    52      *            Detail message.
       
    53      */
       
    54     public MultiCauseBuildException(String msg) {
       
    55         super(msg);
       
    56     }
       
    57 
       
    58     /**
       
    59      * Construct a new multi-cause build exception with no detail message and
       
    60      * the given cause.
       
    61      * 
       
    62      * @param cause
       
    63      *            the cause of failure.
       
    64      */
       
    65     public MultiCauseBuildException(Throwable cause) {
       
    66         super();
       
    67         causes.add(cause);
       
    68     }
       
    69 
       
    70     /**
       
    71      * Construct a new multi-cause exception with the given detail message and
       
    72      * the given cause.
       
    73      * 
       
    74      * @param msg
       
    75      *            Detail message.
       
    76      * @param cause
       
    77      *            the cause of failure
       
    78      * 
       
    79      */
       
    80     public MultiCauseBuildException(String msg, Throwable cause) {
       
    81         super(msg);
       
    82         causes.add(cause);
       
    83     }
       
    84 
       
    85     /**
       
    86      * Method adds the given throwable instance.
       
    87      * 
       
    88      * @param th
       
    89      *            the throwbale instance to be added.
       
    90      */
       
    91     public void add(Throwable th) {
       
    92         causes.add(th);
       
    93     }
       
    94 
       
    95     /**
       
    96      * {@inheritDoc}
       
    97      */
       
    98     public StackTraceElement[] getStackTrace() {
       
    99         List<StackTraceElement> stackTraceElements = new ArrayList<StackTraceElement>();
       
   100         for (Throwable th : causes) {
       
   101             stackTraceElements.addAll(Arrays.asList(th.getStackTrace()));
       
   102         }
       
   103         return stackTraceElements
       
   104                 .toArray(new StackTraceElement[stackTraceElements.size()]);
       
   105     }
       
   106 
       
   107     /**
       
   108      * {@inheritDoc}
       
   109      */
       
   110     public void printStackTrace(PrintStream ps) {
       
   111         synchronized (ps) {
       
   112             super.printStackTrace(ps);
       
   113             int i = 1;
       
   114             for (Throwable cause : causes) {
       
   115                 ps.printf("MultiCauseBuildException caused by [%d]: ", i++);
       
   116                 cause.printStackTrace(ps);
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * {@inheritDoc}
       
   123      */
       
   124     public void printStackTrace(PrintWriter pw) {
       
   125         synchronized (pw) {
       
   126             super.printStackTrace(pw);
       
   127             int i = 1;
       
   128             for (Throwable cause : causes) {
       
   129                 pw.printf("MultiCauseBuildException caused by [%d]: ", i++);
       
   130                 cause.printStackTrace(pw);
       
   131             }
       
   132         }
       
   133     }
       
   134 
       
   135     /**
       
   136      * {@inheritDoc}
       
   137      */
       
   138     public String getMessage() {
       
   139         String message = super.getMessage();
       
   140         for (Throwable th : causes) {
       
   141             if (message == null) {
       
   142                 message = th.getMessage();
       
   143             } else {            
       
   144                 message += "\n" + th.getMessage();
       
   145             }
       
   146         }
       
   147         return message;
       
   148     }
       
   149 
       
   150     /**
       
   151      * Returns the location of the error and the error message.
       
   152      * 
       
   153      * @return the location of the error and the error message
       
   154      */
       
   155     public String toString() {
       
   156         StringBuffer buffer = new StringBuffer();
       
   157         for (Throwable th : causes) {
       
   158             buffer.append(th);
       
   159             buffer.append("\n");
       
   160         }
       
   161         return buffer.toString();
       
   162     }
       
   163 
       
   164 }