001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.monitors;
028    
029    
030    
031    import java.util.ArrayList;
032    import java.util.Collection;
033    import java.util.LinkedHashSet;
034    import java.util.TreeMap;
035    
036    import org.opends.server.admin.std.server.ConnectionHandlerCfg;
037    import org.opends.server.admin.std.server.ClientConnectionMonitorProviderCfg;
038    import org.opends.server.api.ClientConnection;
039    import org.opends.server.api.ConnectionHandler;
040    import org.opends.server.api.MonitorProvider;
041    import org.opends.server.config.ConfigException;
042    import org.opends.server.core.DirectoryServer;
043    import org.opends.server.types.Attribute;
044    import org.opends.server.types.AttributeType;
045    import org.opends.server.types.AttributeValue;
046    import org.opends.server.types.InitializationException;
047    
048    
049    
050    /**
051     * This class defines a Directory Server monitor provider that can be used to
052     * obtain information about the client connections established to the server.
053     * Note that the information reported is obtained with little or no locking, so
054     * it may not be entirely consistent, especially for active connections.
055     */
056    public class ClientConnectionMonitorProvider
057           extends MonitorProvider<ClientConnectionMonitorProviderCfg>
058    {
059      /**
060       * Creates an instance of this monitor provider.
061       */
062      public ClientConnectionMonitorProvider()
063      {
064        super("Client Connection Monitor Provider");
065    
066        // No initialization should be performed here.
067      }
068    
069    
070    
071      /**
072       * {@inheritDoc}
073       */
074      public void initializeMonitorProvider(
075                       ClientConnectionMonitorProviderCfg configuration)
076             throws ConfigException, InitializationException
077      {
078        // No initialization is required.
079      }
080    
081    
082    
083      /**
084       * Retrieves the name of this monitor provider.  It should be unique among all
085       * monitor providers, including all instances of the same monitor provider.
086       *
087       * @return  The name of this monitor provider.
088       */
089      public String getMonitorInstanceName()
090      {
091        return "Client Connections";
092      }
093    
094    
095    
096      /**
097       * Retrieves the length of time in milliseconds that should elapse between
098       * calls to the <CODE>updateMonitorData()</CODE> method.  A negative or zero
099       * return value indicates that the <CODE>updateMonitorData()</CODE> method
100       * should not be periodically invoked.
101       *
102       * @return  The length of time in milliseconds that should elapse between
103       *          calls to the <CODE>updateMonitorData()</CODE> method.
104       */
105      public long getUpdateInterval()
106      {
107        // This monitor does not need to run periodically.
108        return 0;
109      }
110    
111    
112    
113      /**
114       * Performs any processing periodic processing that may be desired to update
115       * the information associated with this monitor.  Note that best-effort
116       * attempts will be made to ensure that calls to this method come
117       * <CODE>getUpdateInterval()</CODE> milliseconds apart, but no guarantees will
118       * be made.
119       */
120      public void updateMonitorData()
121      {
122        // This monitor does not need to run periodically.
123        return;
124      }
125    
126    
127    
128      /**
129       * Retrieves a set of attributes containing monitor data that should be
130       * returned to the client if the corresponding monitor entry is requested.
131       *
132       * @return  A set of attributes containing monitor data that should be
133       *          returned to the client if the corresponding monitor entry is
134       *          requested.
135       */
136      public ArrayList<Attribute> getMonitorData()
137      {
138        // Get information about all the available connections.
139        ArrayList<Collection<ClientConnection>> connCollections =
140             new ArrayList<Collection<ClientConnection>>();
141        for (ConnectionHandler handler : DirectoryServer.getConnectionHandlers())
142        {
143          ConnectionHandler<? extends ConnectionHandlerCfg> connHandler =
144               (ConnectionHandler<? extends ConnectionHandlerCfg>) handler;
145          connCollections.add(connHandler.getClientConnections());
146        }
147    
148    
149        // Re-order the connections by connection ID.
150        TreeMap<Long,ClientConnection> connMap =
151             new TreeMap<Long,ClientConnection>();
152        for (Collection<ClientConnection> collection : connCollections)
153        {
154          for (ClientConnection conn : collection)
155          {
156            connMap.put(conn.getConnectionID(), conn);
157          }
158        }
159    
160    
161        // Iterate through all the client connections and create a one-line summary
162        // of each.
163        AttributeType attrType =
164             DirectoryServer.getDefaultAttributeType("connection");
165        LinkedHashSet<AttributeValue> values =
166             new LinkedHashSet<AttributeValue>(connMap.size());
167        for (ClientConnection conn : connMap.values())
168        {
169          values.add(new AttributeValue(attrType, conn.getMonitorSummary()));
170        }
171    
172    
173        ArrayList<Attribute> attrs = new ArrayList<Attribute>(1);
174        attrs.add(new Attribute(attrType, "connection", values));
175        return attrs;
176      }
177    }
178