001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2006 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.admin;
011:
012: import org.jivesoftware.util.Log;
013: import org.jivesoftware.openfire.ldap.LdapManager;
014:
015: import javax.naming.NamingEnumeration;
016: import javax.naming.directory.Attribute;
017: import javax.naming.directory.Attributes;
018: import javax.naming.directory.SearchControls;
019: import javax.naming.directory.SearchResult;
020: import javax.naming.ldap.Control;
021: import javax.naming.ldap.LdapContext;
022: import javax.naming.ldap.SortControl;
023: import java.text.MessageFormat;
024: import java.util.ArrayList;
025: import java.util.Collection;
026:
027: /**
028: * Class that assists during the testing of the ldap groups.
029: *
030: * @author Gaston Dombiak
031: */
032: public class LdapGroupTester {
033:
034: private LdapManager manager;
035:
036: public LdapGroupTester(LdapManager manager) {
037: this .manager = manager;
038: }
039:
040: /**
041: * Returns fist N groups found in LDAP. The returned groups are only able to return their name,
042: * description and count of members. Count of members is considering all values that were found
043: * in the member field.
044: *
045: * @param maxGroups max number of groups to return.
046: * @return fist N groups found in the LDAP.
047: */
048: public Collection<Group> getGroups(int maxGroups) {
049: Collection<Group> groups = new ArrayList<Group>();
050: LdapContext ctx = null;
051: try {
052: ctx = manager.getContext();
053: // Sort on group name field.
054: Control[] searchControl = new Control[] { new SortControl(
055: new String[] { manager.getGroupNameField() },
056: Control.NONCRITICAL) };
057: ctx.setRequestControls(searchControl);
058:
059: SearchControls searchControls = new SearchControls();
060: // See if recursive searching is enabled. Otherwise, only search one level.
061: if (manager.isSubTreeSearch()) {
062: searchControls
063: .setSearchScope(SearchControls.SUBTREE_SCOPE);
064: } else {
065: searchControls
066: .setSearchScope(SearchControls.ONELEVEL_SCOPE);
067: }
068: // Attributes to return for each group
069: String[] standardAttributes = new String[3];
070: standardAttributes[0] = manager.getGroupNameField();
071: standardAttributes[1] = manager.getGroupDescriptionField();
072: standardAttributes[2] = manager.getGroupMemberField();
073: searchControls.setReturningAttributes(standardAttributes);
074: // Limit results to those we'll need to process
075: searchControls.setCountLimit(maxGroups);
076:
077: String filter = MessageFormat.format(manager
078: .getGroupSearchFilter(), "*");
079: NamingEnumeration answer = ctx.search("", filter,
080: searchControls);
081: while (answer.hasMoreElements()) {
082: // Get the next group.
083: Attributes attributes = ((SearchResult) answer.next())
084: .getAttributes();
085: String groupName = (String) attributes.get(
086: manager.getGroupNameField()).get();
087: String description = "";
088: int elements = 0;
089: try {
090: description = ((String) attributes.get(
091: manager.getGroupDescriptionField()).get());
092: } catch (NullPointerException e) {
093: // Do nothing since the group description field was not found
094: } catch (Exception e) {
095: Log.error("Error retrieving group description", e);
096: }
097:
098: Attribute memberField = attributes.get(manager
099: .getGroupMemberField());
100: if (memberField != null) {
101: NamingEnumeration ne = memberField.getAll();
102: while (ne.hasMore()) {
103: ne.next();
104: elements = elements + 1;
105: }
106: }
107: // Build Group with found information
108: groups.add(new Group(groupName, description, elements));
109: }
110: // Close the enumeration.
111: answer.close();
112: } catch (Exception e) {
113: Log.error(e);
114: } finally {
115: try {
116: if (ctx != null) {
117: ctx.setRequestControls(null);
118: ctx.close();
119: }
120: } catch (Exception ignored) {
121: // Ignore.
122: }
123: }
124: return groups;
125: }
126:
127: /**
128: * Representation of a group found in LDAP. This representatio is read-only and only provides
129: * some basic information: name, description and number of members in the group. Note that
130: * group members are not validated (i.e. checked that they are valid JIDs and that the JID belongs
131: * to an existing user).
132: */
133: public static class Group {
134: private String name;
135: private String description;
136: /**
137: * Elements that the group contains. This includes admins, members or anything listed
138: * in the <tt>member field</tt>. At this point JIDs are not validated.
139: */
140: private int members;
141:
142: public Group(String name, String description, int members) {
143: this .name = name;
144: this .description = description;
145: this .members = members;
146: }
147:
148: public String getName() {
149: return name;
150: }
151:
152: public String getDescription() {
153: return description;
154: }
155:
156: public int getMembers() {
157: return members;
158: }
159: }
160: }
|