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