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