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;
029
030
031
032import static org.opends.messages.AdminMessages.*;
033
034import org.forgerock.i18n.LocalizableMessage;
035
036
037
038/**
039 * Exceptions thrown as a result of errors that occurred when decoding and
040 * modifying property values.
041 */
042public final class PropertyException extends RuntimeException
043{
044
045  /**
046   * Version ID required by serializable classes.
047   */
048  private static final long serialVersionUID = -8465109598081914482L;
049
050
051
052  /**
053   * Creates a new default behavior exception with a cause.
054   *
055   * @param pd
056   *          The property definition whose default values could not be
057   *          determined.
058   * @param cause
059   *          The exception that prevented the default values from being
060   *          determined.
061   * @return A new default behavior exception with a cause.
062   */
063  public static PropertyException defaultBehaviorException(
064      PropertyDefinition<?> pd, Throwable cause)
065  {
066    return new PropertyException(pd,
067        ERR_DEFAULT_BEHAVIOR_PROPERTY_EXCEPTION.get(pd.getName()), cause);
068  }
069
070
071
072  /**
073   * Creates a new illegal property value exception.
074   *
075   * @param pd
076   *          The property definition.
077   * @param value
078   *          The illegal property value.
079   * @return A new illegal property value exception.
080   */
081  public static PropertyException illegalPropertyValueException(
082      PropertyDefinition<?> pd, Object value)
083  {
084    return new PropertyException(pd, createMessage(pd, value));
085  }
086
087
088
089  /**
090   * Creates a new illegal property value exception.
091   *
092   * @param pd
093   *          The property definition.
094   * @param value
095   *          The illegal property value.
096   * @param cause
097   *          The cause.
098   * @return A new illegal property value exception.
099   */
100  public static PropertyException illegalPropertyValueException(
101      PropertyDefinition<?> pd, Object value, Throwable cause)
102  {
103    return new PropertyException(pd, createMessage(pd, value), cause);
104  }
105
106
107
108  /**
109   * Create a new property is mandatory exception.
110   *
111   * @param pd
112   *          The property definition.
113   * @return A new property is mandatory exception.
114   */
115  public static PropertyException propertyIsMandatoryException(
116      PropertyDefinition<?> pd)
117  {
118    return new PropertyException(pd, ERR_PROPERTY_IS_MANDATORY_EXCEPTION.get(pd
119        .getName()));
120  }
121
122
123
124  /**
125   * Create a new property is read-only exception.
126   *
127   * @param pd
128   *          The property definition.
129   * @return A new property is read-only exception.
130   */
131  public static PropertyException propertyIsReadOnlyException(
132      PropertyDefinition<?> pd)
133  {
134    return new PropertyException(pd, ERR_PROPERTY_IS_READ_ONLY_EXCEPTION.get(pd
135        .getName()));
136  }
137
138
139
140  /**
141   * Create a new property is single valued exception.
142   *
143   * @param pd
144   *          The property definition.
145   * @return A new property is single valued exception.
146   */
147  public static PropertyException propertyIsSingleValuedException(
148      PropertyDefinition<?> pd)
149  {
150    return new PropertyException(pd,
151        ERR_PROPERTY_IS_SINGLE_VALUED_EXCEPTION.get(pd.getName()));
152  }
153
154
155
156  /**
157   * Creates a new unknown property definition exception.
158   *
159   * @param pd
160   *          The unknown property definition.
161   * @param p
162   *          The visitor parameter if there was one.
163   * @return A new unknown property definition exception.
164   */
165  public static PropertyException unknownPropertyDefinitionException(
166      PropertyDefinition<?> pd, Object p)
167  {
168    return new PropertyException(pd,
169        ERR_UNKNOWN_PROPERTY_DEFINITION_EXCEPTION.get(pd.getName(), pd
170            .getClass().getName()));
171  }
172
173
174
175  /** Create the message. */
176  private static LocalizableMessage createMessage(PropertyDefinition<?> pd, Object value)
177  {
178    PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
179    return ERR_ILLEGAL_PROPERTY_VALUE_EXCEPTION.get(value, pd.getName(), builder.getUsage(pd));
180  }
181
182
183
184  /** LocalizableMessage that explains the problem. */
185  private final LocalizableMessage message;
186
187  /**
188   * The property definition associated with the property that caused the
189   * exception.
190   */
191  private final PropertyDefinition<?> pd;
192
193
194
195  private PropertyException(PropertyDefinition<?> pd, LocalizableMessage message)
196  {
197    super(message.toString());
198    this.message = message;
199    this.pd = pd;
200  }
201
202
203
204  private PropertyException(PropertyDefinition<?> pd, LocalizableMessage message,
205      Throwable cause)
206  {
207    super(message.toString(), cause);
208    this.message = message;
209    this.pd = pd;
210  }
211
212
213
214  /**
215   * Returns the message that explains the problem that occurred.
216   *
217   * @return Returns the message describing the problem that occurred (never
218   *         <code>null</code>).
219   */
220  public LocalizableMessage getMessageObject()
221  {
222    return message;
223  }
224
225
226
227  /**
228   * Get the property definition associated with the property that caused the
229   * exception.
230   *
231   * @return Returns the property definition associated with the property that
232   *         caused the exception.
233   */
234  public final PropertyDefinition<?> getPropertyDefinition()
235  {
236    return pd;
237  }
238
239}