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