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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.monitors;
028
029import java.util.List;
030import java.util.TreeMap;
031
032import org.forgerock.opendj.config.server.ConfigException;
033import org.opends.server.admin.std.server.ClientConnectionMonitorProviderCfg;
034import org.opends.server.api.ClientConnection;
035import org.opends.server.api.ConnectionHandler;
036import org.opends.server.api.MonitorProvider;
037import org.opends.server.core.DirectoryServer;
038import org.opends.server.types.Attribute;
039import org.opends.server.types.AttributeBuilder;
040import org.opends.server.types.InitializationException;
041
042/**
043 * This class defines a Directory Server monitor provider that can be
044 * used to obtain information about the client connections established
045 * to the server. Note that the information reported is obtained with
046 * little or no locking, so it may not be entirely consistent,
047 * especially for active connections.
048 */
049public class ClientConnectionMonitorProvider extends
050    MonitorProvider<ClientConnectionMonitorProviderCfg>
051{
052
053  /**
054   * The connection handler associated with this monitor, or null if all
055   * connection handlers should be monitored.
056   */
057  private final ConnectionHandler<?> handler;
058
059
060
061  /**
062   * Creates an instance of this monitor provider.
063   */
064  public ClientConnectionMonitorProvider()
065  {
066    // This will monitor all connection handlers.
067    this.handler = null;
068  }
069
070
071
072  /**
073   * Creates an instance of this monitor provider.
074   *
075   * @param handler
076   *          to which the monitor provider is associated.
077   */
078  public ClientConnectionMonitorProvider(ConnectionHandler handler)
079  {
080    this.handler = handler;
081  }
082
083
084
085  /** {@inheritDoc} */
086  @Override
087  public void initializeMonitorProvider(
088      ClientConnectionMonitorProviderCfg configuration)
089      throws ConfigException, InitializationException
090  {
091    // No initialization is required.
092  }
093
094
095
096  /**
097   * Retrieves the name of this monitor provider. It should be unique
098   * among all monitor providers, including all instances of the same
099   * monitor provider.
100   *
101   * @return The name of this monitor provider.
102   */
103  @Override
104  public String getMonitorInstanceName()
105  {
106    if (handler == null)
107    {
108      return "Client Connections";
109    }
110    else
111    {
112      // Client connections of a connection handler
113      return "Client Connections" + ",cn="
114          + handler.getConnectionHandlerName();
115    }
116  }
117
118
119
120  /**
121   * Retrieves a set of attributes containing monitor data that should
122   * be returned to the client if the corresponding monitor entry is
123   * requested.
124   *
125   * @return A set of attributes containing monitor data that should be
126   *         returned to the client if the corresponding monitor entry
127   *         is requested.
128   */
129  @Override
130  public List<Attribute> getMonitorData()
131  {
132    // Re-order the connections by connection ID.
133    TreeMap<Long, ClientConnection> connMap = new TreeMap<>();
134
135    if (handler == null)
136    {
137      // Get information about all the available connections.
138      for (ConnectionHandler<?> hdl : DirectoryServer.getConnectionHandlers())
139      {
140        // FIXME: connections from different handlers could have the same ID.
141        for (ClientConnection conn : hdl.getClientConnections())
142        {
143          connMap.put(conn.getConnectionID(), conn);
144        }
145      }
146    }
147    else
148    {
149      for (ClientConnection conn : handler.getClientConnections())
150      {
151        connMap.put(conn.getConnectionID(), conn);
152      }
153    }
154
155    AttributeBuilder builder = new AttributeBuilder("connection");
156    for (ClientConnection conn : connMap.values())
157    {
158      builder.add(conn.getMonitorSummary());
159    }
160    return builder.toAttributeList();
161  }
162}