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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.forgerock.opendj.config.client;
028
029import static com.forgerock.opendj.ldap.config.AdminMessages.*;
030
031import java.util.Collection;
032import java.util.Collections;
033import java.util.LinkedList;
034
035import org.forgerock.i18n.LocalizableMessage;
036import org.forgerock.i18n.LocalizableMessageBuilder;
037import org.forgerock.opendj.config.DecodingException;
038import org.forgerock.opendj.config.ManagedObjectDefinition;
039import org.forgerock.opendj.config.PropertyException;
040import org.forgerock.util.Reject;
041
042/**
043 * The requested managed object was found but one or more of its properties
044 * could not be decoded successfully.
045 */
046public class ManagedObjectDecodingException extends DecodingException {
047
048    /**
049     * Version ID required by serializable classes.
050     */
051    private static final long serialVersionUID = -4268510652395945357L;
052
053    /** Create the message. */
054    private static LocalizableMessage createMessage(ManagedObject<?> partialManagedObject,
055        Collection<PropertyException> causes) {
056        Reject.ifNull(causes);
057        Reject.ifFalse(!causes.isEmpty(), "causes should not be empty");
058
059        ManagedObjectDefinition<?, ?> d = partialManagedObject.getManagedObjectDefinition();
060        if (causes.size() == 1) {
061            return ERR_MANAGED_OBJECT_DECODING_EXCEPTION_SINGLE.get(d.getUserFriendlyName(), causes.iterator().next()
062                .getMessageObject());
063        } else {
064            LocalizableMessageBuilder builder = new LocalizableMessageBuilder();
065
066            boolean isFirst = true;
067            for (PropertyException cause : causes) {
068                if (!isFirst) {
069                    builder.append("; ");
070                }
071                builder.append(cause.getMessageObject());
072                isFirst = false;
073            }
074
075            return ERR_MANAGED_OBJECT_DECODING_EXCEPTION_PLURAL.get(d.getUserFriendlyName(), builder.toMessage());
076        }
077    }
078
079    /** The exception(s) that caused this decoding exception. */
080    private final Collection<PropertyException> causes;
081
082    /** The partially created managed object. */
083    private final ManagedObject<?> partialManagedObject;
084
085    /**
086     * Create a new property decoding exception.
087     *
088     * @param partialManagedObject
089     *            The partially created managed object containing properties
090     *            which were successfully decoded and empty properties for those
091     *            which were not (this may include empty mandatory properties).
092     * @param causes
093     *            The exception(s) that caused this decoding exception.
094     */
095    public ManagedObjectDecodingException(ManagedObject<?> partialManagedObject, Collection<PropertyException> causes) {
096        super(createMessage(partialManagedObject, causes));
097
098        this.partialManagedObject = partialManagedObject;
099        this.causes = Collections.unmodifiableList(new LinkedList<PropertyException>(causes));
100    }
101
102    /**
103     * Get an unmodifiable collection view of the causes of this exception.
104     *
105     * @return Returns an unmodifiable collection view of the causes of this
106     *         exception.
107     */
108    public Collection<PropertyException> getCauses() {
109        return causes;
110    }
111
112    /**
113     * Get the partially created managed object containing properties which were
114     * successfully decoded and empty properties for those which were not (this
115     * may include empty mandatory properties).
116     *
117     * @return Returns the partially created managed object containing
118     *         properties which were successfully decoded and empty properties
119     *         for those which were not (this may include empty mandatory
120     *         properties).
121     */
122    public ManagedObject<?> getPartialManagedObject() {
123        return partialManagedObject;
124    }
125
126}