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-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2012-2016 ForgeRock AS.
016 * Portions Copyright 2012 Manuel Gaupp
017 */
018package org.opends.server.schema;
019
020import static org.forgerock.opendj.ldap.schema.SchemaOptions.*;
021import static org.opends.server.schema.SchemaConstants.*;
022
023import java.util.List;
024
025import org.forgerock.i18n.LocalizableMessage;
026import org.forgerock.opendj.config.server.ConfigChangeResult;
027import org.forgerock.opendj.config.server.ConfigException;
028import org.forgerock.opendj.ldap.schema.Schema;
029import org.forgerock.opendj.ldap.schema.Syntax;
030import org.forgerock.opendj.config.server.ConfigurationChangeListener;
031import org.forgerock.opendj.server.config.server.CountryStringAttributeSyntaxCfg;
032import org.opends.server.api.AttributeSyntax;
033import org.opends.server.core.ServerContext;
034import org.opends.server.types.DirectoryException;
035
036/**
037 * This class defines the country string attribute syntax, which should be a
038 * two-character ISO 3166 country code.  However, for maintainability, it will
039 * accept any value consisting entirely of two printable characters.  In most
040 * ways, it will behave like the directory string attribute syntax.
041 */
042public class CountryStringSyntax
043       extends AttributeSyntax<CountryStringAttributeSyntaxCfg>
044       implements ConfigurationChangeListener<CountryStringAttributeSyntaxCfg>
045{
046
047  /** The current configuration. */
048  private volatile CountryStringAttributeSyntaxCfg config;
049
050  private ServerContext serverContext;
051
052  /**
053   * Creates a new instance of this syntax.  Note that the only thing that
054   * should be done here is to invoke the default constructor for the
055   * superclass.  All initialization should be performed in the
056   * <CODE>initializeSyntax</CODE> method.
057   */
058  public CountryStringSyntax()
059  {
060    super();
061  }
062
063  @Override
064  public void initializeSyntax(CountryStringAttributeSyntaxCfg configuration, ServerContext serverContext)
065      throws ConfigException, DirectoryException
066  {
067    this.config = configuration;
068    this.serverContext = serverContext;
069    serverContext.getSchema().updateSchemaOption(STRICT_FORMAT_FOR_COUNTRY_STRINGS, config.isStrictFormat());
070    config.addCountryStringChangeListener(this);
071  }
072
073  @Override
074  public Syntax getSDKSyntax(Schema schema)
075  {
076    return schema.getSyntax(SchemaConstants.SYNTAX_COUNTRY_STRING_OID);
077  }
078
079  @Override
080  public boolean isConfigurationChangeAcceptable(
081      CountryStringAttributeSyntaxCfg configuration,
082      List<LocalizableMessage> unacceptableReasons)
083  {
084    // The configuration is always acceptable.
085    return true;
086  }
087
088  @Override
089  public ConfigChangeResult applyConfigurationChange(CountryStringAttributeSyntaxCfg configuration)
090  {
091    this.config = configuration;
092
093    final ConfigChangeResult ccr = new ConfigChangeResult();
094    try
095    {
096      serverContext.getSchema().updateSchemaOption(STRICT_FORMAT_FOR_COUNTRY_STRINGS, config.isStrictFormat());
097    }
098    catch (DirectoryException e)
099    {
100      ccr.setResultCode(e.getResultCode());
101      ccr.addMessage(e.getMessageObject());
102    }
103    return ccr;
104  }
105
106  @Override
107  public String getName()
108  {
109    return SYNTAX_COUNTRY_STRING_NAME;
110  }
111
112  @Override
113  public String getOID()
114  {
115    return SYNTAX_COUNTRY_STRING_OID;
116  }
117
118  @Override
119  public String getDescription()
120  {
121    return SYNTAX_COUNTRY_STRING_DESCRIPTION;
122  }
123}
124