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