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