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 java.util.ArrayList; 020import java.util.Collection; 021import java.util.TreeMap; 022 023import org.forgerock.opendj.config.server.ConfigException; 024import org.forgerock.opendj.server.config.server.ClientConnectionMonitorProviderCfg; 025import org.opends.server.api.ClientConnection; 026import org.opends.server.api.ConnectionHandler; 027import org.opends.server.api.MonitorData; 028import org.opends.server.api.MonitorProvider; 029import org.opends.server.core.DirectoryServer; 030import org.opends.server.types.InitializationException; 031 032/** 033 * This class defines a Directory Server monitor provider that can be 034 * used to obtain information about the client connections established 035 * to the server. Note that the information reported is obtained with 036 * little or no locking, so it may not be entirely consistent, 037 * especially for active connections. 038 */ 039public class ClientConnectionMonitorProvider extends 040 MonitorProvider<ClientConnectionMonitorProviderCfg> 041{ 042 043 /** 044 * The connection handler associated with this monitor, or null if all 045 * connection handlers should be monitored. 046 */ 047 private final ConnectionHandler<?> handler; 048 049 050 051 /** 052 * Creates an instance of this monitor provider. 053 */ 054 public ClientConnectionMonitorProvider() 055 { 056 // This will monitor all connection handlers. 057 this.handler = null; 058 } 059 060 061 062 /** 063 * Creates an instance of this monitor provider. 064 * 065 * @param handler 066 * to which the monitor provider is associated. 067 */ 068 public ClientConnectionMonitorProvider(ConnectionHandler handler) 069 { 070 this.handler = handler; 071 } 072 073 074 075 /** {@inheritDoc} */ 076 @Override 077 public void initializeMonitorProvider( 078 ClientConnectionMonitorProviderCfg configuration) 079 throws ConfigException, InitializationException 080 { 081 // No initialization is required. 082 } 083 084 085 086 /** 087 * Retrieves the name of this monitor provider. It should be unique 088 * among all monitor providers, including all instances of the same 089 * monitor provider. 090 * 091 * @return The name of this monitor provider. 092 */ 093 @Override 094 public String getMonitorInstanceName() 095 { 096 if (handler == null) 097 { 098 return "Client Connections"; 099 } 100 else 101 { 102 // Client connections of a connection handler 103 return "Client Connections" + ",cn=" 104 + handler.getConnectionHandlerName(); 105 } 106 } 107 108 @Override 109 public MonitorData getMonitorData() 110 { 111 // Re-order the connections by connection ID. 112 TreeMap<Long, ClientConnection> connMap = new TreeMap<>(); 113 114 if (handler != null) 115 { 116 for (ClientConnection conn : handler.getClientConnections()) 117 { 118 connMap.put(conn.getConnectionID(), conn); 119 } 120 } 121 else 122 { 123 // Get information about all the available connections. 124 for (ConnectionHandler<?> hdl : DirectoryServer.getConnectionHandlers()) 125 { 126 // FIXME: connections from different handlers could have the same ID. 127 for (ClientConnection conn : hdl.getClientConnections()) 128 { 129 connMap.put(conn.getConnectionID(), conn); 130 } 131 } 132 } 133 134 Collection<String> connectionSummaries = new ArrayList<>(connMap.values().size()); 135 for (ClientConnection conn : connMap.values()) 136 { 137 connectionSummaries.add(conn.getMonitorSummary()); 138 } 139 MonitorData result = new MonitorData(1); 140 result.add("connection", connectionSummaries); 141 return result; 142 } 143}