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 2006-2008 Sun Microsystems, Inc.
025 *      Portions copyright 2012-2013 ForgeRock AS.
026 */
027
028package org.opends.quicksetup;
029
030import static org.opends.server.util.ServerConstants.SERVER_LOCK_FILE_NAME;
031import static org.opends.server.util.ServerConstants.LOCK_FILE_SUFFIX;
032import org.opends.server.core.LockFileManager;
033import org.opends.quicksetup.util.Utils;
034
035import java.io.File;
036
037/**
038 * This class represents the current state of a particular installation.
039 */
040public class Status {
041
042  private Installation installation;
043
044  /**
045   * Creates a status instance of the installation indicated by the
046   * input parameter.
047   * @param installation physical installation
048   */
049  public Status(Installation installation) {
050    this.installation = installation;
051  }
052
053  /**
054   * Returns if the server is running on the given path.
055   * NOTE: this method is to be called only when the OpenDS.jar class has
056   * already been loaded as it uses classes in that jar.
057   *
058   * LIMITATIONS:
059   * If the locks directory does not exist the mechanism fails if the server is
060   * stopped.  However if the server.lock does not exist AND the server is not
061   * running the mechanism should work most of the times (see failing case 3).
062   *
063   * The cases where this mechanism does not work are:
064   *
065   * 1. The user deletes/renames the locks directory.
066   * 2. The user deletes/renames the server.lock file AND the server is running.
067   * 3. The server is not running but the user that is running the code does not
068   * have file system access rights.
069   * 4. The server is not running and another process has a lock on the file.
070   * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE>
071   * otherwise.
072   */
073  public boolean isServerRunning() {
074    boolean isServerRunning;
075    String lockFileName = SERVER_LOCK_FILE_NAME + LOCK_FILE_SUFFIX;
076    String lockFile =
077            Utils.getPath(new File(installation.getLocksDirectory(),
078                                   lockFileName));
079    StringBuilder failureReason = new StringBuilder();
080    try {
081      if (LockFileManager.acquireExclusiveLock(lockFile,
082              failureReason)) {
083        LockFileManager.releaseLock(lockFile,
084                failureReason);
085        isServerRunning = false;
086      } else {
087        isServerRunning = true;
088      }
089    }
090    catch (Throwable t) {
091      // Assume that if we cannot acquire the lock file the
092      // server is running.
093      isServerRunning = true;
094    }
095    return isServerRunning;
096  }
097}