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