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 2013-2015 ForgeRock AS.
016 */
017
018package org.opends.admin.ads;
019import org.opends.server.types.OpenDsException;
020
021import javax.naming.NamingException;
022
023import org.opends.admin.ads.util.ApplicationTrustManager;
024
025/**
026 * This class represents the Exception that can occur while reading server
027 * configuration through the TopologyCache class.
028 */
029public class TopologyCacheException extends OpenDsException {
030
031  private static final long serialVersionUID = 1709535837273360382L;
032  private Type type;
033  private String ldapUrl;
034  private ApplicationTrustManager trustManager;
035
036  /**
037   * Error type.
038   */
039  public enum Type
040  {
041    /**
042     * Error reading the ADS.
043     */
044    GENERIC_READING_ADS,
045    /**
046     * Creating connection to a particular server.
047     */
048    GENERIC_CREATING_CONNECTION,
049    /**
050     * Error reading the configuration of a particular server.
051     */
052    GENERIC_READING_SERVER,
053    /**
054     * The DN provided in the DirContext of ADS is not of a global
055     * administrator.
056     */
057    NOT_GLOBAL_ADMINISTRATOR,
058    /**
059     * Not enough permissions to read the server configuration.
060     */
061    NO_PERMISSIONS,
062    /**
063     * Timeout reading the configuration of a particular server.
064     */
065    TIMEOUT,
066    /**
067     * Unexpected error.
068     */
069    BUG
070  }
071
072  /**
073   * Constructor for the exception that must be generated when an
074   * ADSContextException occurs.
075   * @param ace the exception which is the cause of this exception.
076   */
077  public TopologyCacheException(ADSContextException ace)
078  {
079    super(ace);
080    type = Type.GENERIC_READING_ADS;
081  }
082
083  /**
084  * Constructor for a generic Exception.
085  * @param type the type of this exception.
086  * @param t the cause of this exception.
087  */
088  public TopologyCacheException(Type type, Throwable t)
089  {
090    super(t);
091    this.type = type;
092  }
093
094  /**
095   * Constructor for the exception that must be generated when a
096   * NamingException occurs.
097   * @param type the type of this exception.
098   * @param ne the NamingException that generated this exception.
099   * @param trustManager the ApplicationTrustManager used when the
100   * NamingException occurred.
101   * @param ldapUrl the LDAP URL of the server we where connected to (or trying
102   * to connect) when the NamingException was generated.
103   */
104  public TopologyCacheException(Type type, NamingException ne,
105      ApplicationTrustManager trustManager, String ldapUrl)
106  {
107    super(ne);
108    this.type = type;
109    this.ldapUrl = ldapUrl;
110    this.trustManager = trustManager;
111  }
112
113  /**
114   * Returns the type of this exception.
115   * @return the type of this exception.
116   */
117  public Type getType()
118  {
119    return type;
120  }
121
122  /**
123   * Returns the LDAP URL of the server we where connected to (or trying
124   * to connect) when this exception was generated.
125   * @return the LDAP URL of the server we where connected to (or trying
126   * to connect) when this exception was generated.
127   */
128  public String getLdapUrl()
129  {
130    return ldapUrl;
131  }
132
133  /**
134   * Returns the host port representation of the server we where connected to
135   * (or trying to connect) when this exception was generated.
136   * @return the host port representation of the server we where connected to
137   * (or trying to connect) when this exception was generated.
138   */
139  public String getHostPort()
140  {
141    int index = ldapUrl.indexOf("//");
142    return ldapUrl.substring(index + 2);
143  }
144
145  /**
146   * Returns the ApplicationTrustManager that we were using when this exception
147   * was generated.
148   * @return the ApplicationTrustManager that we were using when this exception
149   * was generated.
150   */
151  public ApplicationTrustManager getTrustManager()
152  {
153    return trustManager;
154  }
155}