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-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.util;
018
019import java.util.ArrayList;
020import java.util.Collection;
021
022import org.forgerock.opendj.ldap.DN;
023import org.opends.server.types.LDAPURL;
024
025/** Class used to handle the case where numsubordinates does not work between databases. */
026public class NumSubordinateHacker {
027  private String serverHost = "not-initialized";
028  private int serverPort = -1;
029  private final ArrayList<DN> unreliableEntryList = new ArrayList<>();
030  private boolean isUnreliableEntryListEmpty;
031
032  /**
033    * Tells whether the list of unreliable contains children of
034    * the entry with LDAPUrl parentUrl.
035    * @param parentUrl the LDAPURL of the parent.
036    * @return <CODE>true</CODE> if the list of unreliable entries contains a
037    * children of the parentUrl.  Returns <CODE>false</CODE> otherwise.
038    */
039  public boolean containsChildrenOf(LDAPURL parentUrl)
040  {
041    return contains(parentUrl);
042  }
043
044  /**
045    * Tells whether the list of unreliable contains the entry with LDAPURL url.
046    * It assumes that we previously called containsChildrenOf (there's no check
047    * of the host/port).
048    * @param url the LDAPURL of the parent.
049    * @return <CODE>true</CODE> if the url correspond to an unreliable
050    * entry and <CODE>false</CODE> otherwise.
051    */
052  public boolean contains(LDAPURL url) {
053    if (!isUnreliableEntryListEmpty) {
054      boolean isInServer = serverHost.equalsIgnoreCase(url.getHost()) && serverPort == url.getPort();
055      if (isInServer) {
056        return unreliableEntryList.contains(DN.valueOf(url.getRawBaseDN()));
057      }
058    }
059    return false;
060  }
061
062  /**
063   * This method construct a list with the entries the entries that are parents
064   * of the suffix entries.  This list is needed to overpass the fact that
065   * numsubordinates does not work between databases.
066   * @param allSuffixes a collection with all the suffixes.
067   * @param rootSuffixes a collection with the root suffixes.
068   * @param serverHost the name of the host where the server is installed.
069   * @param serverPort the LDAP(s) port of the server.
070   */
071  public void update(Collection<DN> allSuffixes,
072      Collection<DN> rootSuffixes,
073      String serverHost,
074      int serverPort)
075  {
076    allSuffixes.removeAll(rootSuffixes);
077    Collection<DN> subSuffixes = allSuffixes;
078    synchronized (unreliableEntryList) {
079      unreliableEntryList.clear();
080
081      for (DN subSuffixDN : subSuffixes) {
082        unreliableEntryList.add(subSuffixDN.parent());
083      }
084      isUnreliableEntryListEmpty = unreliableEntryList.isEmpty();
085    }
086    this.serverHost = serverHost;
087    this.serverPort = serverPort;
088  }
089}