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;
029    
030    
031    
032    import static org.opends.server.util.Validator.ensureNotNull;
033    
034    import java.util.EnumSet;
035    
036    import org.opends.server.core.DirectoryServer;
037    import org.opends.server.types.AttributeType;
038    
039    
040    
041    /**
042     * Attribute type property definition.
043     */
044    public final class AttributeTypePropertyDefinition extends
045        PropertyDefinition<AttributeType> {
046    
047      /**
048       * An interface for incrementally constructing attribute type
049       * property definitions.
050       */
051      public static class Builder extends
052          AbstractBuilder<AttributeType, AttributeTypePropertyDefinition> {
053    
054        // Private constructor
055        private Builder(AbstractManagedObjectDefinition<?, ?> d,
056            String propertyName) {
057          super(d, propertyName);
058        }
059    
060    
061    
062        /**
063         * {@inheritDoc}
064         */
065        @Override
066        protected AttributeTypePropertyDefinition buildInstance(
067            AbstractManagedObjectDefinition<?, ?> d, String propertyName,
068            EnumSet<PropertyOption> options,
069            AdministratorAction adminAction,
070            DefaultBehaviorProvider<AttributeType> defaultBehavior) {
071          return new AttributeTypePropertyDefinition(d, propertyName,
072              options, adminAction, defaultBehavior);
073        }
074      }
075    
076      // Flag indicating whether or not attribute type names should be
077      // validated against the schema.
078      private static boolean isCheckSchema = true;
079    
080    
081    
082      /**
083       * Create a attribute type property definition builder.
084       *
085       * @param d
086       *          The managed object definition associated with this
087       *          property definition.
088       * @param propertyName
089       *          The property name.
090       * @return Returns the new attribute type property definition
091       *         builder.
092       */
093      public static Builder createBuilder(
094          AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
095        return new Builder(d, propertyName);
096      }
097    
098    
099    
100      /**
101       * Determines whether or not attribute type names should be
102       * validated against the schema.
103       *
104       * @return Returns <code>true</code> if attribute type names
105       *         should be validated against the schema.
106       */
107      public static boolean isCheckSchema() {
108        return isCheckSchema;
109      }
110    
111    
112    
113      /**
114       * Specify whether or not attribute type names should be validated
115       * against the schema.
116       * <p>
117       * By default validation is switched on.
118       *
119       * @param value
120       *          <code>true</code> if attribute type names should be
121       *          validated against the schema.
122       */
123      public static void setCheckSchema(boolean value) {
124        isCheckSchema = value;
125      }
126    
127    
128    
129      // Private constructor.
130      private AttributeTypePropertyDefinition(
131          AbstractManagedObjectDefinition<?, ?> d, String propertyName,
132          EnumSet<PropertyOption> options,
133          AdministratorAction adminAction,
134          DefaultBehaviorProvider<AttributeType> defaultBehavior) {
135        super(d, AttributeType.class, propertyName, options,
136            adminAction, defaultBehavior);
137      }
138    
139    
140    
141      /**
142       * {@inheritDoc}
143       */
144      @Override
145      public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
146        return v.visitAttributeType(this, p);
147      }
148    
149    
150    
151      /**
152       * {@inheritDoc}
153       */
154      @Override
155      public <R, P> R accept(PropertyValueVisitor<R, P> v,
156          AttributeType value, P p) {
157        return v.visitAttributeType(this, value, p);
158      }
159    
160    
161    
162      /**
163       * {@inheritDoc}
164       */
165      @Override
166      public int compare(AttributeType o1, AttributeType o2) {
167        return o1.getNameOrOID().compareToIgnoreCase(o2.getNameOrOID());
168      }
169    
170    
171    
172      /**
173       * {@inheritDoc}
174       */
175      @Override
176      public AttributeType decodeValue(String value)
177          throws IllegalPropertyValueStringException {
178        ensureNotNull(value);
179    
180        String name = value.trim().toLowerCase();
181        AttributeType type = DirectoryServer.getAttributeType(name,
182            !isCheckSchema);
183    
184        if (type == null) {
185          throw new IllegalPropertyValueStringException(this, value);
186        } else {
187          try {
188            validateValue(type);
189            return type;
190          } catch (IllegalPropertyValueException e) {
191            throw new IllegalPropertyValueStringException(this, value);
192          }
193        }
194      }
195    
196    
197    
198      /**
199       * {@inheritDoc}
200       */
201      @Override
202      public String encodeValue(AttributeType value)
203          throws IllegalPropertyValueException {
204        return value.getNameOrOID();
205      }
206    
207    
208    
209      /**
210       * {@inheritDoc}
211       */
212      @Override
213      public void validateValue(AttributeType value)
214          throws IllegalPropertyValueException {
215        ensureNotNull(value);
216    
217        // No implementation required.
218      }
219    }