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 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.types;
028
029import org.forgerock.i18n.LocalizableException;
030import org.forgerock.i18n.LocalizableMessage;
031
032/** This class defines a base exception for OpenDS exceptions. */
033@org.opends.server.types.PublicAPI(
034     stability=org.opends.server.types.StabilityLevel.VOLATILE,
035     mayInstantiate=false,
036     mayExtend=false,
037     mayInvoke=true)
038public abstract class OpenDsException extends Exception implements LocalizableException
039{
040  /** Generated serialization ID. */
041  private static final long serialVersionUID = 7310881401563732702L;
042
043  /** LocalizableMessage that explains the problem. */
044  private final LocalizableMessage message;
045
046  /** Creates a new identified exception. */
047  protected OpenDsException()
048  {
049    this(null, null);
050  }
051
052  /**
053   * Constructs a new instance from another <code>OpenDsException</code>.
054   * This constructor sets the message to be that of <code>cause</code>.
055   *
056   * @param cause exception whose message will be used for
057   *        this exception's message.
058   */
059  protected OpenDsException(OpenDsException cause) {
060    this(null, cause);
061  }
062
063  /**
064   * Creates a new identified exception with the provided information.
065   *
066   * @param  message  The message that explains the problem that occurred.
067   */
068  protected OpenDsException(LocalizableMessage message)
069  {
070    this(message, null);
071  }
072
073  /**
074   * Creates a new identified exception with the provided information.
075   *
076   * @param  cause  The underlying cause that triggered this exception.
077   */
078  protected OpenDsException(Throwable cause)
079  {
080    this(null, cause);
081  }
082
083  /**
084   * Creates a new identified exception with the provided information.
085   *
086   * @param  message  The message that explains the problem that occurred.
087   * @param  cause    The underlying cause that triggered this exception.
088   */
089  protected OpenDsException(LocalizableMessage message, Throwable cause)
090  {
091    super(message != null ? message.toString() :
092            cause != null ? cause.getMessage() : null, cause);
093    if (message != null) {
094      this.message = message;
095    } else if (cause instanceof LocalizableException) {
096      this.message = ((LocalizableException) cause).getMessageObject();
097    } else {
098      this.message = null;
099    }
100  }
101
102  /**
103   * Returns the message that explains the problem that occurred.
104   *
105   * @return LocalizableMessage of the problem
106   */
107  @Override
108  public LocalizableMessage getMessageObject() {
109    return this.message;
110  }
111}