001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.monitors;
018
019import static org.opends.server.util.ServerConstants.*;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.LinkedList;
024
025import org.forgerock.opendj.server.config.server.ConnectionHandlerCfg;
026import org.forgerock.opendj.server.config.server.MonitorProviderCfg;
027import org.opends.server.api.ClientConnection;
028import org.opends.server.api.ConnectionHandler;
029import org.opends.server.api.MonitorData;
030import org.opends.server.api.MonitorProvider;
031import org.opends.server.core.DirectoryServer;
032import org.opends.server.types.HostPort;
033import org.forgerock.opendj.ldap.schema.ObjectClass;
034
035/**
036 * This class implements a monitor provider that will report generic information
037 * for an enabled Directory Server connection handler, including its protocol,
038 * listeners, and established connections.
039 */
040public class ConnectionHandlerMonitor
041       extends MonitorProvider<MonitorProviderCfg>
042{
043  /** The connection handler with which this monitor is associated. */
044  private ConnectionHandler<?> connectionHandler;
045
046  /** The name for this monitor. */
047  private String monitorName;
048
049
050
051  /**
052   * Creates a new instance of this connection handler monitor provider that
053   * will work with the provided connection handler.  Most of the initialization
054   * should be handled in the {@code initializeMonitorProvider} method.
055   *
056   * @param  connectionHandler  The connection handler with which this monitor
057   *                            is associated.
058   */
059  public ConnectionHandlerMonitor(
060       ConnectionHandler<? extends ConnectionHandlerCfg> connectionHandler)
061  {
062    this.connectionHandler = connectionHandler;
063  }
064
065
066
067  @Override
068  public void initializeMonitorProvider(MonitorProviderCfg configuration)
069  {
070    monitorName = connectionHandler.getConnectionHandlerName();
071  }
072
073
074
075  /** {@inheritDoc} */
076  @Override
077  public String getMonitorInstanceName()
078  {
079    return monitorName;
080  }
081
082
083
084  /**
085   * Retrieves the objectclass that should be included in the monitor entry
086   * created from this monitor provider.
087   *
088   * @return  The objectclass that should be included in the monitor entry
089   *          created from this monitor provider.
090   */
091  @Override
092  public ObjectClass getMonitorObjectClass()
093  {
094    return DirectoryServer.getSchema().getObjectClass(OC_MONITOR_CONNHANDLER);
095  }
096
097
098
099  @Override
100  public MonitorData getMonitorData()
101  {
102    LinkedList<ClientConnection> conns = new LinkedList<>(connectionHandler.getClientConnections());
103    LinkedList<HostPort> listeners = new LinkedList<>(connectionHandler.getListeners());
104
105    final MonitorData attrs = new MonitorData(5);
106    attrs.add(ATTR_MONITOR_CONFIG_DN, connectionHandler.getComponentEntryDN());
107    attrs.add(ATTR_MONITOR_CONNHANDLER_PROTOCOL, connectionHandler.getProtocol());
108
109    if (!listeners.isEmpty())
110    {
111      attrs.add(ATTR_MONITOR_CONNHANDLER_LISTENER, listeners);
112    }
113
114    attrs.add(ATTR_MONITOR_CONNHANDLER_NUMCONNECTIONS, conns.size());
115    if (!conns.isEmpty())
116    {
117      Collection<String> connectionSummaries = new ArrayList<>();
118      for (ClientConnection c : conns)
119      {
120        connectionSummaries.add(c.getMonitorSummary());
121      }
122      attrs.add(ATTR_MONITOR_CONNHANDLER_CONNECTION, connectionSummaries);
123    }
124
125    return attrs;
126  }
127}