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