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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.extensions;
028 import org.opends.messages.Message;
029
030
031
032 import java.util.Iterator;
033 import java.util.LinkedList;
034 import java.util.Set;
035
036 import org.opends.server.types.DirectoryConfig;
037 import org.opends.server.types.DirectoryException;
038 import org.opends.server.types.DN;
039 import org.opends.server.types.Entry;
040 import org.opends.server.types.MemberList;
041 import org.opends.server.types.MembershipException;
042
043 import static org.opends.server.loggers.debug.DebugLogger.*;
044 import org.opends.server.loggers.debug.DebugTracer;
045 import org.opends.server.types.DebugLogLevel;
046 import static org.opends.messages.ExtensionMessages.*;
047 import static org.opends.server.util.Validator.*;
048
049
050
051 /**
052 * This class provides an implementation of the {@code MemberList} class that
053 * may be used in conjunction when static groups when no additional criteria is
054 * to be used to select a subset of the group members.
055 */
056 public class SimpleStaticGroupMemberList
057 extends MemberList
058 {
059 /**
060 * The tracer object for the debug logger.
061 */
062 private static final DebugTracer TRACER = getTracer();
063
064
065
066
067 // The DN of the static group with which this member list is associated.
068 private DN groupDN;
069
070 // The iterator used to traverse the set of member DNs.
071 private Iterator<DN> memberDNIterator;
072
073 // The set of DNs for the users that are members of the associated static
074 // group.
075 private LinkedList<DN> memberDNs;
076
077
078
079 /**
080 * Creates a new simple static group member list with the provided set of
081 * member DNs.
082 *
083 * @param groupDN The DN of the static group with which this member list
084 * is associated.
085 * @param memberDNs The set of DNs for the users that are members of the
086 * associated static group.
087 */
088 public SimpleStaticGroupMemberList(DN groupDN, Set<DN> memberDNs)
089 {
090 ensureNotNull(groupDN, memberDNs);
091
092 this.groupDN = groupDN;
093 this.memberDNs = new LinkedList<DN>(memberDNs);
094 memberDNIterator = memberDNs.iterator();
095 }
096
097
098
099 /**
100 * {@inheritDoc}
101 */
102 @Override()
103 public boolean hasMoreMembers()
104 {
105 return memberDNIterator.hasNext();
106 }
107
108
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override()
114 public DN nextMemberDN()
115 throws MembershipException
116 {
117 if (memberDNIterator.hasNext())
118 {
119 return memberDNIterator.next();
120 }
121
122 return null;
123 }
124
125
126
127 /**
128 * {@inheritDoc}
129 */
130 @Override()
131 public Entry nextMemberEntry()
132 throws MembershipException
133 {
134 if (memberDNIterator.hasNext())
135 {
136 DN memberDN = memberDNIterator.next();
137
138 try
139 {
140 Entry memberEntry = DirectoryConfig.getEntry(memberDN);
141 if (memberEntry == null)
142 {
143 Message message = ERR_STATICMEMBERS_NO_SUCH_ENTRY.get(
144 String.valueOf(memberDN), String.valueOf(groupDN));
145 throw new MembershipException(message, true);
146 }
147
148 return memberEntry;
149 }
150 catch (DirectoryException de)
151 {
152 if (debugEnabled())
153 {
154 TRACER.debugCaught(DebugLogLevel.ERROR, de);
155 }
156
157 Message message = ERR_STATICMEMBERS_CANNOT_GET_ENTRY.
158 get(String.valueOf(memberDN), String.valueOf(groupDN),
159 String.valueOf(de.getMessageObject()));
160 throw new MembershipException(message, true, de);
161 }
162 }
163
164 return null;
165 }
166
167
168
169 /**
170 * {@inheritDoc}
171 */
172 @Override()
173 public void close()
174 {
175 // No implementation is required.
176 }
177 }
178