Commit d5246f3a authored by Matt Tucker's avatar Matt Tucker Committed by matt

Group code cleanup.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3529 b35dd754-fafc-0310-a699-88a17e54d16e
parent aa7beec9
...@@ -87,7 +87,7 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -87,7 +87,7 @@ public class DefaultGroupProvider implements GroupProvider {
} }
Collection<JID> members = getMembers(name, false); Collection<JID> members = getMembers(name, false);
Collection<JID> administrators = getMembers(name, true); Collection<JID> administrators = getMembers(name, true);
return new Group(this, name, "", members, administrators); return new Group(name, "", members, administrators);
} }
public Group getGroup(String name) throws GroupNotFoundException { public Group getGroup(String name) throws GroupNotFoundException {
...@@ -117,7 +117,7 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -117,7 +117,7 @@ public class DefaultGroupProvider implements GroupProvider {
} }
Collection<JID> members = getMembers(name, false); Collection<JID> members = getMembers(name, false);
Collection<JID> administrators = getMembers(name, true); Collection<JID> administrators = getMembers(name, true);
return new Group(this, name, description, members, administrators); return new Group(name, description, members, administrators);
} }
public void setDescription(String name, String description) public void setDescription(String name, String description)
...@@ -257,17 +257,7 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -257,17 +257,7 @@ public class DefaultGroupProvider implements GroupProvider {
try { if (con != null) con.close(); } try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
} }
List<Group> groups = new ArrayList<Group>(groupNames.size()); return new GroupCollection(groupNames.toArray(new String[groupNames.size()]));
GroupManager manager = GroupManager.getInstance();
for (String groupName : groupNames) {
try {
groups.add(manager.getGroup(groupName));
}
catch (GroupNotFoundException e) {
Log.error(e);
}
}
return groups;
} }
public Collection<Group> getGroups(int startIndex, int numResults) { public Collection<Group> getGroups(int startIndex, int numResults) {
...@@ -295,17 +285,7 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -295,17 +285,7 @@ public class DefaultGroupProvider implements GroupProvider {
try { if (con != null) con.close(); } try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
} }
List<Group> groups = new ArrayList<Group>(groupNames.size()); return new GroupCollection(groupNames.toArray(new String[groupNames.size()]));
GroupManager manager = GroupManager.getInstance();
for (String groupName : groupNames) {
try {
groups.add(manager.getGroup(groupName));
}
catch (GroupNotFoundException e) {
Log.error(e);
}
}
return groups;
} }
public Collection<Group> getGroups(JID user) { public Collection<Group> getGroups(JID user) {
...@@ -331,17 +311,7 @@ public class DefaultGroupProvider implements GroupProvider { ...@@ -331,17 +311,7 @@ public class DefaultGroupProvider implements GroupProvider {
try { if (con != null) con.close(); } try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
} }
List<Group> groups = new ArrayList<Group>(groupNames.size()); return new GroupCollection(groupNames.toArray(new String[groupNames.size()]));
GroupManager manager = GroupManager.getInstance();
for (String groupName : groupNames) {
try {
groups.add(manager.getGroup(groupName));
}
catch (GroupNotFoundException e) {
Log.error(e);
}
}
return groups;
} }
public void addMember(String groupName, JID user, boolean administrator) { public void addMember(String groupName, JID user, boolean administrator) {
......
/**
* $Revision: 691 $
* $Date: 2004-12-13 15:06:54 -0300 (Mon, 13 Dec 2004) $
*
* Copyright (C) 2004-2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.group;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Provides a view of an array of group names as a Collection of Group objects. If
* any of the group names cannot be loaded, they are transparently skipped when
* iterating over the collection.
*
* @author Matt Tucker
*/
public class GroupCollection extends AbstractCollection {
private String[] elements;
/**
* Constructs a new GroupCollection.
*/
public GroupCollection(String [] elements) {
this.elements = elements;
}
public Iterator iterator() {
return new UserIterator();
}
public int size() {
return elements.length;
}
private class UserIterator implements Iterator {
private int currentIndex = -1;
private Object nextElement = null;
public boolean hasNext() {
// If we are at the end of the list, there can't be any more elements
// to iterate through.
if (currentIndex + 1 >= elements.length && nextElement == null) {
return false;
}
// Otherwise, see if nextElement is null. If so, try to load the next
// element to make sure it exists.
if (nextElement == null) {
nextElement = getNextElement();
if (nextElement == null) {
return false;
}
}
return true;
}
public Object next() throws java.util.NoSuchElementException {
Object element;
if (nextElement != null) {
element = nextElement;
nextElement = null;
}
else {
element = getNextElement();
if (element == null) {
throw new NoSuchElementException();
}
}
return element;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Returns the next available element, or null if there are no more elements to return.
*
* @return the next available element.
*/
private Object getNextElement() {
while (currentIndex + 1 < elements.length) {
currentIndex++;
Object element = null;
try {
element = GroupManager.getInstance().getGroup(elements[currentIndex]);
}
catch (GroupNotFoundException unfe) {
// Ignore.
}
if (element != null) {
return element;
}
}
return null;
}
}
}
\ No newline at end of file
...@@ -84,14 +84,16 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -84,14 +84,16 @@ public class LdapGroupProvider implements GroupProvider {
manager.getGroupNameField() + "=" + group + "))"; manager.getGroupNameField() + "=" + group + "))";
Collection<Group> groups = populateGroups(searchForGroups(searchFilter, standardAttributes)); Collection<Group> groups = populateGroups(searchForGroups(searchFilter, standardAttributes));
if (groups.size() > 1) { if (groups.size() > 1) {
//if multiple groups found throw exception // If multiple groups found, throw exception.
throw new GroupNotFoundException("Too many groups with name " + group + " were found."); throw new GroupNotFoundException("Too many groups with name " + group + " were found.");
} }
for (Group g : groups) { else if (groups.isEmpty()) {
return g; //returns the first group found
}
throw new GroupNotFoundException("Group with name " + group + " not found."); throw new GroupNotFoundException("Group with name " + group + " not found.");
} }
else {
return groups.iterator().next();
}
}
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because
...@@ -136,6 +138,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -136,6 +138,7 @@ public class LdapGroupProvider implements GroupProvider {
answer.next(); answer.next();
} }
catch (Exception e) { catch (Exception e) {
// Ignore.
} }
} }
...@@ -189,8 +192,8 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -189,8 +192,8 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups
* LDAP groups are read-only. * are read-only.
* *
* @param groupName name of a group. * @param groupName name of a group.
* @param user the JID of the user to add * @param user the JID of the user to add
...@@ -204,8 +207,8 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -204,8 +207,8 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups
* LDAP groups are read-only. * are read-only.
* *
* @param groupName the naame of a group. * @param groupName the naame of a group.
* @param user the JID of the user with new privileges * @param user the JID of the user with new privileges
...@@ -219,21 +222,19 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -219,21 +222,19 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups
* LDAP groups are read-only. * are read-only.
* *
* @param groupName the name of a group. * @param groupName the name of a group.
* @param user the JID of the user to delete. * @param user the JID of the user to delete.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void deleteMember(String groupName, JID user) public void deleteMember(String groupName, JID user) throws UnsupportedOperationException {
throws UnsupportedOperationException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** /**
* Always throws an UnsupportedOperationException because * Returns true because LDAP groups are read-only.
* LDAP groups are read-only.
* *
* @return true because all LDAP functions are read-only. * @return true because all LDAP functions are read-only.
*/ */
...@@ -253,7 +254,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -253,7 +254,7 @@ public class LdapGroupProvider implements GroupProvider {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Trying to find all groups in the system."); Log.debug("Trying to find all groups in the system.");
} }
DirContext ctx = null; DirContext ctx;
NamingEnumeration<SearchResult> answer = null; NamingEnumeration<SearchResult> answer = null;
try { try {
ctx = manager.getContext(); ctx = manager.getContext();
...@@ -294,7 +295,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -294,7 +295,7 @@ public class LdapGroupProvider implements GroupProvider {
TreeMap<String, Group> groups = new TreeMap<String, Group>(); TreeMap<String, Group> groups = new TreeMap<String, Group>();
DirContext ctx = null; DirContext ctx;
try { try {
ctx = manager.getContext(); ctx = manager.getContext();
} }
...@@ -313,7 +314,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -313,7 +314,7 @@ public class LdapGroupProvider implements GroupProvider {
while (answer.hasMoreElements()) { while (answer.hasMoreElements()) {
String name = ""; String name = "";
try { try {
Attributes a = (((SearchResult) answer.nextElement()).getAttributes()); Attributes a = answer.nextElement().getAttributes();
String description; String description;
try { try {
name = ((String) ((a.get(manager.getGroupNameField())).get())); name = ((String) ((a.get(manager.getGroupNameField())).get()));
...@@ -355,7 +356,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -355,7 +356,7 @@ public class LdapGroupProvider implements GroupProvider {
// Therefore, we have to try to load each user we found to see if // Therefore, we have to try to load each user we found to see if
// it passes the filter. // it passes the filter.
try { try {
JID userJID = null; JID userJID;
// Create JID of local user if JID does not match a component's JID // Create JID of local user if JID does not match a component's JID
if (!username.contains(serverName)) { if (!username.contains(serverName)) {
// In order to lookup a username from the manager, the username // In order to lookup a username from the manager, the username
...@@ -381,7 +382,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -381,7 +382,7 @@ public class LdapGroupProvider implements GroupProvider {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Adding group \"" + name + "\" with " + members.size() + " members."); Log.debug("Adding group \"" + name + "\" with " + members.size() + " members.");
} }
Group g = new Group(this, name, description, members, new ArrayList<JID>()); Group g = new Group(name, description, members, new ArrayList<JID>());
groups.put(name, g); groups.put(name, g);
} }
catch (Exception e) { catch (Exception e) {
...@@ -397,6 +398,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -397,6 +398,7 @@ public class LdapGroupProvider implements GroupProvider {
ctx.close(); ctx.close();
} }
catch (Exception e) { catch (Exception e) {
// Ignore.
} }
return groups.values(); return groups.values();
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
java.net.URLEncoder" java.net.URLEncoder"
errorPage="error.jsp" errorPage="error.jsp"
%> %>
<%@ page import="org.jivesoftware.wildfire.group.Group"%>
<%@ page import="java.util.Collection"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %> <%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
...@@ -195,6 +197,31 @@ ...@@ -195,6 +197,31 @@
<%= JiveGlobals.formatDate(user.getCreationDate()) %> <%= JiveGlobals.formatDate(user.getCreationDate()) %>
</td> </td>
</tr> </tr>
<tr>
<td class="c1">
Groups:
</td>
<td>
<%
Collection<Group> groups = webManager.getGroupManager().getGroups(user);
if (groups.isEmpty()) {
%>
<i>None</i>
<%
}
else {
int count = 0;
for (Group group: groups) {
if (count != 0) {
out.print(", ");
}
out.print(group.getName());
count ++;
}
}
%>
</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment