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 2006-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2016 ForgeRock AS.
016 */
017package org.opends.server.schema;
018
019import static org.forgerock.opendj.ldap.schema.SchemaOptions.*;
020import static org.opends.server.schema.SchemaConstants.*;
021
022import java.util.List;
023
024import org.forgerock.i18n.LocalizableMessage;
025import org.forgerock.opendj.config.server.ConfigChangeResult;
026import org.forgerock.opendj.config.server.ConfigException;
027import org.forgerock.opendj.ldap.schema.Schema;
028import org.forgerock.opendj.ldap.schema.Syntax;
029import org.forgerock.opendj.config.server.ConfigurationChangeListener;
030import org.forgerock.opendj.server.config.server.AttributeTypeDescriptionAttributeSyntaxCfg;
031import org.opends.server.api.AttributeSyntax;
032import org.opends.server.core.ServerContext;
033import org.opends.server.types.DirectoryException;
034import org.opends.server.types.InitializationException;
035import org.opends.server.util.RemoveOnceSDKSchemaIsUsed;
036
037/**
038 * This class defines the attribute type description syntax, which is used to
039 * hold attribute type definitions in the server schema.  The format of this
040 * syntax is defined in RFC 2252.
041 */
042@RemoveOnceSDKSchemaIsUsed
043public class AttributeTypeSyntax
044       extends AttributeSyntax<AttributeTypeDescriptionAttributeSyntaxCfg>
045       implements
046       ConfigurationChangeListener<AttributeTypeDescriptionAttributeSyntaxCfg> {
047
048  /**
049   * The reference to the configuration for this attribute type description
050   * syntax.
051   */
052  private AttributeTypeDescriptionAttributeSyntaxCfg currentConfig;
053
054
055
056  /** If true strip the suggested minimum upper bound from the syntax OID. */
057  private static boolean stripMinimumUpperBound;
058
059  private ServerContext serverContext;
060
061
062  /**
063   * Creates a new instance of this syntax.  Note that the only thing that
064   * should be done here is to invoke the default constructor for the
065   * superclass.  All initialization should be performed in the
066   * <CODE>initializeSyntax</CODE> method.
067   */
068  public AttributeTypeSyntax()
069  {
070    super();
071  }
072
073  @Override
074  public void
075  initializeSyntax(AttributeTypeDescriptionAttributeSyntaxCfg configuration, ServerContext serverContext)
076      throws ConfigException, InitializationException, DirectoryException
077  {
078    this.serverContext = serverContext;
079
080    // This syntax is one of the Directory Server's core syntaxes and therefore
081    // it may be instantiated at times without a configuration entry.  If that
082    // is the case, then we'll exit now before doing anything that could require
083    // access to that entry.
084    if (configuration == null)
085    {
086      return;
087    }
088
089    currentConfig = configuration;
090    currentConfig.addAttributeTypeDescriptionChangeListener(this);
091    stripMinimumUpperBound = configuration.isStripSyntaxMinUpperBound();
092    serverContext.getSchema().updateSchemaOption(STRIP_UPPER_BOUND_FOR_ATTRIBUTE_TYPE, stripMinimumUpperBound);
093  }
094
095  @Override
096  public Syntax getSDKSyntax(Schema schema)
097  {
098    return schema.getSyntax(SchemaConstants.SYNTAX_ATTRIBUTE_TYPE_OID);
099  }
100
101  @Override
102  public String getName()
103  {
104    return SYNTAX_ATTRIBUTE_TYPE_NAME;
105  }
106
107  @Override
108  public String getOID()
109  {
110    return SYNTAX_ATTRIBUTE_TYPE_OID;
111  }
112
113  @Override
114  public String getDescription()
115  {
116    return SYNTAX_ATTRIBUTE_TYPE_DESCRIPTION;
117  }
118
119  @Override
120  public ConfigChangeResult applyConfigurationChange(
121              AttributeTypeDescriptionAttributeSyntaxCfg configuration)
122  {
123    ConfigChangeResult ccr = new ConfigChangeResult();
124    currentConfig = configuration;
125    stripMinimumUpperBound = configuration.isStripSyntaxMinUpperBound();
126    try
127    {
128      serverContext.getSchema().updateSchemaOption(STRIP_UPPER_BOUND_FOR_ATTRIBUTE_TYPE, stripMinimumUpperBound);
129    }
130    catch (DirectoryException e)
131    {
132      ccr.setResultCode(e.getResultCode());
133      ccr.addMessage(e.getMessageObject());
134    }
135    return ccr;
136  }
137
138  @Override
139  public boolean isConfigurationChangeAcceptable(
140                      AttributeTypeDescriptionAttributeSyntaxCfg configuration,
141                      List<LocalizableMessage> unacceptableReasons)
142  {
143    // The configuration will always be acceptable.
144    return true;
145  }
146
147  /**
148   * Boolean that indicates that the minimum upper bound value should be
149   * stripped from the Attribute Type Syntax Description.
150   *
151   * @return True if the minimum upper bound value should be stripped.
152   */
153  public static boolean isStripSyntaxMinimumUpperBound() {
154    return stripMinimumUpperBound;
155  }
156}