001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import java.security.Principal;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import com.ecyrd.jspwiki.WikiContext;
028: import com.ecyrd.jspwiki.WikiEngine;
029: import com.ecyrd.jspwiki.WikiSession;
030:
031: /**
032: * <p>Displays information about active wiki sessions. The parameter
033: * <code>property</code> specifies what information is displayed.
034: * If omitted, the number of sessions is returned.
035: * Valid values for the <code>property</code> parameter
036: * include:</p>
037: * <ul>
038: * <li><code>users</code> - returns a comma-separated list of
039: * users</li>
040: * <li><code>distinctUsers</code> - will only show
041: * distinct users.
042: * </ul>
043: * @since 2.3.84
044: * @author Andrew Jaquith
045: */
046: public class SessionsPlugin implements WikiPlugin {
047: public static final String PARAM_PROP = "property";
048:
049: public String execute(WikiContext context, Map params)
050: throws PluginException {
051: WikiEngine engine = context.getEngine();
052: String prop = (String) params.get(PARAM_PROP);
053:
054: if ("users".equals(prop)) {
055: Principal[] principals = WikiSession.userPrincipals(engine);
056: StringBuffer s = new StringBuffer();
057: for (int i = 0; i < principals.length; i++) {
058: s.append(principals[i].getName() + ", ");
059: }
060: // remove the last comma and blank :
061: return s
062: .substring(0, s.length() - (s.length() > 2 ? 2 : 0));
063: }
064:
065: //
066: // show each user session only once (with a counter that indicates the
067: // number of sessions for each user)
068: if ("distinctUsers".equals(prop)) {
069: Principal[] principals = WikiSession.userPrincipals(engine);
070: // we do not assume that the principals are sorted, so first count
071: // them :
072: HashMap distinctPrincipals = new HashMap();
073: for (int i = 0; i < principals.length; i++) {
074: String principalName = principals[i].getName();
075:
076: if (distinctPrincipals.containsKey(principalName)) {
077: // we already have an entry, increase the counter:
078: int numSessions = ((Integer) distinctPrincipals
079: .get(principalName)).intValue();
080: // store the new value:
081: distinctPrincipals.put(principalName, new Integer(
082: ++numSessions));
083: } else {
084: // first time we see this entry, add entry to HashMap with
085: // value 1
086: distinctPrincipals.put(principalName,
087: new Integer(1));
088: }
089: }
090: //
091: //
092: StringBuffer s = new StringBuffer();
093: Iterator entries = distinctPrincipals.entrySet().iterator();
094: while (entries.hasNext()) {
095: Map.Entry entry = (Map.Entry) entries.next();
096: s.append((entry.getKey().toString() + "("
097: + entry.getValue().toString() + "), "));
098: }
099: // remove the last comma and blank :
100: return s.substring(0, s.length() - 2);
101: }
102:
103: return String.valueOf(WikiSession.sessions(engine));
104: }
105: }
|