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