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 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-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.config.server.ConfigException;
033import org.forgerock.opendj.ldap.ResultCode;
034import org.opends.server.admin.server.ConfigurationAddListener;
035import org.opends.server.admin.server.ConfigurationDeleteListener;
036import org.opends.server.admin.std.server.ReplicationServerCfg;
037import org.opends.server.admin.std.server.ReplicationSynchronizationProviderCfg;
038import org.opends.server.replication.server.ReplicationServer;
039import org.opends.server.replication.service.DSRSShutdownSync;
040import org.forgerock.opendj.config.server.ConfigChangeResult;
041
042/**
043 * This class is used to create and object that can
044 * register in the admin framework as a listener for changes, add and delete
045 * on the ReplicationServer configuration objects.
046 */
047public class ReplicationServerListener
048       implements ConfigurationAddListener<ReplicationServerCfg>,
049       ConfigurationDeleteListener<ReplicationServerCfg>
050{
051  private final DSRSShutdownSync dsrsShutdownSync;
052  private ReplicationServer replicationServer;
053
054  /**
055   * Build a ReplicationServer Listener from the given Multimaster
056   * configuration.
057   *
058   * @param configuration The configuration that will be used to listen
059   *                      for replicationServer configuration changes.
060   * @param dsrsShutdownSync Synchronization object for shutdown of combined DS/RS instances.
061   * @throws ConfigException if the ReplicationServerListener can't register for
062   *                         listening to changes on the provided configuration
063   *                         object.
064   */
065  public ReplicationServerListener(
066      ReplicationSynchronizationProviderCfg configuration,
067      DSRSShutdownSync dsrsShutdownSync) throws ConfigException
068  {
069    configuration.addReplicationServerAddListener(this);
070    configuration.addReplicationServerDeleteListener(this);
071
072    this.dsrsShutdownSync = dsrsShutdownSync;
073    if (configuration.hasReplicationServer())
074    {
075      final ReplicationServerCfg cfg = configuration.getReplicationServer();
076      replicationServer = new ReplicationServer(cfg, dsrsShutdownSync);
077    }
078  }
079
080  /** {@inheritDoc} */
081  @Override
082  public ConfigChangeResult applyConfigurationAdd(ReplicationServerCfg cfg)
083  {
084    final ConfigChangeResult ccr = new ConfigChangeResult();
085    try
086    {
087      replicationServer = new ReplicationServer(cfg, dsrsShutdownSync);
088    }
089    catch (ConfigException e)
090    {
091      // we should never get to this point because the configEntry has
092      // already been validated in configAddisAcceptable
093      ccr.setResultCode(ResultCode.CONSTRAINT_VIOLATION);
094    }
095    return ccr;
096  }
097
098  /** {@inheritDoc} */
099  @Override
100  public boolean isConfigurationAddAcceptable(
101      ReplicationServerCfg cfg, List<LocalizableMessage> unacceptableReasons)
102  {
103    return ReplicationServer.isConfigurationAcceptable(cfg, unacceptableReasons);
104  }
105
106  /**
107   * Shutdown the replication server.
108   */
109  public void shutdown()
110  {
111    if (replicationServer != null)
112    {
113      replicationServer.shutdown();
114    }
115  }
116
117  /** {@inheritDoc} */
118  @Override
119  public ConfigChangeResult applyConfigurationDelete(ReplicationServerCfg cfg)
120  {
121    // There can be only one replicationServer, just shutdown the
122    // replicationServer currently configured.
123    if (replicationServer != null)
124    {
125      replicationServer.remove();
126    }
127    return new ConfigChangeResult();
128  }
129
130  /** {@inheritDoc} */
131  @Override
132  public boolean isConfigurationDeleteAcceptable(
133      ReplicationServerCfg cfg, List<LocalizableMessage> unacceptableReasons)
134  {
135    return true;
136  }
137
138  /**
139   * Returns the associated Replication Server.
140   * @return The replication server.
141   */
142  public ReplicationServer getReplicationServer()
143  {
144    return replicationServer;
145  }
146}