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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027package org.opends.server.replication.plugin;
028
029import java.util.List;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.forgerock.opendj.ldap.ResultCode;
033import org.opends.server.admin.server.ConfigurationAddListener;
034import org.opends.server.admin.server.ConfigurationChangeListener;
035import org.opends.server.admin.server.ConfigurationDeleteListener;
036import org.opends.server.admin.std.server.ExternalChangelogDomainCfg;
037import org.forgerock.opendj.config.server.ConfigChangeResult;
038import org.opends.server.types.DN;
039import org.opends.server.types.RDN;
040
041/**
042 * This class specifies the external changelog feature for a replication
043 * domain.
044 */
045public class ExternalChangelogDomain
046  implements ConfigurationAddListener<ExternalChangelogDomainCfg>,
047             ConfigurationDeleteListener<ExternalChangelogDomainCfg>,
048             ConfigurationChangeListener<ExternalChangelogDomainCfg>
049{
050
051  private LDAPReplicationDomain domain;
052  private boolean isEnabled;
053
054  /**
055   * Constructor from a provided LDAPReplicationDomain.
056   * @param domain The provided domain.
057   * @param configuration The external changelog configuration.
058   */
059  public ExternalChangelogDomain(LDAPReplicationDomain domain,
060      ExternalChangelogDomainCfg configuration)
061  {
062    this.domain = domain;
063    this.isEnabled = configuration.isEnabled();
064    configuration.addChangeListener(this);
065    domain.setEclIncludes(domain.getServerId(),
066        configuration.getECLInclude(),
067        configuration.getECLIncludeForDeletes());
068  }
069
070
071  /** {@inheritDoc} */
072  @Override
073  public ConfigChangeResult applyConfigurationAdd(
074      ExternalChangelogDomainCfg configuration)
075  {
076    final ConfigChangeResult ccr = setDomain(configuration);
077    if (ccr != null)
078    {
079      return ccr;
080    }
081
082    this.isEnabled = configuration.isEnabled();
083    domain.setEclIncludes(domain.getServerId(),
084        configuration.getECLInclude(),
085        configuration.getECLIncludeForDeletes());
086    return new ConfigChangeResult();
087  }
088
089
090  /** {@inheritDoc} */
091  @Override
092  public ConfigChangeResult applyConfigurationChange(
093      ExternalChangelogDomainCfg configuration)
094  {
095    // How it works with dsconfig :
096    // - after dsconfig set-external-changelog-domain-prop --set ecl-include:xx
097    //   configuration contains only attribute xx
098    // - after dsconfig set-external-changelog-domain-prop --add ecl-include:xx
099    //   configuration contains attribute xx and the previous list
100    // Hence in all cases, it is the complete list of attributes.
101    final ConfigChangeResult ccr = setDomain(configuration);
102    if (ccr != null)
103    {
104      return ccr;
105    }
106
107    this.isEnabled = configuration.isEnabled();
108    domain.changeConfig(configuration.getECLInclude(),
109        configuration.getECLIncludeForDeletes());
110    return new ConfigChangeResult();
111  }
112
113  private ConfigChangeResult setDomain(ExternalChangelogDomainCfg configuration)
114  {
115    try
116    {
117      if (domain==null)
118      {
119        RDN rdn = configuration.dn().parent().rdn();
120        DN rdns = DN.decode(rdn.getAttributeValue(0));
121        domain = MultimasterReplication.findDomain(rdns, null);
122      }
123      return null;
124    }
125    catch (Exception e)
126    {
127      final ConfigChangeResult ccr = new ConfigChangeResult();
128      ccr.setResultCode(ResultCode.CONSTRAINT_VIOLATION);
129      return ccr;
130    }
131  }
132
133
134  /** {@inheritDoc} */
135  @Override
136  public boolean isConfigurationAddAcceptable(
137      ExternalChangelogDomainCfg configuration,
138      List<LocalizableMessage> unacceptableReasons)
139  {
140    return true;
141  }
142  /** {@inheritDoc} */
143  @Override
144  public boolean isConfigurationChangeAcceptable(
145      ExternalChangelogDomainCfg configuration,
146      List<LocalizableMessage> unacceptableReasons)
147  {
148    return true;
149  }
150
151
152  /** {@inheritDoc} */
153  @Override
154  public boolean isConfigurationDeleteAcceptable(
155      ExternalChangelogDomainCfg configuration,
156      List<LocalizableMessage> unacceptableReasons)
157  {
158    return true;
159  }
160
161
162  /** {@inheritDoc} */
163  @Override
164  public ConfigChangeResult applyConfigurationDelete(
165      ExternalChangelogDomainCfg configuration)
166  {
167    return new ConfigChangeResult();
168  }
169
170  /**
171   * Specifies whether this domain is enabled/disabled regarding the ECL.
172   * @return enabled/disabled for the ECL.
173   */
174  boolean isEnabled()
175  {
176    return this.isEnabled;
177  }
178}