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