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