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