001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.guitools.controlpanel.util;
028
029import java.util.ArrayList;
030import java.util.Collection;
031
032import org.opends.server.types.DN;
033import org.opends.server.types.LDAPURL;
034import org.opends.server.types.OpenDsException;
035
036/** Class used to handle the case where numsubordinates does not work between databases. */
037public class NumSubordinateHacker {
038  private String serverHost = "not-initialized";
039  private int serverPort = -1;
040  private final ArrayList<DN> unreliableEntryList = new ArrayList<>();
041  private boolean isUnreliableEntryListEmpty;
042
043  /**
044    * Tells whether the list of unreliable contains children of
045    * the entry with LDAPUrl parentUrl.
046    * @param parentUrl the LDAPURL of the parent.
047    * @return <CODE>true</CODE> if the list of unreliable entries contains a
048    * children of the parentUrl.  Returns <CODE>false</CODE> otherwise.
049    */
050  public boolean containsChildrenOf(LDAPURL parentUrl)
051  {
052    return contains(parentUrl);
053  }
054
055  /**
056    * Tells whether the list of unreliable contains the entry with LDAPURL url.
057    * It assumes that we previously called containsChildrenOf (there's no check
058    * of the host/port).
059    * @param url the LDAPURL of the parent.
060    * @return <CODE>true</CODE> if the url correspond to an unreliable
061    * entry and <CODE>false</CODE> otherwise.
062    */
063  public boolean contains(LDAPURL url) {
064    if (!isUnreliableEntryListEmpty) {
065      boolean isInServer = serverHost.equalsIgnoreCase(url.getHost()) && serverPort == url.getPort();
066      if (isInServer) {
067        try
068        {
069          return unreliableEntryList.contains(DN.valueOf(url.getRawBaseDN()));
070        }
071        catch (OpenDsException oe)
072        {
073          throw new RuntimeException("Error decoding DN of url: " + url);
074        }
075      }
076    }
077    return false;
078  }
079
080  /**
081   * This method construct a list with the entries the entries that are parents
082   * of the suffix entries.  This list is needed to overpass the fact that
083   * numsubordinates does not work between databases.
084   * @param allSuffixes a collection with all the suffixes.
085   * @param rootSuffixes a collection with the root suffixes.
086   * @param serverHost the name of the host where the server is installed.
087   * @param serverPort the LDAP(s) port of the server.
088   */
089  public void update(Collection<DN> allSuffixes,
090      Collection<DN> rootSuffixes,
091      String serverHost,
092      int serverPort)
093  {
094    allSuffixes.removeAll(rootSuffixes);
095    Collection<DN> subSuffixes = allSuffixes;
096    synchronized (unreliableEntryList) {
097      unreliableEntryList.clear();
098
099      for (DN subSuffixDN : subSuffixes) {
100        unreliableEntryList.add(subSuffixDN.parent());
101      }
102      isUnreliableEntryListEmpty = unreliableEntryList.isEmpty();
103    }
104    this.serverHost = serverHost;
105    this.serverPort = serverPort;
106  }
107}