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.server;
029
030
031
032import static org.opends.messages.AdminMessages.*;
033
034import java.util.Collection;
035import java.util.Collections;
036import java.util.LinkedList;
037
038import org.forgerock.i18n.LocalizableMessage;
039import org.forgerock.i18n.LocalizableMessageBuilder;
040import org.opends.server.admin.DecodingException;
041import org.opends.server.admin.ManagedObjectDefinition;
042import org.opends.server.admin.PropertyException;
043import org.forgerock.util.Reject;
044
045
046
047/**
048 * The requested server managed object was found but one or more of
049 * its properties could not be decoded successfully.
050 */
051public class ServerManagedObjectDecodingException extends DecodingException {
052
053  /**
054   * Version ID required by serializable classes.
055   */
056  private static final long serialVersionUID = 1598401431084729853L;
057
058
059
060  /** Create the message. */
061  private static LocalizableMessage createMessage(
062      ServerManagedObject<?> 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 server managed object. */
093  private final ServerManagedObject<?> partialManagedObject;
094
095
096
097  /**
098   * Create a new property decoding exception.
099   *
100   * @param partialManagedObject
101   *          The partially created server 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 ServerManagedObjectDecodingException(
109      ServerManagedObject<?> partialManagedObject,
110      Collection<PropertyException> causes) {
111    super(createMessage(partialManagedObject, causes));
112
113    this.partialManagedObject = partialManagedObject;
114    this.causes = Collections
115        .unmodifiableList(new LinkedList<PropertyException>(causes));
116  }
117
118
119
120  /**
121   * Get an unmodifiable collection view of the causes of this
122   * exception.
123   *
124   * @return Returns an unmodifiable collection view of the causes of
125   *         this exception.
126   */
127  public Collection<PropertyException> getCauses() {
128    return causes;
129  }
130
131
132
133  /**
134   * Get the partially created server managed object containing
135   * properties which were successfully decoded and empty properties
136   * for those which were not (this may include empty mandatory
137   * properties).
138   *
139   * @return Returns the partially created server managed object
140   *         containing properties which were successfully decoded and
141   *         empty properties for those which were not (this may
142   *         include empty mandatory properties).
143   */
144  public ServerManagedObject<?> getPartialManagedObject() {
145    return partialManagedObject;
146  }
147
148}