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 2015-2016 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.config;
019
020import static com.forgerock.opendj.ldap.config.ConfigMessages.*;
021
022import org.forgerock.i18n.LocalizableMessage;
023
024/** The requested managed object was found but its type could not be determined. */
025public class DefinitionDecodingException extends DecodingException {
026
027    /** An enumeration defining the reasons why the definition could not be resolved. */
028    public static enum Reason {
029        /**
030         * The managed object could be found but its type resolved to an
031         * abstract managed object definition.
032         */
033        ABSTRACT_TYPE_INFORMATION(),
034
035        /**
036         * The managed object could be found but did not contain any type
037         * information (eg missing object classes in LDAP).
038         */
039        NO_TYPE_INFORMATION(),
040
041        /**
042         * The managed object could be found but did not contain the expected
043         * type information (eg incorrect object classes in LDAP).
044         */
045        WRONG_TYPE_INFORMATION();
046
047    }
048
049    /** Version ID required by serializable classes. */
050    private static final long serialVersionUID = 3459033551415663416L;
051
052    /** Create the message. */
053    private static LocalizableMessage createLocalizableMessage(AbstractManagedObjectDefinition<?, ?> d, Reason reason) {
054        LocalizableMessage ufn = d.getUserFriendlyName();
055        switch (reason) {
056        case NO_TYPE_INFORMATION:
057            return ERR_DECODING_EXCEPTION_NO_TYPE_INFO.get(ufn);
058        case WRONG_TYPE_INFORMATION:
059            return ERR_DECODING_EXCEPTION_WRONG_TYPE_INFO.get(ufn);
060        default:
061            return ERR_DECODING_EXCEPTION_ABSTRACT_TYPE_INFO.get(ufn);
062        }
063    }
064
065    /** The expected type of managed object. */
066    private final AbstractManagedObjectDefinition<?, ?> d;
067
068    /** The reason why the definition could not be determined. */
069    private final Reason reason;
070
071    /**
072     * Create a new definition decoding exception.
073     *
074     * @param d
075     *            The expected type of managed object.
076     * @param reason
077     *            The reason why the definition could not be determined.
078     */
079    public DefinitionDecodingException(AbstractManagedObjectDefinition<?, ?> d, Reason reason) {
080        super(createLocalizableMessage(d, reason));
081        this.d = d;
082        this.reason = reason;
083    }
084
085    /**
086     * Gets the expected managed object definition.
087     *
088     * @return Returns the expected managed object definition.
089     */
090    public AbstractManagedObjectDefinition<?, ?> getManagedObjectDefinition() {
091        return d;
092    }
093
094    /**
095     * Gets the reason why the definition could not be determined.
096     *
097     * @return Returns the reason why the definition could not be determined.
098     */
099    public Reason getReason() {
100        return reason;
101    }
102}