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.client; 029 030 031 032 import static org.opends.messages.AdminMessages.*; 033 034 import org.opends.messages.Message; 035 import org.opends.messages.MessageBuilder; 036 037 import java.util.Collection; 038 import java.util.Collections; 039 import java.util.LinkedList; 040 041 import org.opends.server.admin.DecodingException; 042 import org.opends.server.admin.ManagedObjectDefinition; 043 import org.opends.server.admin.PropertyException; 044 import org.opends.server.util.Validator; 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 */ 052 public 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 Message createMessage(ManagedObject<?> 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 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 }