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 */
017package org.forgerock.opendj.config.client;
018
019import static com.forgerock.opendj.ldap.config.ConfigMessages.*;
020
021import java.util.Collection;
022import java.util.Collections;
023import java.util.LinkedList;
024
025import org.forgerock.i18n.LocalizableMessage;
026import org.forgerock.i18n.LocalizableMessageBuilder;
027import org.forgerock.opendj.config.DecodingException;
028import org.forgerock.opendj.config.ManagedObjectDefinition;
029import org.forgerock.opendj.config.PropertyException;
030import org.forgerock.util.Reject;
031
032/**
033 * The requested managed object was found but one or more of its properties
034 * could not be decoded successfully.
035 */
036public class ManagedObjectDecodingException extends DecodingException {
037
038    /**
039     * Version ID required by serializable classes.
040     */
041    private static final long serialVersionUID = -4268510652395945357L;
042
043    /** Create the message. */
044    private static LocalizableMessage createMessage(ManagedObject<?> partialManagedObject,
045        Collection<PropertyException> causes) {
046        Reject.ifNull(causes);
047        Reject.ifFalse(!causes.isEmpty(), "causes should not be empty");
048
049        ManagedObjectDefinition<?, ?> d = partialManagedObject.getManagedObjectDefinition();
050        if (causes.size() == 1) {
051            return ERR_MANAGED_OBJECT_DECODING_EXCEPTION_SINGLE.get(d.getUserFriendlyName(), causes.iterator().next()
052                .getMessageObject());
053        } else {
054            LocalizableMessageBuilder builder = new LocalizableMessageBuilder();
055
056            boolean isFirst = true;
057            for (PropertyException cause : causes) {
058                if (!isFirst) {
059                    builder.append("; ");
060                }
061                builder.append(cause.getMessageObject());
062                isFirst = false;
063            }
064
065            return ERR_MANAGED_OBJECT_DECODING_EXCEPTION_PLURAL.get(d.getUserFriendlyName(), builder.toMessage());
066        }
067    }
068
069    /** The exception(s) that caused this decoding exception. */
070    private final Collection<PropertyException> causes;
071
072    /** The partially created managed object. */
073    private final ManagedObject<?> partialManagedObject;
074
075    /**
076     * Create a new property decoding exception.
077     *
078     * @param partialManagedObject
079     *            The partially created managed object containing properties
080     *            which were successfully decoded and empty properties for those
081     *            which were not (this may include empty mandatory properties).
082     * @param causes
083     *            The exception(s) that caused this decoding exception.
084     */
085    public ManagedObjectDecodingException(ManagedObject<?> partialManagedObject, Collection<PropertyException> causes) {
086        super(createMessage(partialManagedObject, causes));
087
088        this.partialManagedObject = partialManagedObject;
089        this.causes = Collections.unmodifiableList(new LinkedList<PropertyException>(causes));
090    }
091
092    /**
093     * Get an unmodifiable collection view of the causes of this exception.
094     *
095     * @return Returns an unmodifiable collection view of the causes of this
096     *         exception.
097     */
098    public Collection<PropertyException> getCauses() {
099        return causes;
100    }
101
102    /**
103     * Get the partially created managed object containing properties which were
104     * successfully decoded and empty properties for those which were not (this
105     * may include empty mandatory properties).
106     *
107     * @return Returns the partially created managed object containing
108     *         properties which were successfully decoded and empty properties
109     *         for those which were not (this may include empty mandatory
110     *         properties).
111     */
112    public ManagedObject<?> getPartialManagedObject() {
113        return partialManagedObject;
114    }
115
116}