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