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