01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
05:
06: This program is free software; you can redistribute it and/or modify
07: it under the terms of the GNU Lesser General Public License as published by
08: the Free Software Foundation; either version 2.1 of the License, or
09: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package com.ecyrd.jspwiki.plugin;
21:
22: import java.security.Principal;
23: import java.util.Arrays;
24: import java.util.Comparator;
25: import java.util.Map;
26:
27: import com.ecyrd.jspwiki.WikiContext;
28: import com.ecyrd.jspwiki.WikiEngine;
29: import com.ecyrd.jspwiki.auth.PrincipalComparator;
30: import com.ecyrd.jspwiki.auth.authorize.GroupManager;
31:
32: /**
33: * <p>Prints the groups managed by this wiki, separated by commas.
34: * The groups are sorted in ascending order, and are hyperlinked
35: * to the page that displays the group's members.</p>
36: * @since 2.4.19
37: * @author Andrew Jaquith
38: */
39: public class Groups implements WikiPlugin {
40: private static final Comparator COMPARATOR = new PrincipalComparator();
41:
42: public String execute(WikiContext context, Map params)
43: throws PluginException {
44: // Retrieve groups, and sort by name
45: WikiEngine engine = context.getEngine();
46: GroupManager groupMgr = engine.getGroupManager();
47: Principal[] groups = groupMgr.getRoles();
48: Arrays.sort(groups, COMPARATOR);
49:
50: StringBuffer s = new StringBuffer();
51:
52: for (int i = 0; i < groups.length; i++) {
53: String name = groups[i].getName();
54:
55: // Make URL
56: String url = engine.getURLConstructor().makeURL(
57: WikiContext.VIEW_GROUP, name, false, null);
58:
59: // Create hyperlink
60: s.append("<a href=\"");
61: s.append(url);
62: s.append("\">");
63: s.append(name);
64: s.append("</a>");
65:
66: // If not the last one, add a comma and space
67: if (i < (groups.length - 1)) {
68: s.append(',');
69: s.append(' ');
70: }
71: }
72: return s.toString();
73: }
74: }
|