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