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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.admin.ads;
028
029import static org.opends.messages.QuickSetupMessages.*;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.opends.server.types.OpenDsException;
033
034/**
035 * This is the exception that is thrown in ADSContext.
036 * @see org.opends.admin.ads.ADSContext
037 */
038public class ADSContextException extends OpenDsException {
039  private static final long serialVersionUID = 1984039711031042813L;
040
041  /** The enumeration containing the different error types. */
042  public enum ErrorType
043  {
044    /** The host name is missing. */
045    MISSING_HOSTNAME,
046    /** The host name is not valid. */
047    NOVALID_HOSTNAME,
048    /** The installation path is missing. */
049    MISSING_IPATH,
050    /** The installation path is not valid. */
051    NOVALID_IPATH,
052    /** An access permission error. */
053    ACCESS_PERMISSION,
054    /** The entity is already registered. */
055    ALREADY_REGISTERED,
056    /** The installation is broken. */
057    BROKEN_INSTALL,
058    /** The entity is not yet registered. */
059    NOT_YET_REGISTERED,
060    /** The port is missing. */
061    MISSING_PORT,
062    /** The port is not valid. */
063    NOVALID_PORT,
064    /** The name is missing. */
065    MISSING_NAME,
066    /** The administration UID is missing. */
067    MISSING_ADMIN_UID,
068    /** The administrator password is missing. */
069    MISSING_ADMIN_PASSWORD,
070    /** There is already a backend with the name of the ADS backend but not of the expected type. */
071    UNEXPECTED_ADS_BACKEND_TYPE,
072    /** Error merging with another ADSContext. */
073    ERROR_MERGING,
074    /** Unexpected error (potential bug). */
075    ERROR_UNEXPECTED;
076  }
077
078  private final ErrorType error;
079  private final String toString;
080
081  /**
082   * Creates an ADSContextException of the given error type.
083   * @param error the error type.
084   */
085  ADSContextException(ErrorType error)
086  {
087    this(error, null);
088  }
089
090  /**
091   * Creates an ADSContextException of the given error type with the provided
092   * error cause.
093   * @param error the error type.
094   * @param x the throwable that generated this exception.
095   */
096  ADSContextException(ErrorType error, Throwable x)
097  {
098    this(error, getMessage(error, x), x);
099  }
100
101  /**
102   * Creates an ADSContextException of the given error type with the provided error cause and
103   * message.
104   *
105   * @param error
106   *          the error type.
107   * @param msg
108   *          the message describing the error.
109   * @param cause
110   *          the throwable that generated this exception.
111   */
112  ADSContextException(ErrorType error, LocalizableMessage msg, Throwable cause)
113  {
114    super(msg, cause);
115    this.error = error;
116    toString = "ADSContextException: error type " + error + "." + (cause != null ? "  Root cause: " + cause : "");
117  }
118
119  /**
120   * Returns the error type of this exception.
121   * @return the error type of this exception.
122   */
123  public ErrorType getError()
124  {
125    return error;
126  }
127
128  /** {@inheritDoc} */
129  @Override
130  public void printStackTrace()
131  {
132    super.printStackTrace();
133    if (getCause() != null)
134    {
135      System.out.println("embeddedException = {");
136      getCause().printStackTrace();
137      System.out.println("}");
138    }
139  }
140
141  /** {@inheritDoc} */
142  @Override
143  public String toString()
144  {
145    return toString;
146  }
147
148  private static LocalizableMessage getMessage(ErrorType error, Throwable x)
149  {
150    if (x instanceof OpenDsException)
151    {
152      return INFO_ADS_CONTEXT_EXCEPTION_WITH_DETAILS_MSG.get(error,
153          ((OpenDsException)x).getMessageObject());
154    } else if (x != null)
155    {
156      return INFO_ADS_CONTEXT_EXCEPTION_WITH_DETAILS_MSG.get(error, x);
157    }
158    else
159    {
160      return INFO_ADS_CONTEXT_EXCEPTION_MSG.get(error);
161    }
162  }
163}