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