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