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 2015 ForgeRock AS
025 */
026
027package org.opends.server.backends.pluggable.spi;
028
029import org.forgerock.i18n.LocalizableMessage;
030
031/**
032 * Represents the current status of a storage with respect to its resources.
033 */
034public final class StorageStatus
035{
036  /** Internal States. */
037  private enum Code
038  {
039    /** Storage is working normally. */
040    WORKING,
041    /** Storage resources start getting scarce. */
042    LOCKED_DOWN,
043    /** Storage has no resources to execute operations. */
044    UNUSABLE
045  }
046
047  /** Hopefully resources are always in this state. */
048  private static final StorageStatus WORKING = new StorageStatus(Code.WORKING, null);
049
050  /** Current status. */
051  private final Code code;
052  /** Current warning/error message. */
053  private final LocalizableMessage reason;
054
055  /**
056   * Returns normal state.
057   *
058   * @return normal state
059   */
060  public static StorageStatus working()
061  {
062    return WORKING;
063  }
064
065  /**
066   * Returns state for resources getting scarce.
067   *
068   * @param reason the message to forward
069   * @return state for resources getting scarce
070   */
071  public static StorageStatus lockedDown(LocalizableMessage reason)
072  {
073    return new StorageStatus(Code.LOCKED_DOWN, reason);
074  }
075
076  /**
077   * Returns state for no more resources.
078   *
079   * @param reason the message to forward
080   * @return state for no more resources
081   */
082  public static StorageStatus unusable(LocalizableMessage reason)
083  {
084    return new StorageStatus(Code.UNUSABLE, reason);
085  }
086
087  private StorageStatus(Code code, LocalizableMessage reason)
088  {
089    this.code = code;
090    this.reason = reason;
091  }
092
093  /**
094   * Returns true if resources are getting scarce.
095   *
096   * @return true if resources are getting scarce
097   */
098  public boolean isLockedDown()
099  {
100    return code == Code.LOCKED_DOWN;
101  }
102
103  /**
104   * Returns true if state is normal.
105   *
106   * @return true if state is normal
107   */
108  public boolean isWorking()
109  {
110    return code == Code.WORKING;
111  }
112
113  /**
114   * Returns true if no more resources are available.
115   *
116   * @return true if no more resources are available
117   */
118  public boolean isUnusable()
119  {
120    return code == Code.UNUSABLE;
121  }
122
123  /**
124   * Returns the error message for non working states.
125   *
126   * @return the error message for non working states
127   */
128  public LocalizableMessage getReason()
129  {
130    return reason;
131  }
132}