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