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 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.core;
018import java.util.List;
019
020import org.forgerock.i18n.LocalizableMessage;
021import org.forgerock.opendj.config.ClassPropertyDefinition;
022import org.forgerock.opendj.config.server.ConfigurationChangeListener;
023import org.forgerock.opendj.server.config.meta.WorkQueueCfgDefn;
024import org.forgerock.opendj.server.config.server.WorkQueueCfg;
025import org.opends.server.api.WorkQueue;
026import org.forgerock.opendj.config.server.ConfigException;
027import org.forgerock.opendj.config.server.ConfigChangeResult;
028import org.opends.server.types.InitializationException;
029
030import static org.opends.messages.ConfigMessages.*;
031import static org.opends.server.util.StaticUtils.*;
032
033/** This class defines a utility that will be used to manage the Directory Server work queue. */
034public class WorkQueueConfigManager
035       implements ConfigurationChangeListener<WorkQueueCfg>
036{
037  private final ServerContext serverContext;
038
039  /**
040   * Creates a new instance of this work queue config manager.
041   *
042   * @param serverContext
043   *            The server context.
044   */
045  public WorkQueueConfigManager(ServerContext serverContext)
046  {
047    this.serverContext = serverContext;
048  }
049
050  /**
051   * Initializes the Directory Server's work queue.  This should only be called
052   * at server startup.
053   *
054   * @return  WorkQueue  The initialized work queue that should be used by the
055   *                     server.
056   *
057   * @throws  ConfigException  If a configuration problem causes the work queue
058   *                           initialization process to fail.
059   *
060   * @throws  InitializationException  If a problem occurs while initializing
061   *                                   the work queue that is not related to the
062   *                                   server configuration.
063   */
064  public WorkQueue initializeWorkQueue()
065         throws ConfigException, InitializationException
066  {
067    // Get the work queue configuration and register with it as a change listener.
068    WorkQueueCfg workQueueConfig = serverContext.getRootConfig().getWorkQueue();
069    workQueueConfig.addChangeListener(this);
070
071    // Get the work queue class, and load and instantiate an instance of it
072    // using that configuration.
073    WorkQueueCfgDefn definition = WorkQueueCfgDefn.getInstance();
074    ClassPropertyDefinition propertyDefinition =
075         definition.getJavaClassPropertyDefinition();
076    Class<? extends WorkQueue> workQueueClass =
077         propertyDefinition.loadClass(workQueueConfig.getJavaClass(),
078                                      WorkQueue.class);
079
080    try
081    {
082      WorkQueue workQueue = workQueueClass.newInstance();
083
084      workQueue.initializeWorkQueue(workQueueConfig);
085
086      return workQueue;
087    }
088    catch (Exception e)
089    {
090      LocalizableMessage message = ERR_CONFIG_WORK_QUEUE_INITIALIZATION_FAILED.
091          get(workQueueConfig.getJavaClass(), workQueueConfig.dn(), stackTraceToSingleLineString(e));
092      throw new InitializationException(message, e);
093    }
094  }
095
096  @Override
097  public boolean isConfigurationChangeAcceptable(WorkQueueCfg configuration,
098                      List<LocalizableMessage> unacceptableReasons)
099  {
100    // Changes to the work queue configuration will always be acceptable to this
101    // generic implementation.
102    return true;
103  }
104
105  @Override
106  public ConfigChangeResult applyConfigurationChange(WorkQueueCfg configuration)
107  {
108    final ConfigChangeResult ccr = new ConfigChangeResult();
109
110    // If the work queue class has been changed, then we should warn the user
111    // that it won't take effect until the server is restarted.
112    WorkQueue workQueue = DirectoryServer.getWorkQueue();
113    String workQueueClass = configuration.getJavaClass();
114    if (! workQueueClass.equals(workQueue.getClass().getName()))
115    {
116      ccr.addMessage(INFO_CONFIG_WORK_QUEUE_CLASS_CHANGE_REQUIRES_RESTART.get(
117              workQueue.getClass().getName(), workQueueClass));
118      ccr.setAdminActionRequired(true);
119    }
120    return ccr;
121  }
122}