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 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.monitors;
028    
029    import java.util.ArrayList;
030    import org.opends.messages.Message;
031    
032    import org.opends.server.admin.std.server.EntryCacheCfg;
033    import org.opends.server.admin.std.server.EntryCacheMonitorProviderCfg;
034    import org.opends.server.api.EntryCache;
035    import org.opends.server.api.MonitorProvider;
036    import org.opends.server.config.ConfigConstants;
037    import org.opends.server.config.ConfigException;
038    import org.opends.server.core.DirectoryServer;
039    import org.opends.server.types.Attribute;
040    
041    import static org.opends.server.loggers.debug.DebugLogger.*;
042    import static org.opends.server.loggers.ErrorLogger.*;
043    import static org.opends.messages.ConfigMessages.*;
044    
045    /**
046     * This class defines a Directory Server monitor provider that can be used to
047     * obtain information about the entry cache state. Note that the information
048     * reported is obtained with no locking, so it may not be entirely consistent.
049     */
050    public class EntryCacheMonitorProvider
051           extends MonitorProvider<EntryCacheMonitorProviderCfg>
052    {
053      // The name for this monitor.
054      private String monitorName;
055    
056      // The entry cache common name.
057      private String entryCacheName;
058    
059      // The entry cache with which this monitor is associated.
060      private EntryCache<? extends EntryCacheCfg> entryCache;
061    
062      // Global entry cache monitor configuration.
063      private static EntryCacheMonitorProviderCfg monitorConfiguration;
064    
065      /**
066       * Creates default instance of this monitor provider.
067       */
068      public EntryCacheMonitorProvider()
069      {
070        super("Entry Caches Monitor Provider");
071        this.entryCacheName = "Entry Caches";
072        this.entryCache = (EntryCache<? extends EntryCacheCfg>)
073          DirectoryServer.getEntryCache();
074      }
075    
076      /**
077       * Creates implementation specific instance of this monitor provider.
078       *
079       * @param  entryCacheName  The name to use for this monitor provider.
080       * @param  entryCache      The entry cache to associate this monitor
081       *                         provider with.
082       */
083      public EntryCacheMonitorProvider(
084        String entryCacheName,
085        EntryCache<? extends EntryCacheCfg> entryCache)
086      {
087        super(entryCacheName + " Entry Cache Monitor Provider");
088        this.entryCacheName = entryCacheName + " Entry Cache";
089        this.entryCache = entryCache;
090      }
091    
092      /**
093       * {@inheritDoc}
094       */
095      public void initializeMonitorProvider(
096        EntryCacheMonitorProviderCfg configuration)
097        throws ConfigException
098      {
099        monitorName = entryCacheName;
100    
101        if (configuration != null) {
102          monitorConfiguration = configuration;
103        }
104        if (monitorConfiguration == null) {
105          Message message =
106            INFO_WARN_CONFIG_ENTRYCACHE_NO_MONITOR_CONFIG_ENTRY.get(
107            ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG,
108            monitorName);
109          logError(message);
110          throw new ConfigException(message);
111        }
112        if (!monitorConfiguration.isEnabled()) {
113          Message message =
114            INFO_WARN_CONFIG_ENTRYCACHE_MONITOR_CONFIG_DISABLED.get(
115            ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG,
116            monitorName);
117          logError(message);
118          throw new ConfigException(message);
119        }
120      }
121    
122      /**
123       * {@inheritDoc}
124       */
125      public String getMonitorInstanceName()
126      {
127        return monitorName;
128      }
129    
130      /**
131       * {@inheritDoc}
132       */
133      public long getUpdateInterval()
134      {
135        // This monitor does not need to run periodically.
136        return 0;
137      }
138    
139      /**
140       * {@inheritDoc}
141       */
142      public void updateMonitorData()
143      {
144        // This monitor does not need to run periodically.
145        return;
146      }
147    
148      /**
149       * {@inheritDoc}
150       */
151      public ArrayList<Attribute> getMonitorData()
152      {
153        ArrayList<Attribute> attrs = new ArrayList<Attribute>();
154    
155        if ((entryCache != null) &&
156            (monitorConfiguration != null) &&
157            (monitorConfiguration.isEnabled())) {
158          // Get monitor data from the cache.
159          attrs = entryCache.getMonitorData();
160        }
161    
162        return attrs;
163      }
164    }