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.AttributeType;
041import org.opends.server.types.InitializationException;
042
043/**
044 * This class defines a Directory Server monitor provider that can be
045 * used to obtain information about the client connections established
046 * to the server. Note that the information reported is obtained with
047 * little or no locking, so it may not be entirely consistent,
048 * especially for active connections.
049 */
050public class ClientConnectionMonitorProvider extends
051    MonitorProvider<ClientConnectionMonitorProviderCfg>
052{
053
054  /**
055   * The connection handler associated with this monitor, or null if all
056   * connection handlers should be monitored.
057   */
058  private final ConnectionHandler<?> handler;
059
060
061
062  /**
063   * Creates an instance of this monitor provider.
064   */
065  public ClientConnectionMonitorProvider()
066  {
067    // This will monitor all connection handlers.
068    this.handler = null;
069  }
070
071
072
073  /**
074   * Creates an instance of this monitor provider.
075   *
076   * @param handler
077   *          to which the monitor provider is associated.
078   */
079  public ClientConnectionMonitorProvider(ConnectionHandler handler)
080  {
081    this.handler = handler;
082  }
083
084
085
086  /** {@inheritDoc} */
087  @Override
088  public void initializeMonitorProvider(
089      ClientConnectionMonitorProviderCfg configuration)
090      throws ConfigException, InitializationException
091  {
092    // No initialization is required.
093  }
094
095
096
097  /**
098   * Retrieves the name of this monitor provider. It should be unique
099   * among all monitor providers, including all instances of the same
100   * monitor provider.
101   *
102   * @return The name of this monitor provider.
103   */
104  @Override
105  public String getMonitorInstanceName()
106  {
107    if (handler == null)
108    {
109      return "Client Connections";
110    }
111    else
112    {
113      // Client connections of a connection handler
114      return "Client Connections" + ",cn="
115          + handler.getConnectionHandlerName();
116    }
117  }
118
119
120
121  /**
122   * Retrieves a set of attributes containing monitor data that should
123   * be returned to the client if the corresponding monitor entry is
124   * requested.
125   *
126   * @return A set of attributes containing monitor data that should be
127   *         returned to the client if the corresponding monitor entry
128   *         is requested.
129   */
130  @Override
131  public List<Attribute> getMonitorData()
132  {
133    // Re-order the connections by connection ID.
134    TreeMap<Long, ClientConnection> connMap = new TreeMap<>();
135
136    if (handler == null)
137    {
138      // Get information about all the available connections.
139      for (ConnectionHandler<?> hdl : DirectoryServer.getConnectionHandlers())
140      {
141        // FIXME: connections from different handlers could have the same ID.
142        for (ClientConnection conn : hdl.getClientConnections())
143        {
144          connMap.put(conn.getConnectionID(), conn);
145        }
146      }
147    }
148    else
149    {
150      for (ClientConnection conn : handler.getClientConnections())
151      {
152        connMap.put(conn.getConnectionID(), conn);
153      }
154    }
155
156    AttributeType attrType = DirectoryServer.getDefaultAttributeType("connection");
157    AttributeBuilder builder = new AttributeBuilder(attrType);
158    for (ClientConnection conn : connMap.values())
159    {
160      builder.add(conn.getMonitorSummary());
161    }
162    return builder.toAttributeList();
163  }
164}