All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.exc.BootRuntimeException Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * Created on : 03.05.2003
 * Created by : s3595
 */
package at.spardat.xma.boot.exc;

import java.text.MessageFormat;
import java.util.Locale;

/**
 * @author s3595 Chris Sch?fer (CGS)
 * @version 0.1.0
 */
public class BootRuntimeException extends Exception {

    /**
     * The code of this notification. Defaults to 0 if not set.
     */
    private int                 code_;

    /**
     * The message of this notification.
     */
    private String              message_;

    /**
     * The detail exception
     */
    private Throwable           detail_;

    /**
     * As for runtime exceptions:
     * Indicates if this exception may be shown to the end user or not. This is just
     * a hint for the presentation layer to decide if it should wrap this in
     * another exception.
     */
    protected boolean           showToEndUser_ = true;

    /**
     * For internal use only.
     */
    protected BootRuntimeException () {
    }

    /**
     * Constructs and sets the message from a format string as
     * defined in java.text.MessageFormat. The required parameters
     * must be contained in the params array.
     *
     * @param detail     detail exception.
     * @param message    string that either is a java.text.MessageFormat
     *                    or not, depending on params.
     * @param l          a java.util.Locale to format locale
     *                    dependent data types or null if the
     *                    params do not contain local specifics.
     * @param params the parameters of the message. If not null, message
     *                is expected to be a string compliant to MessageFormat.
     */
    public BootRuntimeException (Throwable detail, String message, Locale l, Object[] params) {
        // set detail
        if (detail != null) setDetail (detail);
        this.setMessage(message, l, params);
    }
    public BootRuntimeException (Throwable detail, String message) {
        // set detail
        if (detail != null) setDetail (detail);
        this.setMessage(message, null, null);
    }

    /**
     * Constructs and sets the message of this from a two
     * parameter java.text.MessageFormat compatible string.
     *
     * @param messageFmt format string as defined in java.text.MessageFormat.
     * @param param1 first message parameter
     * @param param2 second message parameter
     */
    public BootRuntimeException (String messageFmt, Object param1, Object param2) {
        this (messageFmt, null, new Object[]{param1,param2});
    }

    /**
     * Constructs and sets the message of this from a one
     * parameter java.text.MessageFormat compatible string.
     *
     * @param messageFmt format string as defined in java.text.MessageFormat.
     * @param param1 message parameter
     */
    public BootRuntimeException (String messageFmt, Object param1) {
        this (messageFmt, null, new Object[]{param1});
    }

    /**
     * Constructs and sets the message to the provided value.
     *
     * @param message message text.
     */
    public BootRuntimeException (String message) {
        this (message, null, null);
    }

    /**
     * Constructs and sets the message from a format string as
     * defined in java.text.MessageFormat. The required parameters
     * must be contained in the params array.
     *
     * @param message    format string as defined in java.text.MessageFormat
     *                    or plain text that is not a MessageFormat, depending on params
     * @param l          a java.util.Locale to format locale
     *                    dependent data types or null if the
     *                    params do not contain local specifics.
     * @param params the parameters of the message. If params is null,
     *                message is not subject to MessageFormat-processing. If params
     *                is not null, message must be compliant to
     *                java.text.MessageFormat.
     */
    public BootRuntimeException (String message, Locale l, Object[] params) {
        setMessage (message, l, params);
    }

    /**
     * Sets the message from a format string as
     * defined in java.text.MessageFormat. The required parameters
     * must be contained in the params array.
     *
     * @param message    format string as defined in java.text.MessageFormat
     *                    or plain text that is not a MessageFormat, depending on params
     * @param l          a java.util.Locale to format locale
     *                    dependent data types or null if the
     *                    params do not contain local specifics.
     * @param params the parameters of the message. If params is null,
     *                message is not subject to MessageFormat-processing. If params
     *                is not null, message must be compliant to
     *                java.text.MessageFormat.
     * @return this
     */
    public BootRuntimeException setMessage (String message, Locale l, Object[] params) {
        if (message == null) {
            message_ = "";
            return this;
        }
        if (params == null) {
            message_ = message;
        } else {
            try {
                MessageFormat       mf = new MessageFormat (message);
                if (l != null) mf.setLocale(l);
                message_ = mf.format (params);
            } catch (Exception ex) {
                message_ = ex.toString();
            }
        }
        return this;
    }

    /**
     * Sets the message of this from a one
     * parameter java.text.MessageFormat compatible string.
     *
     * @param messageFmt format string as defined in java.text.MessageFormat.
     * @param param1 message parameter
     * @param param2 message parameter
     * @return this
     */
    public BootRuntimeException setMessage (String messageFmt, Object param1, Object param2) {
        setMessage (messageFmt, null, new Object[]{param1, param2});
        return this;
    }

    /**
     * Sets the message of this from a one
     * parameter java.text.MessageFormat compatible string.
     *
     * @param messageFmt format string as defined in java.text.MessageFormat.
     * @param param1 message parameter
     * @return this
     */
    public BootRuntimeException setMessage (String messageFmt, Object param1) {
        setMessage (messageFmt, null, new Object[]{param1});
        return this;
    }

    /**
     * Constructs and sets the message to the provided value.
     *
     * @param message text to set
     * @return this
     */
    public BootRuntimeException setMessage (String message) {
        setMessage (message, null, null);
        return this;
    }

    /**
     * Sets the code. The code must be greater than zero.
     * @see #getCode()
     */
    public BootRuntimeException setCode (int code) {
        if (code <= 0) throw new IllegalArgumentException();
        code_ = code;
        return this;
    }

    /**
     * Returns the code set. The code should be used to discrimitate amongst
     * different kinds of notifications.
     */
    public int getCode () {
        return code_;
    }

    /**
     * @return String message
     */
    public String getMessage() {
        return message_ == null ? "" : message_;
    }

    /**
     * Sets the detail exception. This usually is the technical exception that
     * is the root cause of this exception.
     *
     * @param detail the root cause technical exception.
     */
    private void setDetail (Throwable detail) {
        initCause(detail);
        detail_ = detail;
    }

    /**
     * Returns the detail throwable. The returned value is null if
     * no detail has been set or this has been migrated and
     * the detail is not an BaseException.
     */
    public Throwable getDetail () {
        return detail_;
    }


    public BootRuntimeException setShowToEndUser( boolean bIn ){
        showToEndUser_ = bIn;
        return this;
    }

    public boolean getShowToEndUser( ){
        return showToEndUser_;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy