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