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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.admin.server;
029    
030    
031    
032    import static org.opends.messages.AdminMessages.*;
033    
034    import java.util.Collection;
035    import java.util.Collections;
036    import java.util.LinkedList;
037    
038    import org.opends.messages.Message;
039    import org.opends.messages.MessageBuilder;
040    import org.opends.server.admin.DecodingException;
041    import org.opends.server.admin.ManagedObjectDefinition;
042    import org.opends.server.admin.PropertyException;
043    import org.opends.server.util.Validator;
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     */
051    public 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 Message createMessage(
062          ServerManagedObject<?> partialManagedObject,
063          Collection<PropertyException> causes) {
064        Validator.ensureNotNull(causes);
065        Validator.ensureTrue(!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          MessageBuilder builder = new MessageBuilder();
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    }