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 */
026
027package org.forgerock.opendj.config;
028
029import org.forgerock.util.Reject;
030
031import java.util.EnumSet;
032
033import org.forgerock.opendj.ldap.schema.AttributeType;
034import org.forgerock.opendj.ldap.schema.Schema;
035
036/**
037 * Attribute type property definition.
038 */
039public final class AttributeTypePropertyDefinition extends PropertyDefinition<AttributeType> {
040
041    /**
042     * An interface for incrementally constructing attribute type property
043     * definitions.
044     */
045    public static final class Builder extends AbstractBuilder<AttributeType, AttributeTypePropertyDefinition> {
046
047        /** Private constructor. */
048        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
049            super(d, propertyName);
050        }
051
052        /** {@inheritDoc} */
053        @Override
054        protected AttributeTypePropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
055            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
056            DefaultBehaviorProvider<AttributeType> defaultBehavior) {
057            return new AttributeTypePropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
058        }
059    }
060
061    /**
062     * Create a attribute type property definition builder.
063     *
064     * @param d
065     *            The managed object definition associated with this property
066     *            definition.
067     * @param propertyName
068     *            The property name.
069     * @return Returns the new attribute type property definition builder.
070     */
071    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
072        return new Builder(d, propertyName);
073    }
074
075    /** Private constructor. */
076    private AttributeTypePropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
077        EnumSet<PropertyOption> options, AdministratorAction adminAction,
078        DefaultBehaviorProvider<AttributeType> defaultBehavior) {
079        super(d, AttributeType.class, propertyName, options, adminAction, defaultBehavior);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
085        return v.visitAttributeType(this, p);
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public <R, P> R accept(PropertyValueVisitor<R, P> v, AttributeType value, P p) {
091        return v.visitAttributeType(this, value, p);
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public int compare(AttributeType o1, AttributeType o2) {
097        return o1.getNameOrOID().compareToIgnoreCase(o2.getNameOrOID());
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public AttributeType decodeValue(String value) {
103        Reject.ifNull(value);
104
105        final String name = value.trim();
106        if (!ConfigurationFramework.getInstance().isClient()
107                && !Schema.getDefaultSchema().hasAttributeType(name)) {
108            // If this is the server then the attribute type must be defined.
109            throw PropertyException.illegalPropertyValueException(this, value);
110        }
111        final AttributeType type =
112                Schema.getDefaultSchema().asNonStrictSchema().getAttributeType(name);
113        try {
114            validateValue(type);
115            return type;
116        } catch (PropertyException e) {
117            throw PropertyException.illegalPropertyValueException(this, value);
118        }
119    }
120
121    /** {@inheritDoc} */
122    @Override
123    public String encodeValue(AttributeType value) {
124        return value.getNameOrOID();
125    }
126
127    /** {@inheritDoc} */
128    @Override
129    public void validateValue(AttributeType value) {
130        Reject.ifNull(value);
131
132        // No implementation required.
133    }
134}