001    /* 
002        Licensed to the Apache Software Foundation (ASF) under one
003        or more contributor license agreements.  See the NOTICE file
004        distributed with this work for additional information
005        regarding copyright ownership.  The ASF licenses this file
006        to you under the Apache License, Version 2.0 (the
007        "License"); you may not use this file except in compliance
008        with the License.  You may obtain a copy of the License at
009    
010           http://www.apache.org/licenses/LICENSE-2.0
011    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.plugin;
020    
021    import java.security.Principal;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    import org.apache.wiki.WikiContext;
027    import org.apache.wiki.WikiEngine;
028    import org.apache.wiki.WikiSession;
029    import org.apache.wiki.api.exceptions.PluginException;
030    import org.apache.wiki.api.plugin.WikiPlugin;
031    
032    /**
033     *  <p>Displays information about active wiki sessions. The parameter
034     *  <code>property</code> specifies what information is displayed.
035     *  If omitted, the number of sessions is returned.
036     *  
037     *  <p>Parameters : </p>
038     *  <ul>
039     *  <li><b>property</b> - specify what output to display, valid values are:</li>
040     *  <ul>
041     *    <li><code>users</code> - returns a comma-separated list of
042     *    users</li>
043     *    <li><code>distinctUsers</code> - will only show
044     *    distinct users.
045     *  </ul>
046     *  </ul>
047     *  @since 2.3.84
048     */
049    public class SessionsPlugin
050        implements WikiPlugin
051    {
052        /** The parameter name for setting the property value. */
053        public static final String PARAM_PROP = "property";
054        
055        /**
056         *  {@inheritDoc}
057         */
058        public String execute( WikiContext context, Map<String, String> params )
059            throws PluginException
060        {
061            WikiEngine engine = context.getEngine();
062            String prop = params.get( PARAM_PROP );
063            
064            if ( "users".equals( prop ) )
065            {
066                Principal[] principals = WikiSession.userPrincipals( engine );
067                StringBuffer s = new StringBuffer();
068                for ( int i = 0; i < principals.length; i++ )
069                {
070                    s.append(principals[i].getName() + ", ");
071                }
072                // remove the last comma and blank :
073                return s.substring(0, s.length() - (s.length() > 2 ? 2 : 0) );
074            }
075    
076            //
077            // show each user session only once (with a counter that indicates the
078            // number of sessions for each user)
079            if ("distinctUsers".equals(prop))
080            {
081                Principal[] principals = WikiSession.userPrincipals(engine);
082                // we do not assume that the principals are sorted, so first count
083                // them :
084                HashMap<String,Integer> distinctPrincipals = new HashMap<String,Integer>();
085                for (int i = 0; i < principals.length; i++)
086                {
087                    String principalName = principals[i].getName();
088    
089                    if (distinctPrincipals.containsKey(principalName))
090                    {
091                        // we already have an entry, increase the counter:
092                        int numSessions = distinctPrincipals.get(principalName).intValue();
093                        // store the new value:
094                        distinctPrincipals.put(principalName, ++numSessions);
095                    }
096                    else
097                    {
098                        // first time we see this entry, add entry to HashMap with value 1
099                        distinctPrincipals.put(principalName, 1);
100                    }
101                }
102                //
103                //
104                StringBuffer s = new StringBuffer();
105                Iterator entries = distinctPrincipals.entrySet().iterator();
106                while (entries.hasNext())
107                {
108                    Map.Entry entry = (Map.Entry)entries.next();
109                    s.append( entry.getKey().toString() + "(" + entry.getValue().toString() + "), " );
110                }
111                // remove the last comma and blank :
112                return s.substring(0, s.length() - 2);
113            }
114    
115            return String.valueOf( WikiSession.sessions( engine ) );
116        }
117    }