Package Framework

Source Code of Framework.ErrorMgr

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package Framework;

import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import DisplayProject.ErrorDialog;

/**
* This class is a limited implementation of the Forte ErrorMgr
*
* The ErrorMgr class allows you to work with the error stack. It includes
* methods to control the error stack, add context information to errors on the
* stack, and route error messages for display or printing.
*
*/
public class ErrorMgr  {

  /**
   * The list of errors associated with this error manager. This errorList can
   * never be null during the life of the class.
   */
  final private List<Throwable> errorList;

    private static Logger myLogger = Logger.getLogger("task.part.logmgr");

    private ErrorMgr(){
      this.errorList = new ArrayList<Throwable>();
    }

    /**
     * This variation of the AddError method adds the current ErrorDesc object to the error stack.
     */
    public static void addError(Throwable error){
        if (!isListed(error)){
            errorMgrs.get().errorList.add(0, error);
        }
    }
   
    /**
   * This variation of the AddError method appends the specified ErrorMgr
   * stack to the top of the error stack for the current task.
   *
   * @param errorMgr
   *            When you add an ErrorMgr to an error stack, all the elements
   *            in ErrorMgr are transferred to the error stack and ErrorMgr is
   *            cleared. Subsequent references to errors within ErrorMgr will
   *            return <code>null</code>.
   */
    public static void addError(ErrorMgr errorMgr){
      if (errorMgr == null) {
      return;
    } else {
      // TF:10/07/2009:Implemented this method
      ErrorMgr thisMgr = errorMgrs.get();
      thisMgr.errorList.addAll(errorMgr.errorList);
    }
    }
   
    /**
     * This AddError method adds an error message to the top of the error stack, using the specified error text, severity, and parameter values.
     * @param severity
     * @param message
     * @param parameters
     */
    public static void addError(int severity, String message, DataValue ... parameters){
        TextData td = new TextData();
        // TF:24/06/2008:Fixed this up to pass the varargs parameter correctly
        td.replaceParameters(message, (Object [])parameters);
        GenericException ex = new GenericException(td, severity);
        addError(ex);
    }
    public static void addError(int severity, TextData message, DataValue ... parameters){
        addError(severity, message.toString(), parameters);
    }
    /**
     * Clears the error stack without printing
     */
    public static void clear(){
        errorMgrs.get().errorList.clear();
    }
   
    /**
     * returns a specific throwable indicated by the index. The index is 1 based
     * @param index
     * @return
     */
    public static Throwable getAt(int index) {
      //PM:17/3/08 minor change to boundary checking
      // TF:10/07/2009:Changed to use new error structure, plus performance optimisation.
      ErrorMgr mgr = errorMgrs.get();
     
        if (index < 1 || index > mgr.errorList.size()) {
            return null;
        }
        return mgr.errorList.get(index -1);
    }
   
    /**
     * Removes an error from the error stack
     * @param error
     */
    public static void remove(Object error){
        ErrorMgr mgr = errorMgrs.get();
        //PM:17/3/08 simplified remove algorithm
        mgr.errorList.remove(error);
    }

    /**
     * Removes an error from the error stack by position. The position is 1 based
     * @param position
     */
    public static void remove(int position){
        if (position < 1) return;
        errorMgrs.get().errorList.remove(position -1);
    }

    /**
     * return the number of errors in the error list
     *
     * @param e
     * @return
     */
    public static int getErrorCount() {
        return errorMgrs.get().errorList.size();
    }

    /**
     * This method has little effect. If the passed parameter is 0, ErrorMgr.clear() will be called,
     * but otherwise this method has no effect.
     * @param severity
     * @deprecated this method should not be used -- used ErrorMgr.clear() to clear an error stack
     */
    public static void setErrorCount(int count) {
      if (count == 0) {
        ErrorMgr.clear();
      }
    }

    /**
     * returns the worst severity in the error list. NOTE: this ignores all
     * exceptions that are not subclassed of ErrorDesc
     *
     * @param e
     * @return
     */
    public static int getWorstSeverity() {
        int worst = Constants.SP_ER_INFORMATION;
        ErrorMgr mgr = errorMgrs.get();
        for (Throwable ref : mgr.errorList){
            if (ref instanceof ErrorDesc && ((ErrorDesc)ref).getSeverity() > worst) {
                worst = ((ErrorDesc)ref).getSeverity();
            }
        }
        return worst;
    }

    /**
     * This method has no effect. It is introduced here to allow some methods to compile, but will have no effect.
     * @param severity
     * @deprecated this method should not be used
     */
    public static void setWorstSeverity(int severity) {
    }

    /**
     * Logs errors to "task.part.logmgr". If there is a graphics environment, it
     * also shows an error dialog box.
     *
     * @param owner
     * @param message
     */
    public static void showErrors(Frame owner, String message) {
      showErrors(owner, message, true);
    }
   
    /**
   * The showErrors method prints or displays the information in the error
   * stack. It is normally used in the exception clauses of blocks. Logs
   * errors to "task.part.logmgr". If there is a graphics environment, it also
   * shows an error dialog box.
   *
   */
    public static void showErrors() {
      showErrors(null, "Error", true);
    }
    /**
   * The showErrors method prints or displays the information in the error
   * stack. It is normally used in the exception clauses of blocks. Logs
   * errors to "task.part.logmgr". If there is a graphics environment, it also
   * shows an error dialog box.
   *
   * @param clearout
   *            If set to TRUE, the clearout parameter tells the error manager
   *            to clearout the error stack after printing; if set to FALSE
   *            the stack is not affected.
   */
    public static void showErrors(boolean clearout) {
      showErrors(null, "Error", clearout);
    }

  /**
   * The showErrors method prints or displays the information in the error
   * stack. It is normally used in the exception clauses of blocks. Logs
   * errors to "task.part.logmgr". If there is a graphics environment, it also
   * shows an error dialog box.
   *
   * @param clearout
   *            If set to TRUE, the clearout parameter tells the error manager
   *            to clearout the error stack after printing; if set to FALSE
   *            the stack is not affected.
   * @param messageType
   *            The messageType specifies the dialog title
   */
    public static void showErrors(boolean clearout, int messageType) {
      String message;
      switch (messageType) {
    case 1:
      message = "Information";
      break;

    case 2:
      message = "Warning";
      break;

    default:
      message = "Error";
      break;
    }
      showErrors(null, message, clearout);
    }
    /**
   * The showErrors method prints or displays the information in the error
   * stack. It is normally used in the exception clauses of blocks. Logs
   * errors to "task.part.logmgr". If there is a graphics environment, it also
   * shows an error dialog box.
   *
   * @param owner
   * @param message
   * @param clearout
   *            If set to TRUE, the clearout parameter tells the error manager
   *            to clearout the error stack after printing; if set to FALSE
   *            the stack is not affected.
   */
    public static void showErrors(Frame owner, String message, boolean clearout) {
        myLogger.error("Show Errors");
        // TF:10/07/2009:Changed to use new error manager and optimised this code.
        ErrorMgr mgr = errorMgrs.get();
        for (Throwable ref : mgr.errorList){
            if (ref != null) {
              myLogger.error(ref.getClass().getName()); // PM:16/07/2008
              myLogger.error(ref);
                StackTraceElement[] trace = ref.getStackTrace();
                for (int i=0; i < trace.length; i++)
                  myLogger.error("\tat " + trace[i]);

            }
        }
        if (!GraphicsEnvironment.isHeadless()) {
            if (mgr.errorList.size() > 0){
                Throwable e = mgr.errorList.get(0);
                new ErrorDialog(owner, message, e).setVisible(true);
            } else {
                new ErrorDialog(owner, message, null).setVisible(true);
            }

        }
        if (clearout) {
          clear();
    }
    }
    /**
     * returns true if the exception is already in the list
     * @param error
     * @return
     */
    public static boolean isListed(Throwable error) {
      boolean result = false;
        // TF:10/07/2009:Changed to use new error manager
      ErrorMgr mgr = errorMgrs.get();
        for (Throwable ref : mgr.errorList){
            if (ref == error){
              result = true;
              break;
            } else {
              //DQ:14/11/2008:  Check to see if we're wrapping the error that is already in the list.
               result = isCauseListed(ref, error);
              if (result) {
                //Change the element in the list?
              }
            }
        }
        return result;
    }
   
    /**
     * //DQ:14/11/2008:  Check to see if the error is the same as the errorWithCause or one of its causes.
     * @param error
     * @param errorWithCause
     * @return
     */
    public static boolean isCauseListed(Throwable error,
      Throwable errorWithCause) {
    boolean result = false;
    if (errorWithCause.getCause() != null
        && errorWithCause.getCause() != errorWithCause) {
      if (error == errorWithCause.getCause()) {
        result = true;
      } else {
        result = isCauseListed(error, errorWithCause.getCause());
      }
      if (!result
          && (error.getCause() != null && error.getCause() != error)) {
        result = isCauseListed(error.getCause(), errorWithCause);
      }
    }
    return result;

  }

    //================================================================
    // these operation are used when jcTOOL generates code WITHOUT the
    // "generate error manager code"  option switched on
    //================================================================
    /**
     * Logs errors to "task.part.logmgr". If there is a graphics environment, it
     * also shows an error dialog box.
     *
     * @param owner
     * @param message
     * @param e
     */
    public static void showErrors(Frame owner, String message, Throwable e) {
        //PM:15/2/08 added to error list
        addChain(e);
        showErrors(owner, message);
    }

    /**
     * returns a specific exception in the error list based on the index.
     *
     * @param e
     * @param index
     *            1 based
     * @return
     */
    public static Throwable getAt(Throwable e, int index) {
        //PM:15/2/08 added to error list
        addChain(e);
        return getAt(index);
    }

    /**
     * return the number of errors in the error list
     *
     * @param e
     * @return
     */
    public static int getErrorCount(Throwable e) {
        //PM:15/2/08 added to error list
        addChain(e);
        return getErrorCount();
    }

    /**
     * returns the worst severity in the error list. NOTE: this ignores all
     * exceptions that are not subclessed of ErrorDesc
     *
     * @param e
     * @return
     */
    public static int getWorstSeverity(Throwable e) {
        //PM:15/2/08 added to error list
        addChain(e);
        return getWorstSeverity();
    }

  /**
   * Checks and adds the exception to the error manage collection. Any causes
   * of the exception are also added to the error stack.
   * <p>
   * AD:21/10/2008: JCT-576 Made public to allow customer code access
   *
   * @param e
   *            Exception to be added to the error manager, including all
   *            causes of the exception
   */
    public static void addChain(Throwable e){
        Throwable ex = e;
        int at = 0;
        // TF:10/07/2009:Changed to use new error manager and optimised this code.
        ErrorMgr mgr = errorMgrs.get();
        while (ex != null) {
            if (!isListed(ex)){
                mgr.errorList.add(at, ex);
                at++;
            }
            ex = ex.getCause();
        }
    }
   
    /**
     * Return a copy of the error manager for this thread.
     * @return A copy of the error mgr for this thread.
     */
    public static ErrorMgr get() {
      ErrorMgr thisMgr = new ErrorMgr();
      ErrorMgr result = errorMgrs.get();
      result.errorList.addAll(thisMgr.errorList);
      return result;
    }
   
    /**
     * One error list per thread.
     * For server threads, the error list will be cleared after each remote method invocation.
     */
    private static ThreadLocal<ErrorMgr> errorMgrs = new ThreadLocal<ErrorMgr>() {
        @Override
        protected ErrorMgr initialValue() {
            return new ErrorMgr();
        }
    };
}
TOP

Related Classes of Framework.ErrorMgr

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.