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