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 */
026package org.forgerock.opendj.config.client;
027
028import static com.forgerock.opendj.ldap.AdminMessages.*;
029
030import org.forgerock.i18n.LocalizableMessage;
031import org.forgerock.opendj.config.PropertyException;
032import org.forgerock.opendj.config.OperationsException;
033import org.forgerock.opendj.config.PropertyDefinition;
034import org.forgerock.opendj.config.PropertyDefinitionUsageBuilder;
035
036/**
037 * Thrown when an attempt is made to create a new managed object with an illegal
038 * name.
039 * <p>
040 * This exception can occur when a new managed object is given a name which is
041 * either an empty string, a string containing just white-spaces, or a string
042 * which is invalid according to the managed object's naming property (if it has
043 * one).
044 */
045public class IllegalManagedObjectNameException extends OperationsException {
046
047    /**
048     * Serialization ID.
049     */
050    private static final long serialVersionUID = 7491748228684293291L;
051
052    /** Create the message. */
053    private static LocalizableMessage createMessage(String illegalName,
054            PropertyDefinition<?> namingPropertyDefinition) {
055        if (illegalName.length() == 0) {
056            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_EMPTY.get();
057        } else if (illegalName.trim().length() == 0) {
058            return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_BLANK.get();
059        } else if (namingPropertyDefinition != null) {
060            try {
061                namingPropertyDefinition.decodeValue(illegalName);
062            } catch (PropertyException e) {
063                PropertyDefinitionUsageBuilder builder = new PropertyDefinitionUsageBuilder(true);
064                return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_SYNTAX.get(illegalName,
065                        namingPropertyDefinition.getName(), builder
066                                .getUsage(namingPropertyDefinition));
067            }
068        }
069
070        return ERR_ILLEGAL_MANAGED_OBJECT_NAME_EXCEPTION_OTHER.get(illegalName);
071    }
072
073    /** The illegal name. */
074    private final String illegalName;
075
076    /** The naming property definition if applicable. */
077    private final PropertyDefinition<?> namingPropertyDefinition;
078
079    /**
080     * Create a new illegal name exception and no naming property definition.
081     *
082     * @param illegalName
083     *            The illegal managed object name.
084     */
085    public IllegalManagedObjectNameException(String illegalName) {
086        this(illegalName, null);
087    }
088
089    /**
090     * Create a new illegal name exception and a naming property definition.
091     *
092     * @param illegalName
093     *            The illegal managed object name.
094     * @param namingPropertyDefinition
095     *            The naming property definition.
096     */
097    public IllegalManagedObjectNameException(String illegalName, PropertyDefinition<?> namingPropertyDefinition) {
098        super(createMessage(illegalName, namingPropertyDefinition));
099
100        this.illegalName = illegalName;
101        this.namingPropertyDefinition = namingPropertyDefinition;
102    }
103
104    /**
105     * Get the illegal managed object name.
106     *
107     * @return Returns the illegal managed object name.
108     */
109    public String getIllegalName() {
110        return illegalName;
111    }
112
113    /**
114     * Get the naming property definition if applicable.
115     *
116     * @return Returns naming property definition, or <code>null</code> if none
117     *         was specified.
118     */
119    public PropertyDefinition<?> getNamingPropertyDefinition() {
120        return namingPropertyDefinition;
121    }
122
123}