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.server; 028 029import static com.forgerock.opendj.ldap.AdminMessages.*; 030 031import org.forgerock.opendj.config.DecodingException; 032import org.forgerock.util.Reject; 033 034import java.util.ArrayList; 035import java.util.Collection; 036import java.util.Collections; 037 038import org.forgerock.i18n.LocalizableMessage; 039import org.forgerock.i18n.LocalizableMessageBuilder; 040 041/** 042 * This exception is thrown when the server refuses to use or delete a managed 043 * object due to one or more constraints that cannot be satisfied. 044 */ 045public class ConstraintViolationException extends DecodingException { 046 047 /** 048 * Serialization ID. 049 */ 050 private static final long serialVersionUID = -4902443848460011875L; 051 052 /** The server managed object. */ 053 private final ServerManagedObject<?> managedObject; 054 055 /** Gets the default message. */ 056 private static LocalizableMessage getDefaultMessage(Collection<LocalizableMessage> messages) { 057 Reject.ifNull(messages); 058 Reject.ifFalse(!messages.isEmpty(), "messages should not be empty"); 059 060 if (messages.size() == 1) { 061 return ERR_CONSTRAINT_VIOLATION_EXCEPTION_SINGLE.get(messages.iterator().next()); 062 } else { 063 return ERR_CONSTRAINT_VIOLATION_EXCEPTION_PLURAL.get(getSingleMessage(messages)); 064 } 065 } 066 067 /** Merge the messages into a single message. */ 068 private static LocalizableMessage getSingleMessage(Collection<LocalizableMessage> messages) { 069 if (messages.size() == 1) { 070 return messages.iterator().next(); 071 } else { 072 LocalizableMessageBuilder builder = new LocalizableMessageBuilder(); 073 074 boolean isFirst = true; 075 for (LocalizableMessage m : messages) { 076 if (!isFirst) { 077 builder.append("; "); 078 } 079 builder.append(m); 080 isFirst = false; 081 } 082 083 return builder.toMessage(); 084 } 085 } 086 087 /** The messages describing the constraint violations that occurred. */ 088 private final Collection<LocalizableMessage> messages; 089 090 /** 091 * Creates a new constraint violation exception with the provided messages. 092 * 093 * @param managedObject 094 * The server managed object which caused the constraint 095 * violations. 096 * @param messages 097 * The messages describing the constraint violations that 098 * occurred (must be non-<code>null</code> and non-empty). 099 */ 100 public ConstraintViolationException(ServerManagedObject<?> managedObject, Collection<LocalizableMessage> messages) { 101 super(getDefaultMessage(messages)); 102 103 this.managedObject = managedObject; 104 this.messages = new ArrayList<>(messages); 105 } 106 107 /** 108 * Creates a new constraint violation exception with the provided message. 109 * 110 * @param managedObject 111 * The server managed object which caused the constraint 112 * violations. 113 * @param message 114 * The message describing the constraint violation that occurred. 115 */ 116 public ConstraintViolationException(ServerManagedObject<?> managedObject, LocalizableMessage message) { 117 this(managedObject, Collections.singleton(message)); 118 } 119 120 /** 121 * Gets an unmodifiable collection view of the messages describing the 122 * constraint violations that occurred. 123 * 124 * @return Returns an unmodifiable collection view of the messages 125 * describing the constraint violations that occurred. 126 */ 127 public Collection<LocalizableMessage> getMessages() { 128 return Collections.unmodifiableCollection(messages); 129 } 130 131 /** 132 * Creates a single message listing all the messages combined into a single 133 * list separated by semi-colons. 134 * 135 * @return Returns a single message listing all the messages combined into a 136 * single list separated by semi-colons. 137 */ 138 public LocalizableMessage getMessagesAsSingleMessage() { 139 return getSingleMessage(messages); 140 } 141 142 /** 143 * Gets the server managed object which caused the constraint violations. 144 * 145 * @return Returns the server managed object which caused the constraint 146 * violations. 147 */ 148 public ServerManagedObject<?> getManagedObject() { 149 return managedObject; 150 } 151}