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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.admin.ads;
029    import org.opends.server.types.OpenDsException;
030    
031    import javax.naming.NamingException;
032    
033    import org.opends.admin.ads.util.ApplicationTrustManager;
034    
035    /**
036     * This class represents the Exception that can occur while reading server
037     * configuration through the TopologyCache class.
038     */
039    public class TopologyCacheException extends OpenDsException {
040    
041      private static final long serialVersionUID = 1709535837273360382L;
042      private Type type;
043      private String ldapUrl;
044      private ApplicationTrustManager trustManager;
045    
046      /**
047       * Error type.
048       */
049      public enum Type
050      {
051        /**
052         * Error reading the ADS.
053         */
054        GENERIC_READING_ADS,
055        /**
056         * Creating connection to a particular server.
057         */
058        GENERIC_CREATING_CONNECTION,
059        /**
060         * Error reading the configuration of a particular server.
061         */
062        GENERIC_READING_SERVER,
063        /**
064         * The DN provided in the DirContext of ADS is not of a global
065         * administrator.
066         */
067        NOT_GLOBAL_ADMINISTRATOR,
068        /**
069         * Not enough permissions to read the server configuration.
070         */
071        NO_PERMISSIONS,
072        /**
073         * Timeout reading the configuration of a particular server.
074         */
075        TIMEOUT,
076        /**
077         * Unexpected error.
078         */
079        BUG
080      }
081    
082      /**
083       * Constructor for the exception that must be generated when an
084       * ADSContextException occurs.
085       * @param ace the exception which is the cause of this exception.
086       */
087      public TopologyCacheException(ADSContextException ace)
088      {
089        type = Type.GENERIC_READING_ADS;
090        initCause(ace);
091      }
092    
093      /**
094      * Constructor for a generic Exception.
095      * @param type the type of this exception.
096      * @param t the cause of this exception.
097      */
098      public TopologyCacheException(Type type, Throwable t)
099      {
100        this.type = type;
101        initCause(t);
102      }
103    
104      /**
105       * Constructor for the exception that must be generated when a
106       * NamingException occurs.
107       * @param type the type of this exception.
108       * @param ne the NamingException that generated this exception.
109       * @param trustManager the ApplicationTrustManager used when the
110       * NamingException occurred.
111       * @param ldapUrl the LDAP URL of the server we where connected to (or trying
112       * to connect) when the NamingException was generated.
113       */
114      public TopologyCacheException(Type type, NamingException ne,
115          ApplicationTrustManager trustManager, String ldapUrl)
116      {
117        this.type = type;
118        initCause(ne);
119        this.ldapUrl = ldapUrl;
120        this.trustManager = trustManager;
121      }
122    
123      /**
124       * Returns the type of this exception.
125       * @return the type of this exception.
126       */
127      public Type getType()
128      {
129        return type;
130      }
131    
132      /**
133       * Returns the LDAP URL of the server we where connected to (or trying
134       * to connect) when this exception was generated.
135       * @return the LDAP URL of the server we where connected to (or trying
136       * to connect) when this exception was generated.
137       */
138      public String getLdapUrl()
139      {
140        return ldapUrl;
141      }
142    
143      /**
144       * Returns the host port representation of the server we where connected to
145       * (or trying to connect) when this exception was generated.
146       * @return the host port representation of the server we where connected to
147       * (or trying to connect) when this exception was generated.
148       */
149      public String getHostPort()
150      {
151        int index = ldapUrl.indexOf("//");
152        String hostPort = ldapUrl.substring(index + 2);
153        return hostPort;
154      }
155    
156      /**
157       * Returns the ApplicationTrustManager that we were using when this exception
158       * was generated.
159       * @return the ApplicationTrustManager that we were using when this exception
160       * was generated.
161       */
162      public ApplicationTrustManager getTrustManager()
163      {
164        return trustManager;
165      }
166    }