001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions copyright 2014-2015 ForgeRock AS.
016 */
017
018package org.opends.server.admin;
019
020
021
022import static org.opends.messages.AdminMessages.*;
023
024import org.forgerock.i18n.LocalizableMessage;
025
026
027
028/**
029 * Exceptions thrown as a result of errors that occurred when decoding and
030 * modifying property values.
031 */
032public final class PropertyException extends RuntimeException
033{
034
035  /**
036   * Version ID required by serializable classes.
037   */
038  private static final long serialVersionUID = -8465109598081914482L;
039
040
041
042  /**
043   * Creates a new default behavior exception with a cause.
044   *
045   * @param pd
046   *          The property definition whose default values could not be
047   *          determined.
048   * @param cause
049   *          The exception that prevented the default values from being
050   *          determined.
051   * @return A new default behavior exception with a cause.
052   */
053  public static PropertyException defaultBehaviorException(
054      PropertyDefinition<?> pd, Throwable cause)
055  {
056    return new PropertyException(pd,
057        ERR_DEFAULT_BEHAVIOR_PROPERTY_EXCEPTION.get(pd.getName()), cause);
058  }
059
060
061
062  /**
063   * Creates a new illegal property value exception.
064   *
065   * @param pd
066   *          The property definition.
067   * @param value
068   *          The illegal property value.
069   * @return A new illegal property value exception.
070   */
071  public static PropertyException illegalPropertyValueException(
072      PropertyDefinition<?> pd, Object value)
073  {
074    return new PropertyException(pd, createMessage(pd, value));
075  }
076
077
078
079  /**
080   * Creates a new illegal property value exception.
081   *
082   * @param pd
083   *          The property definition.
084   * @param value
085   *          The illegal property value.
086   * @param cause
087   *          The cause.
088   * @return A new illegal property value exception.
089   */
090  public static PropertyException illegalPropertyValueException(
091      PropertyDefinition<?> pd, Object value, Throwable cause)
092  {
093    return new PropertyException(pd, createMessage(pd, value), cause);
094  }
095
096
097
098  /**
099   * Create a new property is mandatory exception.
100   *
101   * @param pd
102   *          The property definition.
103   * @return A new property is mandatory exception.
104   */
105  public static PropertyException propertyIsMandatoryException(
106      PropertyDefinition<?> pd)
107  {
108    return new PropertyException(pd, ERR_PROPERTY_IS_MANDATORY_EXCEPTION.get(pd
109        .getName()));
110  }
111
112
113
114  /**
115   * Create a new property is read-only exception.
116   *
117   * @param pd
118   *          The property definition.
119   * @return A new property is read-only exception.
120   */
121  public static PropertyException propertyIsReadOnlyException(
122      PropertyDefinition<?> pd)
123  {
124    return new PropertyException(pd, ERR_PROPERTY_IS_READ_ONLY_EXCEPTION.get(pd
125        .getName()));
126  }
127
128
129
130  /**
131   * Create a new property is single valued exception.
132   *
133   * @param pd
134   *          The property definition.
135   * @return A new property is single valued exception.
136   */
137  public static PropertyException propertyIsSingleValuedException(
138      PropertyDefinition<?> pd)
139  {
140    return new PropertyException(pd,
141        ERR_PROPERTY_IS_SINGLE_VALUED_EXCEPTION.get(pd.getName()));
142  }
143
144
145
146  /**
147   * Creates a new unknown property definition exception.
148   *
149   * @param pd
150   *          The unknown property definition.
151   * @param p
152   *          The visitor parameter if there was one.
153   * @return A new unknown property definition exception.
154   */
155  public static PropertyException unknownPropertyDefinitionException(
156      PropertyDefinition<?> pd, Object p)
157  {
158    return new PropertyException(pd,
159        ERR_UNKNOWN_PROPERTY_DEFINITION_EXCEPTION.get(pd.getName(), pd
160            .getClass().getName()));
161  }
162
163
164
165  /** Create the message. */
166  private static LocalizableMessage createMessage(PropertyDefinition<?> pd, Object value)
167  {
168    PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
169    return ERR_ILLEGAL_PROPERTY_VALUE_EXCEPTION.get(value, pd.getName(), builder.getUsage(pd));
170  }
171
172
173
174  /** LocalizableMessage that explains the problem. */
175  private final LocalizableMessage message;
176
177  /**
178   * The property definition associated with the property that caused the
179   * exception.
180   */
181  private final PropertyDefinition<?> pd;
182
183
184
185  private PropertyException(PropertyDefinition<?> pd, LocalizableMessage message)
186  {
187    super(message.toString());
188    this.message = message;
189    this.pd = pd;
190  }
191
192
193
194  private PropertyException(PropertyDefinition<?> pd, LocalizableMessage message,
195      Throwable cause)
196  {
197    super(message.toString(), cause);
198    this.message = message;
199    this.pd = pd;
200  }
201
202
203
204  /**
205   * Returns the message that explains the problem that occurred.
206   *
207   * @return Returns the message describing the problem that occurred (never
208   *         <code>null</code>).
209   */
210  public LocalizableMessage getMessageObject()
211  {
212    return message;
213  }
214
215
216
217  /**
218   * Get the property definition associated with the property that caused the
219   * exception.
220   *
221   * @return Returns the property definition associated with the property that
222   *         caused the exception.
223   */
224  public final PropertyDefinition<?> getPropertyDefinition()
225  {
226    return pd;
227  }
228
229}