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