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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027
028 package org.opends.server.admin.server;
029
030
031
032 import static org.opends.messages.AdminMessages.*;
033
034 import java.util.ArrayList;
035 import java.util.Collection;
036 import java.util.Collections;
037
038 import org.opends.messages.Message;
039 import org.opends.messages.MessageBuilder;
040 import org.opends.server.admin.DecodingException;
041 import org.opends.server.util.Validator;
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 */
050 public 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 Message getDefaultMessage(Collection<Message> messages) {
064 Validator.ensureNotNull(messages);
065 Validator.ensureTrue(!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 Message getSingleMessage(Collection<Message> messages) {
080 if (messages.size() == 1) {
081 return messages.iterator().next();
082 } else {
083 MessageBuilder builder = new MessageBuilder();
084
085 boolean isFirst = true;
086 for (Message 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<Message> 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<Message> messages) {
117 super(getDefaultMessage(messages));
118
119 this.managedObject = managedObject;
120 this.messages = new ArrayList<Message>(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 Message 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<Message> 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 Message 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 }