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