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 */
026
027package org.forgerock.opendj.config.server;
028
029import static com.forgerock.opendj.ldap.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 server managed object was found but one or more of its
044 * properties could not be decoded successfully.
045 */
046public class ServerManagedObjectDecodingException extends DecodingException {
047
048    /**
049     * Version ID required by serializable classes.
050     */
051    private static final long serialVersionUID = 1598401431084729853L;
052
053    /** Create the message. */
054    private static LocalizableMessage createMessage(ServerManagedObject<?> partialManagedObject,
055            Collection<PropertyException> causes) {
056        Reject.ifNull(causes);
057        Reject.ifFalse(!causes.isEmpty(), "causes should nnot 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 server managed object. */
083    private final ServerManagedObject<?> partialManagedObject;
084
085    /**
086     * Create a new property decoding exception.
087     *
088     * @param partialManagedObject
089     *            The partially created server managed object containing
090     *            properties which were successfully decoded and empty
091     *            properties for those which were not (this may include empty
092     *            mandatory properties).
093     * @param causes
094     *            The exception(s) that caused this decoding exception.
095     */
096    public ServerManagedObjectDecodingException(ServerManagedObject<?> partialManagedObject,
097            Collection<PropertyException> causes) {
098        super(createMessage(partialManagedObject, causes));
099
100        this.partialManagedObject = partialManagedObject;
101        this.causes = Collections.unmodifiableList(new LinkedList<PropertyException>(causes));
102    }
103
104    /**
105     * Get an unmodifiable collection view of the causes of this exception.
106     *
107     * @return Returns an unmodifiable collection view of the causes of this
108     *         exception.
109     */
110    public Collection<PropertyException> getCauses() {
111        return causes;
112    }
113
114    /**
115     * Get the partially created server managed object containing properties
116     * which were successfully decoded and empty properties for those which were
117     * not (this may include empty mandatory properties).
118     *
119     * @return Returns the partially created server managed object containing
120     *         properties which were successfully decoded and empty properties
121     *         for those which were not (this may include empty mandatory
122     *         properties).
123     */
124    public ServerManagedObject<?> getPartialManagedObject() {
125        return partialManagedObject;
126    }
127
128}