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.server;
019
020
021
022import static org.opends.messages.AdminMessages.*;
023
024import java.util.Collection;
025import java.util.Collections;
026import java.util.LinkedList;
027
028import org.forgerock.i18n.LocalizableMessage;
029import org.forgerock.i18n.LocalizableMessageBuilder;
030import org.opends.server.admin.DecodingException;
031import org.opends.server.admin.ManagedObjectDefinition;
032import org.opends.server.admin.PropertyException;
033import org.forgerock.util.Reject;
034
035
036
037/**
038 * The requested server managed object was found but one or more of
039 * its properties could not be decoded successfully.
040 */
041public class ServerManagedObjectDecodingException extends DecodingException {
042
043  /**
044   * Version ID required by serializable classes.
045   */
046  private static final long serialVersionUID = 1598401431084729853L;
047
048
049
050  /** Create the message. */
051  private static LocalizableMessage createMessage(
052      ServerManagedObject<?> 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 server managed object. */
083  private final ServerManagedObject<?> partialManagedObject;
084
085
086
087  /**
088   * Create a new property decoding exception.
089   *
090   * @param partialManagedObject
091   *          The partially created server 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 ServerManagedObjectDecodingException(
099      ServerManagedObject<?> partialManagedObject,
100      Collection<PropertyException> causes) {
101    super(createMessage(partialManagedObject, causes));
102
103    this.partialManagedObject = partialManagedObject;
104    this.causes = Collections
105        .unmodifiableList(new LinkedList<PropertyException>(causes));
106  }
107
108
109
110  /**
111   * Get an unmodifiable collection view of the causes of this
112   * exception.
113   *
114   * @return Returns an unmodifiable collection view of the causes of
115   *         this exception.
116   */
117  public Collection<PropertyException> getCauses() {
118    return causes;
119  }
120
121
122
123  /**
124   * Get the partially created server managed object containing
125   * properties which were successfully decoded and empty properties
126   * for those which were not (this may include empty mandatory
127   * properties).
128   *
129   * @return Returns the partially created server managed object
130   *         containing properties which were successfully decoded and
131   *         empty properties for those which were not (this may
132   *         include empty mandatory properties).
133   */
134  public ServerManagedObject<?> getPartialManagedObject() {
135    return partialManagedObject;
136  }
137
138}