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 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS
026 *      Portions Copyright 2012 Manuel Gaupp
027 */
028package org.opends.server.schema;
029
030
031import static org.opends.server.schema.SchemaConstants.*;
032
033import java.util.List;
034
035import org.forgerock.i18n.LocalizableMessage;
036import org.forgerock.opendj.config.server.ConfigChangeResult;
037import org.forgerock.opendj.config.server.ConfigException;
038import org.forgerock.opendj.ldap.schema.Schema;
039import org.forgerock.opendj.ldap.schema.SchemaOptions;
040import org.forgerock.opendj.ldap.schema.Syntax;
041import org.forgerock.util.Option;
042import org.opends.server.admin.server.ConfigurationChangeListener;
043import org.opends.server.admin.std.server.CountryStringAttributeSyntaxCfg;
044import org.opends.server.api.AttributeSyntax;
045import org.opends.server.core.ServerContext;
046
047/**
048 * This class defines the country string attribute syntax, which should be a
049 * two-character ISO 3166 country code.  However, for maintainability, it will
050 * accept any value consisting entirely of two printable characters.  In most
051 * ways, it will behave like the directory string attribute syntax.
052 */
053public class CountryStringSyntax
054       extends AttributeSyntax<CountryStringAttributeSyntaxCfg>
055       implements ConfigurationChangeListener<CountryStringAttributeSyntaxCfg>
056{
057
058  /** The current configuration. */
059  private volatile CountryStringAttributeSyntaxCfg config;
060
061  private ServerContext serverContext;
062
063  /**
064   * Creates a new instance of this syntax.  Note that the only thing that
065   * should be done here is to invoke the default constructor for the
066   * superclass.  All initialization should be performed in the
067   * <CODE>initializeSyntax</CODE> method.
068   */
069  public CountryStringSyntax()
070  {
071    super();
072  }
073
074  /** {@inheritDoc} */
075  @Override
076  public void initializeSyntax(CountryStringAttributeSyntaxCfg configuration, ServerContext serverContext)
077         throws ConfigException
078  {
079    this.config = configuration;
080    this.serverContext = serverContext;
081    updateNewSchema();
082    config.addCountryStringChangeListener(this);
083  }
084
085  /** Update the option in new schema if it changes from current value. */
086  private void updateNewSchema()
087  {
088    Option<Boolean> option = SchemaOptions.STRICT_FORMAT_FOR_COUNTRY_STRINGS;
089    if (config.isStrictFormat() != serverContext.getSchemaNG().getOption(option))
090    {
091      SchemaUpdater schemaUpdater = serverContext.getSchemaUpdater();
092      schemaUpdater.updateSchema(
093          schemaUpdater.getSchemaBuilder().setOption(option, config.isStrictFormat()).toSchema());
094    }
095  }
096
097  /** {@inheritDoc} */
098  @Override
099  public Syntax getSDKSyntax(Schema schema)
100  {
101    return schema.getSyntax(SchemaConstants.SYNTAX_COUNTRY_STRING_OID);
102  }
103
104  /** {@inheritDoc} */
105  @Override
106  public boolean isConfigurationChangeAcceptable(
107      CountryStringAttributeSyntaxCfg configuration,
108      List<LocalizableMessage> unacceptableReasons)
109  {
110    // The configuration is always acceptable.
111    return true;
112  }
113
114  /** {@inheritDoc} */
115  @Override
116  public ConfigChangeResult applyConfigurationChange(
117      CountryStringAttributeSyntaxCfg configuration)
118  {
119    this.config = configuration;
120    updateNewSchema();
121    return new ConfigChangeResult();
122  }
123
124
125
126
127  /**
128   * Retrieves the common name for this attribute syntax.
129   *
130   * @return  The common name for this attribute syntax.
131   */
132  @Override
133  public String getName()
134  {
135    return SYNTAX_COUNTRY_STRING_NAME;
136  }
137
138  /**
139   * Retrieves the OID for this attribute syntax.
140   *
141   * @return  The OID for this attribute syntax.
142   */
143  @Override
144  public String getOID()
145  {
146    return SYNTAX_COUNTRY_STRING_OID;
147  }
148
149  /**
150   * Retrieves a description for this attribute syntax.
151   *
152   * @return  A description for this attribute syntax.
153   */
154  @Override
155  public String getDescription()
156  {
157    return SYNTAX_COUNTRY_STRING_DESCRIPTION;
158  }
159}
160