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-2016 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.task;
019
020import java.util.Collection;
021import java.util.HashSet;
022import java.util.Set;
023import java.util.TreeSet;
024
025import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
026import org.opends.guitools.controlpanel.datamodel.BaseDNDescriptor;
027import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
028import org.opends.guitools.controlpanel.ui.ProgressDialog;
029import org.forgerock.opendj.ldap.DN;
030
031/**
032 * Abstract task used to factorize some code shared by different tasks involving
033 * indexes.
034 *
035 */
036public abstract class IndexTask extends Task
037{
038  /**
039   * The set of backends that are affected by this task.
040   */
041  protected Set<String> backendSet;
042  /**
043   * The set of base DNs that are affected by this task.
044   */
045  protected Set<String> baseDNs;
046
047  /**
048   * Constructor of the task.
049   * @param info the control panel information.
050   * @param dlg the progress dialog where the task progress will be displayed.
051   * @param baseDN the base DN where the indexes are defined.
052   */
053  protected IndexTask(ControlPanelInfo info, ProgressDialog dlg,
054      String baseDN)
055  {
056    super(info, dlg);
057    baseDNs = new HashSet<>();
058    baseDNs.add(baseDN);
059    initializeBackendSet();
060  }
061
062  /**
063   * Constructor of the task.
064   * @param info the control panel information.
065   * @param dlg the progress dialog where the task progress will be displayed.
066   * @param baseDNs the list of base DNs where the indexes are defined.
067   */
068  protected IndexTask(ControlPanelInfo info, ProgressDialog dlg,
069      Collection<String> baseDNs)
070  {
071    super(info, dlg);
072    backendSet = new HashSet<>();
073    this.baseDNs = new TreeSet<>();
074    this.baseDNs.addAll(baseDNs);
075    initializeBackendSet();
076  }
077
078  /** Initialize the list of backends that are affected by this task. */
079  private void initializeBackendSet()
080  {
081    backendSet = new TreeSet<>();
082    DN theDN = null;
083    for (String baseDN : baseDNs)
084    {
085      try
086      {
087        theDN = DN.valueOf(baseDN);
088      }
089      catch (Throwable t)
090      {
091        throw new IllegalArgumentException("Could not decode dn " + baseDN, t);
092      }
093      BackendDescriptor backend = findBackendByID(theDN);
094      if (backend != null) {
095        backendSet.add(backend.getBackendID());
096      }
097    }
098  }
099
100  private BackendDescriptor findBackendByID(DN dn)
101  {
102    for (BackendDescriptor backend : getInfo().getServerDescriptor().getBackends())
103    {
104      for (BaseDNDescriptor b : backend.getBaseDns())
105      {
106        if (b.getDn().equals(dn))
107        {
108          return backend;
109        }
110      }
111    }
112    return null;
113  }
114
115  /** {@inheritDoc} */
116  public Set<String> getBackends()
117  {
118    return backendSet;
119  }
120}