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    /** Version ID required by serializable classes. */
032    private static final long serialVersionUID = -8465109598081914482L;
033
034    /**
035     * Creates a new default behavior exception with a cause.
036     *
037     * @param pd
038     *            The property definition whose default values could not be
039     *            determined.
040     * @param cause
041     *            The exception that prevented the default values from being
042     *            determined.
043     * @return A new default behavior exception with a cause.
044     */
045    public static PropertyException defaultBehaviorException(final PropertyDefinition<?> pd,
046            final Throwable cause) {
047        return new PropertyException(pd, ERR_DEFAULT_BEHAVIOR_PROPERTY_EXCEPTION.get(pd.getName()),
048                cause);
049    }
050
051    /**
052     * Creates a new illegal property value exception.
053     *
054     * @param pd
055     *            The property definition.
056     * @param value
057     *            The illegal property value.
058     * @return A new illegal property value exception.
059     */
060    public static PropertyException illegalPropertyValueException(final PropertyDefinition<?> pd,
061            final Object value) {
062        return new PropertyException(pd, createMessage(pd, value));
063    }
064
065    /**
066     * Creates a new illegal property value exception.
067     *
068     * @param pd
069     *            The property definition.
070     * @param value
071     *            The illegal property value.
072     * @param cause
073     *            The cause.
074     * @return A new illegal property value exception.
075     */
076    public static PropertyException illegalPropertyValueException(final PropertyDefinition<?> pd,
077            final Object value, final Throwable cause) {
078        return new PropertyException(pd, createMessage(pd, value), cause);
079    }
080
081    /**
082     * Creates a new property is mandatory exception.
083     *
084     * @param pd
085     *            The property definition.
086     * @return A new property is mandatory exception.
087     */
088    public static PropertyException propertyIsMandatoryException(final PropertyDefinition<?> pd) {
089        return new PropertyException(pd, ERR_PROPERTY_IS_MANDATORY_EXCEPTION.get(pd.getName()));
090    }
091
092    /**
093     * Creates a new property is read-only exception.
094     *
095     * @param pd
096     *            The property definition.
097     * @return A new property is read-only exception.
098     */
099    public static PropertyException propertyIsReadOnlyException(final PropertyDefinition<?> pd) {
100        return new PropertyException(pd, ERR_PROPERTY_IS_READ_ONLY_EXCEPTION.get(pd.getName()));
101    }
102
103    /**
104     * Creates a new property is single valued exception.
105     *
106     * @param pd
107     *            The property definition.
108     * @return A new property is single valued exception.
109     */
110    public static PropertyException propertyIsSingleValuedException(final PropertyDefinition<?> pd) {
111        return new PropertyException(pd, ERR_PROPERTY_IS_SINGLE_VALUED_EXCEPTION.get(pd.getName()));
112    }
113
114    /**
115     * Creates a new unknown property definition exception.
116     *
117     * @param pd
118     *            The unknown property definition.
119     * @return A new unknown property definition exception.
120     */
121    public static PropertyException unknownPropertyDefinitionException(final PropertyDefinition<?> pd) {
122        return new PropertyException(pd, ERR_UNKNOWN_PROPERTY_DEFINITION_EXCEPTION.get(
123                pd.getName(), pd.getClass().getName()));
124    }
125
126    /** Create the message. */
127    private static LocalizableMessage createMessage(final PropertyDefinition<?> pd,
128            final Object value) {
129        final PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
130        return ERR_ILLEGAL_PROPERTY_VALUE_EXCEPTION.get(value, pd.getName(), builder.getUsage(pd));
131    }
132
133    /** LocalizableMessage that explains the problem. */
134    private final LocalizableMessage message;
135
136    /** The property definition associated with the property that caused the exception. */
137    private final PropertyDefinition<?> pd;
138
139    private PropertyException(final PropertyDefinition<?> pd, final LocalizableMessage message) {
140        super(message.toString());
141        this.message = message;
142        this.pd = pd;
143    }
144
145    private PropertyException(final PropertyDefinition<?> pd, final LocalizableMessage message,
146            final Throwable cause) {
147        super(message.toString(), cause);
148        this.message = message;
149        this.pd = pd;
150    }
151
152    @Override
153    public LocalizableMessage getMessageObject() {
154        return message;
155    }
156
157    /**
158     * Returns the property definition associated with the property that caused
159     * the exception.
160     *
161     * @return The property definition associated with the property that caused
162     *         the exception.
163     */
164    public final PropertyDefinition<?> getPropertyDefinition() {
165        return pd;
166    }
167
168}