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