001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import org.forgerock.i18n.LocalizableException;
020import org.forgerock.i18n.LocalizableMessage;
021
022/** This class defines a base exception for OpenDS exceptions. */
023@org.opends.server.types.PublicAPI(
024     stability=org.opends.server.types.StabilityLevel.VOLATILE,
025     mayInstantiate=false,
026     mayExtend=false,
027     mayInvoke=true)
028public abstract class OpenDsException extends Exception implements LocalizableException
029{
030  /** Generated serialization ID. */
031  private static final long serialVersionUID = 7310881401563732702L;
032
033  /** LocalizableMessage that explains the problem. */
034  private final LocalizableMessage message;
035
036  /** Creates a new identified exception. */
037  protected OpenDsException()
038  {
039    this(null, null);
040  }
041
042  /**
043   * Constructs a new instance from another <code>OpenDsException</code>.
044   * This constructor sets the message to be that of <code>cause</code>.
045   *
046   * @param cause exception whose message will be used for
047   *        this exception's message.
048   */
049  protected OpenDsException(OpenDsException cause) {
050    this(null, cause);
051  }
052
053  /**
054   * Creates a new identified exception with the provided information.
055   *
056   * @param  message  The message that explains the problem that occurred.
057   */
058  protected OpenDsException(LocalizableMessage message)
059  {
060    this(message, null);
061  }
062
063  /**
064   * Creates a new identified exception with the provided information.
065   *
066   * @param  cause  The underlying cause that triggered this exception.
067   */
068  protected OpenDsException(Throwable cause)
069  {
070    this(null, cause);
071  }
072
073  /**
074   * Creates a new identified exception with the provided information.
075   *
076   * @param  message  The message that explains the problem that occurred.
077   * @param  cause    The underlying cause that triggered this exception.
078   */
079  protected OpenDsException(LocalizableMessage message, Throwable cause)
080  {
081    super(message != null ? message.toString() :
082            cause != null ? cause.getMessage() : null, cause);
083    if (message != null) {
084      this.message = message;
085    } else if (cause instanceof LocalizableException) {
086      this.message = ((LocalizableException) cause).getMessageObject();
087    } else {
088      this.message = null;
089    }
090  }
091
092  /**
093   * Returns the message that explains the problem that occurred.
094   *
095   * @return LocalizableMessage of the problem
096   */
097  @Override
098  public LocalizableMessage getMessageObject() {
099    return this.message;
100  }
101}