001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009 Sun Microsystems, Inc.
025 */
026
027package org.forgerock.opendj.ldap;
028
029import java.io.IOException;
030
031import org.forgerock.i18n.LocalizableException;
032import org.forgerock.i18n.LocalizableMessage;
033
034/**
035 * Thrown when data from an input source cannot be decoded, perhaps due to the
036 * data being malformed in some way. By default decoding exceptions are fatal,
037 * indicating that the associated input source is no longer usable.
038 */
039@SuppressWarnings("serial")
040public final class DecodeException extends IOException implements LocalizableException {
041    /**
042     * Creates a new non-fatal decode exception with the provided message.
043     *
044     * @param message
045     *            The message that explains the problem that occurred.
046     * @return The new non-fatal decode exception.
047     */
048    public static DecodeException error(final LocalizableMessage message) {
049        return new DecodeException(message, false, null);
050    }
051
052    /**
053     * Creates a new non-fatal decode exception with the provided message and
054     * root cause.
055     *
056     * @param message
057     *            The message that explains the problem that occurred.
058     * @param cause
059     *            The underlying cause of this exception.
060     * @return The new non-fatal decode exception.
061     */
062    public static DecodeException error(final LocalizableMessage message, final Throwable cause) {
063        return new DecodeException(message, false, cause);
064    }
065
066    /**
067     * Creates a new fatal decode exception with the provided message. The
068     * associated input source can no longer be used.
069     *
070     * @param message
071     *            The message that explains the problem that occurred.
072     * @return The new fatal decode exception.
073     */
074    public static DecodeException fatalError(final LocalizableMessage message) {
075        return new DecodeException(message, true, null);
076    }
077
078    /**
079     * Creates a new fatal decode exception with the provided message and root
080     * cause. The associated input source can no longer be used.
081     *
082     * @param message
083     *            The message that explains the problem that occurred.
084     * @param cause
085     *            The underlying cause of this exception.
086     * @return The new fatal decode exception.
087     */
088    public static DecodeException fatalError(final LocalizableMessage message, final Throwable cause) {
089        return new DecodeException(message, true, cause);
090    }
091
092    private final LocalizableMessage message;
093
094    private final boolean isFatal;
095
096    /** Construction is provided via factory methods. */
097    private DecodeException(final LocalizableMessage message, final boolean isFatal,
098            final Throwable cause) {
099        super(message.toString(), cause);
100        this.message = message;
101        this.isFatal = isFatal;
102    }
103
104    /**
105     * Returns the message that explains the problem that occurred.
106     *
107     * @return LocalizableMessage of the problem
108     */
109    public LocalizableMessage getMessageObject() {
110        return message;
111    }
112
113    /**
114     * Indicates whether or not the error was fatal and the associated input
115     * source can no longer be used.
116     *
117     * @return {@code true} if the error was fatal and the associated input
118     *         source can no longer be used, otherwise {@code false} .
119     */
120    public boolean isFatal() {
121        return isFatal;
122    }
123}