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 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.monitors;
028
029import java.util.Collections;
030import java.util.List;
031
032import org.forgerock.i18n.LocalizableMessage;
033import org.forgerock.i18n.slf4j.LocalizedLogger;
034import org.opends.server.admin.std.server.EntryCacheCfg;
035import org.opends.server.admin.std.server.EntryCacheMonitorProviderCfg;
036import org.opends.server.api.EntryCache;
037import org.opends.server.api.MonitorProvider;
038import org.opends.server.config.ConfigConstants;
039import org.forgerock.opendj.config.server.ConfigException;
040import org.opends.server.core.DirectoryServer;
041import org.opends.server.types.Attribute;
042
043import 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 */
050public class EntryCacheMonitorProvider
051       extends MonitorProvider<EntryCacheMonitorProviderCfg>
052{
053
054  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
055
056  /** The name for this monitor. */
057  private String monitorName;
058
059  /** The entry cache common name. */
060  private String entryCacheName;
061
062  /** The entry cache with which this monitor is associated. */
063  private EntryCache<? extends EntryCacheCfg> entryCache;
064
065  /** Entry cache monitor configuration. */
066  private EntryCacheMonitorProviderCfg monitorConfiguration;
067
068  /**
069   * Creates default instance of this monitor provider.
070   */
071  public EntryCacheMonitorProvider()
072  {
073    this.entryCacheName = "Entry Caches";
074    this.entryCache = DirectoryServer.getEntryCache();
075  }
076
077  /**
078   * Creates implementation specific instance of this monitor provider.
079   *
080   * @param  entryCacheName  The name to use for this monitor provider.
081   * @param  entryCache      The entry cache to associate this monitor
082   *                         provider with.
083   */
084  public EntryCacheMonitorProvider(
085    String entryCacheName,
086    EntryCache<? extends EntryCacheCfg> entryCache)
087  {
088    this.entryCacheName = entryCacheName + " Entry Cache";
089    this.entryCache = entryCache;
090  }
091
092  /** {@inheritDoc} */
093  @Override
094  public void initializeMonitorProvider(
095    EntryCacheMonitorProviderCfg configuration)
096    throws ConfigException
097  {
098    monitorName = entryCacheName;
099
100    if (configuration != null) {
101      monitorConfiguration = configuration;
102    }
103    if (monitorConfiguration == null) {
104      LocalizableMessage message =
105          INFO_WARN_CONFIG_ENTRYCACHE_NO_MONITOR_CONFIG_ENTRY.get(
106              ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG, monitorName);
107      logger.debug(message);
108      throw new ConfigException(message);
109    }
110    if (!monitorConfiguration.isEnabled()) {
111      LocalizableMessage message =
112          INFO_WARN_CONFIG_ENTRYCACHE_MONITOR_CONFIG_DISABLED.get(
113              ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG, monitorName);
114      logger.debug(message);
115      throw new ConfigException(message);
116    }
117  }
118
119  /** {@inheritDoc} */
120  @Override
121  public String getMonitorInstanceName()
122  {
123    return monitorName;
124  }
125
126  /** {@inheritDoc} */
127  @Override
128  public List<Attribute> getMonitorData()
129  {
130    if (entryCache != null &&
131        monitorConfiguration != null &&
132        monitorConfiguration.isEnabled()) {
133      // Get monitor data from the cache.
134      return entryCache.getMonitorData();
135    }
136    return Collections.emptyList();
137  }
138}