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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.admin.client;
029    
030    
031    
032    import static org.opends.messages.AdminMessages.*;
033    
034    import org.opends.messages.Message;
035    import org.opends.server.admin.IllegalPropertyValueStringException;
036    import org.opends.server.admin.OperationsException;
037    import org.opends.server.admin.PropertyDefinition;
038    import 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     */
051    public 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 Message 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 (IllegalPropertyValueStringException 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    }