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.LinkedHashSet;
032    import java.util.LinkedList;
033    import java.util.List;
034    
035    import org.opends.server.admin.std.server.ConnectionHandlerCfg;
036    import org.opends.server.admin.std.server.MonitorProviderCfg;
037    import org.opends.server.api.ClientConnection;
038    import org.opends.server.api.ConnectionHandler;
039    import org.opends.server.api.MonitorProvider;
040    import org.opends.server.types.Attribute;
041    import org.opends.server.types.AttributeType;
042    import org.opends.server.types.AttributeValue;
043    import org.opends.server.types.ByteStringFactory;
044    import org.opends.server.types.DirectoryConfig;
045    import org.opends.server.types.HostPort;
046    import org.opends.server.types.ObjectClass;
047    
048    import static org.opends.server.util.ServerConstants.*;
049    
050    
051    
052    /**
053     * This class implements a monitor provider that will report generic information
054     * for an enabled Directory Server connection handler, including its protocol,
055     * listeners, and established connections.
056     */
057    public class ConnectionHandlerMonitor
058           extends MonitorProvider<MonitorProviderCfg>
059    {
060      // The attribute type that will be used to report the established connections.
061      private AttributeType connectionsType;
062    
063      // The attribute type that will be used to report the listeners.
064      private AttributeType listenerType;
065    
066      // The attribute type that will be used to report the number of established
067      // client connections.
068      private AttributeType numConnectionsType;
069    
070      // The attribute type that will be used to report the protocol.
071      private AttributeType protocolType;
072    
073      // The connection handler with which this monitor is associated.
074      private ConnectionHandler<?> connectionHandler;
075    
076      // The name for this monitor.
077      private String monitorName;
078    
079    
080    
081      /**
082       * Creates a new instance of this connection handler monitor provider that
083       * will work with the provided connection handler.  Most of the initialization
084       * should be handled in the {@code initializeMonitorProvider} method.
085       *
086       * @param  connectionHandler  The connection handler with which this monitor
087       *                            is associated.
088       */
089      public ConnectionHandlerMonitor(
090           ConnectionHandler<? extends ConnectionHandlerCfg> connectionHandler)
091      {
092        super(connectionHandler.getConnectionHandlerName());
093    
094        this.connectionHandler = connectionHandler;
095      }
096    
097    
098    
099      /**
100       * {@inheritDoc}
101       */
102      public void initializeMonitorProvider(MonitorProviderCfg configuration)
103      {
104        monitorName = connectionHandler.getConnectionHandlerName();
105    
106        connectionsType =
107             DirectoryConfig.getAttributeType(ATTR_MONITOR_CONNHANDLER_CONNECTION,
108                                              true);
109    
110        listenerType =
111             DirectoryConfig.getAttributeType(ATTR_MONITOR_CONNHANDLER_LISTENER,
112                                              true);
113    
114        numConnectionsType =
115             DirectoryConfig.getAttributeType(
116                  ATTR_MONITOR_CONNHANDLER_NUMCONNECTIONS, true);
117    
118        protocolType =
119             DirectoryConfig.getAttributeType(ATTR_MONITOR_CONNHANDLER_PROTOCOL,
120                                              true);
121      }
122    
123    
124    
125      /**
126       * {@inheritDoc}
127       */
128      public String getMonitorInstanceName()
129      {
130        return monitorName;
131      }
132    
133    
134    
135      /**
136       * Retrieves the objectclass that should be included in the monitor entry
137       * created from this monitor provider.
138       *
139       * @return  The objectclass that should be included in the monitor entry
140       *          created from this monitor provider.
141       */
142      public ObjectClass getMonitorObjectClass()
143      {
144        return DirectoryConfig.getObjectClass(OC_MONITOR_CONNHANDLER, true);
145      }
146    
147    
148    
149      /**
150       * {@inheritDoc}
151       */
152      public long getUpdateInterval()
153      {
154        // We don't need do anything on a periodic basis.
155        return 0;
156      }
157    
158    
159    
160      /**
161       * {@inheritDoc}
162       */
163      public void updateMonitorData()
164      {
165        // No implementaiton is required.
166      }
167    
168    
169    
170      /**
171       * {@inheritDoc}
172       */
173      public List<Attribute> getMonitorData()
174      {
175        LinkedList<Attribute> attrs = new LinkedList<Attribute>();
176    
177        int numConnections = 0;
178        LinkedList<ClientConnection> conns =
179             new LinkedList<ClientConnection>(
180                      connectionHandler.getClientConnections());
181        LinkedList<HostPort> listeners =
182             new LinkedList<HostPort>(connectionHandler.getListeners());
183    
184        LinkedHashSet<AttributeValue> values = new LinkedHashSet<AttributeValue>();
185        values.add(new AttributeValue(protocolType,
186             ByteStringFactory.create(connectionHandler.getProtocol())));
187        attrs.add(new Attribute(protocolType, ATTR_MONITOR_CONNHANDLER_PROTOCOL,
188                                values));
189    
190    
191        if (! listeners.isEmpty())
192        {
193          values = new LinkedHashSet<AttributeValue>();
194          for (HostPort hp : listeners)
195          {
196            values.add(new AttributeValue(listenerType,
197                                          ByteStringFactory.create(hp.toString())));
198          }
199          attrs.add(new Attribute(listenerType, ATTR_MONITOR_CONNHANDLER_LISTENER,
200                                  values));
201        }
202    
203        if (! conns.isEmpty())
204        {
205          values = new LinkedHashSet<AttributeValue>();
206          for (ClientConnection c : conns)
207          {
208            numConnections++;
209            values.add(new AttributeValue(connectionsType,
210                 ByteStringFactory.create(c.getMonitorSummary())));
211          }
212          attrs.add(new Attribute(connectionsType,
213                                  ATTR_MONITOR_CONNHANDLER_CONNECTION, values));
214        }
215    
216        values = new LinkedHashSet<AttributeValue>();
217        values.add(new AttributeValue(numConnectionsType,
218             ByteStringFactory.create(String.valueOf(numConnections))));
219        attrs.add(new Attribute(numConnectionsType,
220                                ATTR_MONITOR_CONNHANDLER_NUMCONNECTIONS, values));
221    
222        return attrs;
223      }
224    }
225