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 2014-2015 ForgeRock AS.
016 */
017
018package org.opends.server.admin.client;
019
020
021
022import static org.opends.messages.AdminMessages.*;
023
024import org.forgerock.i18n.LocalizableMessage;
025import org.forgerock.i18n.LocalizableMessageBuilder;
026
027import java.util.Collection;
028import java.util.Collections;
029import java.util.LinkedList;
030
031import org.opends.server.admin.DecodingException;
032import org.opends.server.admin.ManagedObjectDefinition;
033import org.opends.server.admin.PropertyException;
034import org.forgerock.util.Reject;
035
036
037
038/**
039 * The requested managed object was found but one or more of its
040 * properties could not be decoded successfully.
041 */
042public class ManagedObjectDecodingException extends DecodingException {
043
044  /**
045   * Version ID required by serializable classes.
046   */
047  private static final long serialVersionUID = -4268510652395945357L;
048
049
050
051  /** Create the message. */
052  private static LocalizableMessage createMessage(ManagedObject<?> partialManagedObject,
053      Collection<PropertyException> causes) {
054    Reject.ifNull(causes);
055    Reject.ifFalse(!causes.isEmpty());
056
057    ManagedObjectDefinition<?, ?> d = partialManagedObject
058        .getManagedObjectDefinition();
059    if (causes.size() == 1) {
060      return ERR_MANAGED_OBJECT_DECODING_EXCEPTION_SINGLE.get(d
061          .getUserFriendlyName(), causes.iterator().next().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
075          .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 managed object. */
083  private final ManagedObject<?> partialManagedObject;
084
085
086
087  /**
088   * Create a new property decoding exception.
089   *
090   * @param partialManagedObject
091   *          The partially created managed object containing
092   *          properties which were successfully decoded and empty
093   *          properties for those which were not (this may include
094   *          empty mandatory properties).
095   * @param causes
096   *          The exception(s) that caused this decoding exception.
097   */
098  public ManagedObjectDecodingException(ManagedObject<?> partialManagedObject,
099      Collection<PropertyException> causes) {
100    super(createMessage(partialManagedObject, causes));
101
102    this.partialManagedObject = partialManagedObject;
103    this.causes = Collections
104        .unmodifiableList(new LinkedList<PropertyException>(causes));
105  }
106
107
108
109  /**
110   * Get an unmodifiable collection view of the causes of this
111   * exception.
112   *
113   * @return Returns an unmodifiable collection view of the causes of
114   *         this exception.
115   */
116  public Collection<PropertyException> getCauses() {
117    return causes;
118  }
119
120
121
122  /**
123   * Get the partially created managed object containing properties
124   * which were successfully decoded and empty properties for those
125   * which were not (this may include empty mandatory properties).
126   *
127   * @return Returns the partially created managed object containing
128   *         properties which were successfully decoded and empty
129   *         properties for those which were not (this may include
130   *         empty mandatory properties).
131   */
132  public ManagedObject<?> getPartialManagedObject() {
133    return partialManagedObject;
134  }
135
136}