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 legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS. 026 */ 027package org.forgerock.opendj.config.client; 028 029import static com.forgerock.opendj.ldap.AdminMessages.*; 030 031import java.util.ArrayList; 032import java.util.Collection; 033import java.util.Collections; 034 035import org.forgerock.i18n.LocalizableMessage; 036import org.forgerock.i18n.LocalizableMessageBuilder; 037import org.forgerock.opendj.config.OperationsException; 038import org.forgerock.opendj.config.PropertyException; 039import org.forgerock.util.Reject; 040 041/** 042 * This exception is thrown when an attempt is made to add or modify a managed 043 * object when one or more of its mandatory properties are undefined. 044 */ 045public class MissingMandatoryPropertiesException extends OperationsException { 046 047 /** 048 * Serialization ID. 049 */ 050 private static final long serialVersionUID = 6342522125252055588L; 051 052 /** Create the message. */ 053 private static LocalizableMessage createMessage(Collection<PropertyException> causes) { 054 Reject.ifNull(causes); 055 Reject.ifFalse(!causes.isEmpty(), "causes should not be empty"); 056 057 if (causes.size() == 1) { 058 return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_SINGLE.get(causes.iterator().next() 059 .getPropertyDefinition().getName()); 060 } else { 061 LocalizableMessageBuilder builder = new LocalizableMessageBuilder(); 062 063 boolean isFirst = true; 064 for (PropertyException cause : causes) { 065 if (!isFirst) { 066 builder.append(", "); 067 } 068 builder.append(cause.getPropertyDefinition().getName()); 069 isFirst = false; 070 } 071 072 return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_PLURAL.get(builder.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 * Creates a new missing mandatory properties exception with the provided 087 * causes. 088 * 089 * @param ufn 090 * The user friendly name of the component that caused this 091 * exception. 092 * @param causes 093 * The causes of this exception (must be non-<code>null</code> 094 * and non-empty). 095 * @param isCreate 096 * Indicates whether the exception occurred during managed object 097 * creation. 098 */ 099 public MissingMandatoryPropertiesException(LocalizableMessage ufn, 100 Collection<PropertyException> causes, boolean isCreate) { 101 super(createMessage(causes)); 102 103 this.causes = new ArrayList<>(causes); 104 this.ufn = ufn; 105 this.isCreate = isCreate; 106 } 107 108 /** 109 * Gets the first exception that caused this exception. 110 * 111 * @return Returns the first exception that caused this exception. 112 */ 113 @Override 114 public PropertyException getCause() { 115 return causes.iterator().next(); 116 } 117 118 /** 119 * Gets an unmodifiable collection view of the causes of this exception. 120 * 121 * @return Returns an unmodifiable collection view of the causes of this 122 * exception. 123 */ 124 public Collection<PropertyException> getCauses() { 125 return Collections.unmodifiableCollection(causes); 126 } 127 128 /** 129 * Gets the user friendly name of the component that caused this exception. 130 * 131 * @return Returns the user friendly name of the component that caused this 132 * exception. 133 */ 134 public LocalizableMessage getUserFriendlyName() { 135 return ufn; 136 } 137 138 /** 139 * Indicates whether or not this exception was thrown during managed object 140 * creation or during modification. 141 * 142 * @return Returns <code>true</code> if this exception was thrown during 143 * managed object creation. 144 */ 145 public boolean isCreate() { 146 return isCreate; 147 } 148 149}