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