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