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 2008-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.monitors;
018
019import static org.opends.messages.ConfigMessages.*;
020
021import org.forgerock.i18n.LocalizableMessage;
022import org.forgerock.i18n.slf4j.LocalizedLogger;
023import org.forgerock.opendj.config.server.ConfigException;
024import org.forgerock.opendj.server.config.server.EntryCacheCfg;
025import org.forgerock.opendj.server.config.server.EntryCacheMonitorProviderCfg;
026import org.opends.server.api.EntryCache;
027import org.opends.server.api.MonitorData;
028import org.opends.server.api.MonitorProvider;
029import org.opends.server.config.ConfigConstants;
030import org.opends.server.core.DirectoryServer;
031
032/**
033 * This class defines a Directory Server monitor provider that can be used to
034 * obtain information about the entry cache state. Note that the information
035 * reported is obtained with no locking, so it may not be entirely consistent.
036 */
037public class EntryCacheMonitorProvider
038       extends MonitorProvider<EntryCacheMonitorProviderCfg>
039{
040
041  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
042
043  /** The name for this monitor. */
044  private String monitorName;
045
046  /** The entry cache common name. */
047  private String entryCacheName;
048
049  /** The entry cache with which this monitor is associated. */
050  private EntryCache<? extends EntryCacheCfg> entryCache;
051
052  /** Entry cache monitor configuration. */
053  private EntryCacheMonitorProviderCfg monitorConfiguration;
054
055  /**
056   * Creates default instance of this monitor provider.
057   */
058  public EntryCacheMonitorProvider()
059  {
060    this.entryCacheName = "Entry Caches";
061    this.entryCache = DirectoryServer.getEntryCache();
062  }
063
064  /**
065   * Creates implementation specific instance of this monitor provider.
066   *
067   * @param  entryCacheName  The name to use for this monitor provider.
068   * @param  entryCache      The entry cache to associate this monitor
069   *                         provider with.
070   */
071  public EntryCacheMonitorProvider(
072    String entryCacheName,
073    EntryCache<? extends EntryCacheCfg> entryCache)
074  {
075    this.entryCacheName = entryCacheName + " Entry Cache";
076    this.entryCache = entryCache;
077  }
078
079  /** {@inheritDoc} */
080  @Override
081  public void initializeMonitorProvider(
082    EntryCacheMonitorProviderCfg configuration)
083    throws ConfigException
084  {
085    monitorName = entryCacheName;
086
087    if (configuration != null) {
088      monitorConfiguration = configuration;
089    }
090    if (monitorConfiguration == null) {
091      LocalizableMessage message =
092          INFO_WARN_CONFIG_ENTRYCACHE_NO_MONITOR_CONFIG_ENTRY.get(
093              ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG, monitorName);
094      logger.debug(message);
095      throw new ConfigException(message);
096    }
097    if (!monitorConfiguration.isEnabled()) {
098      LocalizableMessage message =
099          INFO_WARN_CONFIG_ENTRYCACHE_MONITOR_CONFIG_DISABLED.get(
100              ConfigConstants.DN_ENTRY_CACHE_MONITOR_CONFIG, monitorName);
101      logger.debug(message);
102      throw new ConfigException(message);
103    }
104  }
105
106  @Override
107  public String getMonitorInstanceName()
108  {
109    return monitorName;
110  }
111
112  @Override
113  public MonitorData getMonitorData()
114  {
115    if (entryCache != null &&
116        monitorConfiguration != null &&
117        monitorConfiguration.isEnabled()) {
118      // Get monitor data from the cache.
119      return entryCache.getMonitorData();
120    }
121    return new MonitorData(0);
122  }
123}