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