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 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027package org.opends.server.extensions;
028import org.forgerock.i18n.LocalizableMessage;
029
030
031
032
033import org.forgerock.i18n.LocalizedIllegalArgumentException;
034
035import java.util.Iterator;
036import java.util.Set;
037
038import org.forgerock.opendj.ldap.DN.CompactDn;
039import org.opends.server.types.DirectoryConfig;
040import org.opends.server.types.DirectoryException;
041import org.opends.server.types.DN;
042import org.opends.server.types.Entry;
043import org.opends.server.types.MemberList;
044import org.opends.server.types.MembershipException;
045import org.forgerock.i18n.slf4j.LocalizedLogger;
046
047import static org.opends.messages.ExtensionMessages.*;
048import static org.opends.server.extensions.StaticGroup.*;
049import static org.forgerock.util.Reject.*;
050
051
052
053/**
054 * This class provides an implementation of the {@code MemberList} class that
055 * may be used in conjunction when static groups when no additional criteria is
056 * to be used to select a subset of the group members.
057 */
058public class SimpleStaticGroupMemberList extends MemberList
059{
060  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
061
062  /** The DN of the static group with which this member list is associated. */
063  private DN groupDN;
064
065  /** The iterator used to traverse the set of member DNs. */
066  private Iterator<CompactDn> memberDNIterator;
067
068  /**
069   * Creates a new simple static group member list with the provided set of
070   * member DNs.
071   *
072   * @param  groupDN    The DN of the static group with which this member list
073   *                    is associated.
074   * @param  memberDNs  The set of DNs for the users that are members of the
075   *                    associated static group.
076   */
077  public SimpleStaticGroupMemberList(DN groupDN, Set<CompactDn> memberDNs)
078  {
079    ifNull(groupDN, memberDNs);
080    this.groupDN   = groupDN;
081    this.memberDNIterator = memberDNs.iterator();
082  }
083
084  /** {@inheritDoc} */
085  @Override
086  public boolean hasMoreMembers()
087  {
088    return memberDNIterator.hasNext();
089  }
090
091  /** {@inheritDoc} */
092  @Override
093  public DN nextMemberDN()
094         throws MembershipException
095  {
096    DN dn = null;
097    if (memberDNIterator.hasNext())
098    {
099      try
100      {
101        dn = fromCompactDn(memberDNIterator.next());
102      }
103      catch (LocalizedIllegalArgumentException e)
104      {
105        // Should not happen
106        logger.traceException(e);
107        throw new MembershipException(ERR_STATICMEMBERS_CANNOT_DECODE_DN.get(dn, groupDN, e.getMessageObject()),
108            true, e);
109      }
110    }
111
112    return dn;
113  }
114
115  /** {@inheritDoc} */
116  @Override
117  public Entry nextMemberEntry() throws MembershipException
118  {
119    if (memberDNIterator.hasNext())
120    {
121      CompactDn memberDN = memberDNIterator.next();
122
123      try
124      {
125        Entry memberEntry = DirectoryConfig.getEntry(fromCompactDn(memberDN));
126        if (memberEntry == null)
127        {
128          LocalizableMessage message = ERR_STATICMEMBERS_NO_SUCH_ENTRY.get(memberDN, groupDN);
129          throw new MembershipException(message, true);
130        }
131
132        return memberEntry;
133      }
134      catch (DirectoryException de)
135      {
136        logger.traceException(de);
137        throw new MembershipException(ERR_STATICMEMBERS_CANNOT_GET_ENTRY.get(memberDN, groupDN, de.getMessageObject()),
138            true, de);
139      }
140    }
141
142    return null;
143  }
144
145  /** {@inheritDoc} */
146  @Override
147  public void close()
148  {
149    // No implementation is required.
150  }
151}
152