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