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.client; 019 020 021 022import static org.opends.messages.AdminMessages.*; 023 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.Collections; 027 028import org.forgerock.i18n.LocalizableMessage; 029import org.forgerock.i18n.LocalizableMessageBuilder; 030import org.opends.server.admin.OperationsException; 031import org.opends.server.admin.PropertyException; 032import org.forgerock.util.Reject; 033 034 035 036/** 037 * This exception is thrown when an attempt is made to add or modify a 038 * managed object when one or more of its mandatory properties are 039 * undefined. 040 */ 041public class MissingMandatoryPropertiesException extends OperationsException { 042 043 /** 044 * Serialization ID. 045 */ 046 private static final long serialVersionUID = 6342522125252055588L; 047 048 049 050 /** Create the message. */ 051 private static LocalizableMessage createMessage(Collection<PropertyException> causes) 052 { 053 Reject.ifNull(causes); 054 Reject.ifFalse(!causes.isEmpty()); 055 056 if (causes.size() == 1) { 057 return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_SINGLE.get(causes 058 .iterator().next().getPropertyDefinition().getName()); 059 } else { 060 LocalizableMessageBuilder builder = new LocalizableMessageBuilder(); 061 062 boolean isFirst = true; 063 for (PropertyException cause : causes) { 064 if (!isFirst) { 065 builder.append(", "); 066 } 067 builder.append(cause.getPropertyDefinition().getName()); 068 isFirst = false; 069 } 070 071 return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_PLURAL.get(builder 072 .toMessage()); 073 } 074 } 075 076 /** The causes of this exception. */ 077 private final Collection<PropertyException> causes; 078 079 /** Indicates whether the exception occurred during managed object creation. */ 080 private final boolean isCreate; 081 082 /** The user friendly name of the component that caused this exception. */ 083 private final LocalizableMessage ufn; 084 085 086 087 /** 088 * Creates a new missing mandatory properties exception with the 089 * provided causes. 090 * 091 * @param ufn 092 * The user friendly name of the component that caused this 093 * exception. 094 * @param causes 095 * The causes of this exception (must be non-<code>null</code> 096 * and non-empty). 097 * @param isCreate 098 * Indicates whether the exception occurred during managed 099 * object creation. 100 */ 101 public MissingMandatoryPropertiesException(LocalizableMessage ufn, 102 Collection<PropertyException> causes, boolean isCreate) { 103 super(createMessage(causes)); 104 105 this.causes = new ArrayList<>(causes); 106 this.ufn = ufn; 107 this.isCreate = isCreate; 108 } 109 110 111 112 /** 113 * Gets the first exception that caused this exception. 114 * 115 * @return Returns the first exception that caused this exception. 116 */ 117 @Override 118 public PropertyException getCause() { 119 return causes.iterator().next(); 120 } 121 122 123 124 /** 125 * Gets an unmodifiable collection view of the causes of this 126 * exception. 127 * 128 * @return Returns an unmodifiable collection view of the causes of 129 * this exception. 130 */ 131 public Collection<PropertyException> getCauses() { 132 return Collections.unmodifiableCollection(causes); 133 } 134 135 136 137 /** 138 * Gets the user friendly name of the component that caused this 139 * exception. 140 * 141 * @return Returns the user friendly name of the component that 142 * caused this exception. 143 */ 144 public LocalizableMessage getUserFriendlyName() { 145 return ufn; 146 } 147 148 149 150 /** 151 * Indicates whether or not this exception was thrown during managed 152 * object creation or during modification. 153 * 154 * @return Returns <code>true</code> if this exception was thrown 155 * during managed object creation. 156 */ 157 public boolean isCreate() { 158 return isCreate; 159 } 160 161}