mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

jvergara
28.35.2007 856bfa759bec64b3cfc710fa26b755855bcc61c8
Fix for issue 1285.

The problem was caused because in the class InstallDS the check for the port was not performed when the user already provided the port through the command line (see first diffs in InstallDS).

The rest of the diffs consist of factorization of code to be shared between the quicksetup classes and the class org.opends.server.tools.InstallDS. Some methods from the Utils class of quicksetup have been moved to the SetupUtils class; these methods allow to know if a given port is priviledged or not, if we can use it to configure the server and to know in which Operating System the JVM is running. The operating system methods use the enumeration OperatingSystem (which has been slightly modified to not have dependencies on other classes and to be included in quicksetup.jar).

Extra: use the class LockFileManager to know if the server is running or not. The use of this class does not impact the Web Start Installer as we only want to know if the server is running or not once we have tried to start it (and so OpenDS.jar has already been loaded by the class loader).
11 files modified
393 ■■■■ changed files
opends/build.xml 4 ●●●● patch | view | raw | blame | history
opends/src/quicksetup/org/opends/quicksetup/CurrentInstallStatus.java 38 ●●●● patch | view | raw | blame | history
opends/src/quicksetup/org/opends/quicksetup/uninstaller/Uninstaller.java 4 ●●●● patch | view | raw | blame | history
opends/src/quicksetup/org/opends/quicksetup/util/Utils.java 135 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/InstallDS.java 73 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/types/OperatingSystem.java 15 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/util/SetupUtils.java 105 ●●●●● patch | view | raw | blame | history
opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java 3 ●●●● patch | view | raw | blame | history
opends/src/statuspanel/org/opends/statuspanel/StatusCli.java 6 ●●●● patch | view | raw | blame | history
opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java 7 ●●●●● patch | view | raw | blame | history
opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java 3 ●●●● patch | view | raw | blame | history
opends/build.xml
@@ -417,7 +417,7 @@
    </copy>
    <copy todir="${quicksetup.classes.dir}">
      <fileset dir="${classes.dir}"
               includes="**/DynamicConstants.class **/SetupUtils.class" />
               includes="**/DynamicConstants.class **/SetupUtils.class **/OperatingSystem.class" />
    </copy>
  </target>
@@ -516,7 +516,7 @@
    <copy todir="${quicksetup.classes.dir}">
        <fileset dir="${classes.dir}"
            includes="**/DynamicConstants.class **/SetupUtils.class"/>
            includes="**/DynamicConstants.class **/SetupUtils.class **/OperatingSystem.class"/>
    </copy>
    <!-- Generate the quicksetup.jar file -->
opends/src/quicksetup/org/opends/quicksetup/CurrentInstallStatus.java
@@ -213,14 +213,40 @@
  }
  /**
   * Indicates whether there is the server is running.
   *
   * @return <CODE>true</CODE> if the server is running, or <CODE>false</CODE>
   *         if not.
   * Returns if the server is running on the given path.
   * NOTE: this method is to be called only when the OpenDS.jar class has
   * already been loaded as it uses classes in that jar.
   * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE>
   * otherwise.
   */
  public boolean isServerRunning()
  public static boolean isServerRunning()
  {
    return Utils.isServerRunning(Utils.getInstallPathFromClasspath());
    boolean isServerRunning;
    String lockFile =
      org.opends.server.core.LockFileManager.getServerLockFileName();
    StringBuilder failureReason = new StringBuilder();
    try
    {
      if (org.opends.server.core.LockFileManager.acquireExclusiveLock(lockFile,
          failureReason))
      {
        org.opends.server.core.LockFileManager.releaseLock(lockFile,
            failureReason);
        isServerRunning = false;
      }
      else
      {
        isServerRunning = true;
      }
    }
    catch (Throwable t)
    {
      t.printStackTrace();
      // Assume that if we cannot acquire the lock file the server is
      // running.
      isServerRunning = true;
    }
    return isServerRunning;
  }
  /**
opends/src/quicksetup/org/opends/quicksetup/uninstaller/Uninstaller.java
@@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Set;
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.event.UninstallProgressUpdateEvent;
import org.opends.quicksetup.event.UninstallProgressUpdateListener;
import org.opends.quicksetup.i18n.ResourceProvider;
@@ -686,8 +687,7 @@
          for (int i=0; i<nTries && !stopped; i++)
          {
            stopped = !Utils.isServerRunning(
                Utils.getInstallPathFromClasspath());
            stopped = !CurrentInstallStatus.isServerRunning();
            if (!stopped)
            {
              String msg =
opends/src/quicksetup/org/opends/quicksetup/util/Utils.java
@@ -31,7 +31,6 @@
import java.awt.Toolkit;
import java.awt.Window;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
@@ -39,11 +38,7 @@
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -62,6 +57,7 @@
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.i18n.ResourceProvider;
import org.opends.quicksetup.installer.webstart.JnlpProperties;
import org.opends.server.util.SetupUtils;
/**
@@ -229,51 +225,7 @@
   */
  public static boolean canUseAsPort(int port)
  {
    boolean canUseAsPort = false;
    ServerSocket serverSocket = null;
    try
    {
      InetSocketAddress socketAddress = new InetSocketAddress(port);
      serverSocket = new ServerSocket();
      if (!isWindows())
      {
        serverSocket.setReuseAddress(true);
      }
      serverSocket.bind(socketAddress);
      canUseAsPort = true;
      serverSocket.close();
      /* Try to create a socket because sometimes even if we can create a server
       * socket there is already someone listening to the port (is the case
       * of products as Sun DS 6.0).
       */
      try
      {
        new Socket("localhost", port);
        canUseAsPort = false;
      } catch (IOException ioe)
      {
      }
    } catch (IOException ex)
    {
      canUseAsPort = false;
    } finally
    {
      try
      {
        if (serverSocket != null)
        {
          serverSocket.close();
        }
      } catch (Exception ex)
      {
      }
    }
    return canUseAsPort;
    return SetupUtils.canUseAsPort(port);
  }
  /**
@@ -285,7 +237,7 @@
   */
  public static boolean isPriviledgedPort(int port)
  {
    return (port <= 1024) && !isWindows();
    return SetupUtils.isPriviledgedPort(port);
  }
  /**
@@ -369,7 +321,7 @@
   */
  public static boolean isWindows()
  {
    return containsOsProperty("windows");
    return SetupUtils.isWindows();
  }
  /**
@@ -380,7 +332,7 @@
   */
  public static boolean isMacOS()
  {
    return containsOsProperty("mac os");
    return SetupUtils.isMacOS();
  }
  /**
@@ -391,7 +343,7 @@
   */
  public static boolean isUnix()
  {
    return !isWindows();
    return SetupUtils.isUnix();
  }
  /**
@@ -773,25 +725,6 @@
  }
  /**
   * Commodity method to help identifying the OS we are running on.
   * @param s the String that represents an OS.
   * @return <CODE>true</CODE> if there is os java property exists and contains
   * the value specified in s, <CODE>false</CODE> otherwise.
   */
  private static boolean containsOsProperty(String s)
  {
    boolean containsOsProperty = false;
    String osName = System.getProperty("os.name");
    if (osName != null)
    {
      containsOsProperty = osName.toLowerCase().indexOf(s) != -1;
    }
    return containsOsProperty;
  }
  /**
   * Sets the permissions of the provided paths with the provided permission
   * String.
   * @param paths the paths to set permissions on.
@@ -1362,62 +1295,6 @@
  }
  /**
   * Returns if the server is running on the given path.
   * @param serverPath the installation path of the server.
   * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE>
   * otherwise.
   */
  public static boolean isServerRunning(String serverPath)
  {
    boolean isServerRunning;
    if (isWindows())
    {
      String testPath = serverPath+File.separator+
      "locks"+File.separator+"server.lock";
      File testFile = new File(testPath);
      boolean canWriteFile = false;
      Writer output = null;
      try {
        //use buffering
        //FileWriter always assumes default encoding is OK!
        output = new BufferedWriter( new FileWriter(testFile) );
        output.write("test");
        output.close();
        output = new BufferedWriter( new FileWriter(testFile) );
        output.write("");
        output.close();
        canWriteFile = true;
      }
      catch (Throwable t)
      {
      }
      finally
      {
        if (output != null)
        {
          try
          {
            output.close();
          }
          catch (Throwable t)
          {
          }
        }
      }
      isServerRunning = !canWriteFile;
    }
    else
    {
      isServerRunning = fileExists(serverPath+File.separator+
          "logs"+File.separator+"server.pid");
    }
    return isServerRunning;
  }
  /**
   * This is just a commodity method used to try to get an InitialLdapContext.
   * @param t the Thread to be used to create the InitialLdapContext.
   * @param pair an Object[] array that contains the InitialLdapContext and the
opends/src/server/org/opends/server/tools/InstallDS.java
@@ -30,9 +30,6 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
@@ -424,6 +421,29 @@
      try
      {
        ldapPortNumber = ldapPort.getIntValue();
        if (! skipPortCheck.isPresent())
        {
          // Check if the port can be used.
          if (!SetupUtils.canUseAsPort(ldapPortNumber))
          {
            int msgID;
            String message;
            if (SetupUtils.isPriviledgedPort(ldapPortNumber))
            {
              msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PRIVILEGED_PORT;
              message = getMessage(msgID, ldapPortNumber);
              System.err.println(wrapText(message, MAX_LINE_WIDTH));
            }
            else
            {
              msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PORT;
              message = getMessage(msgID, ldapPortNumber);
              System.err.println(wrapText(message, MAX_LINE_WIDTH));
            }
            return 1;
          }
        }
      }
      catch (ArgumentException ae)
      {
@@ -439,45 +459,20 @@
        String message = getMessage(msgID);
        ldapPortNumber = promptForInteger(message, 389, 1, 65535);
        if (! skipPortCheck.isPresent())
        if (skipPortCheck.isPresent())
        {
          try
          {
            InetSocketAddress socketAddress =
                                   new InetSocketAddress(ldapPortNumber);
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(socketAddress);
            serverSocket.close();
            try
            {
              Socket socket = new Socket("127.0.0.1", ldapPortNumber);
              socket.close();
              if ((ldapPortNumber <= 1024) && (! isWindows))
              {
                msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PRIVILEGED_PORT;
                message = getMessage(msgID, ldapPortNumber);
                System.err.println(wrapText(message, MAX_LINE_WIDTH));
              }
              else
              {
                msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PORT;
                message = getMessage(msgID, ldapPortNumber);
                System.err.println(wrapText(message, MAX_LINE_WIDTH));
              }
              continue;
            }
            catch (Exception e)
            {
              // This is expected, so no action should be taken.
              break;
            }
          }
          catch (Exception e)
        else
          {
            if ((ldapPortNumber <= 1024) && (! isWindows))
          // Check if the port can be used.
          if (SetupUtils.canUseAsPort(ldapPortNumber))
          {
              break;
          }
          else
          {
            if (SetupUtils.isPriviledgedPort(ldapPortNumber))
            {
              msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PRIVILEGED_PORT;
              message = getMessage(msgID, ldapPortNumber);
@@ -489,8 +484,6 @@
              message = getMessage(msgID, ldapPortNumber);
              System.err.println(wrapText(message, MAX_LINE_WIDTH));
            }
            continue;
          }
        }
      }
opends/src/server/org/opends/server/types/OperatingSystem.java
@@ -26,15 +26,16 @@
 */
package org.opends.server.types;
import static org.opends.server.util.StaticUtils.*;
/**
 * This class defines an enumeration that may be used to identify
 * the operating system on which the JVM is running.
 *
 * NOTE: to share code this class is used in SetupUtils and should
 * not contain any dependency with other classes (not even with
 * classes in this package).
 * If this class is modified to depend on other classes it will break
 * the quicksetup.  If this must be done, the references to this
 * class in SetupUtils must be removed.
 */
public enum OperatingSystem
{
@@ -148,7 +149,7 @@
    }
    String lowerName = toLowerCase(osName);
    String lowerName = osName.toLowerCase();
    if ((lowerName.indexOf("solaris") >= 0) ||
        (lowerName.indexOf("sunos") >= 0))
opends/src/server/org/opends/server/util/SetupUtils.java
@@ -32,8 +32,12 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import org.opends.server.types.OperatingSystem;
/**
@@ -116,7 +120,27 @@
    return templateFile;
  }
  /**
   * Returns {@code true} if we are running under Mac OS and
   * {@code false} otherwise.
   * @return {@code true} if we are running under Mac OS and
   * {@code false} otherwise.
   */
  public static boolean isMacOS()
  {
    return OperatingSystem.MACOS == getOperatingSystem();
  }
  /**
   * Returns {@code true} if we are running under Unix and
   * {@code false} otherwise.
   * @return {@code true} if we are running under Unix and
   * {@code false} otherwise.
   */
  public static boolean isUnix()
  {
    return OperatingSystem.isUNIXBased(getOperatingSystem());
  }
  /**
   * Indicates whether the underlying operating system is a Windows variant.
@@ -126,8 +150,17 @@
   */
  public static boolean isWindows()
  {
    String osName = System.getProperty("os.name");
    return ((osName != null) && (osName.toLowerCase().indexOf("windows") >= 0));
      return OperatingSystem.WINDOWS == getOperatingSystem();
  }
  /**
   * Commodity method to help identifying the OS we are running on.
   * @return the OperatingSystem we are running on.
   */
  private static OperatingSystem getOperatingSystem()
  {
    return OperatingSystem.forName(System.getProperty("os.name"));
  }
@@ -200,5 +233,73 @@
    return setJavaHomeFile;
  }
  /**
   * Returns {@code true} if the provided port is free and we can use it,
   * {@code false} otherwise.
   * @param port the port we are analyzing.
   * @return {@code true} if the provided port is free and we can use it,
   * {@code false} otherwise.
   */
  public static boolean canUseAsPort(int port)
  {
    boolean canUseAsPort = false;
    ServerSocket serverSocket = null;
    try
    {
      InetSocketAddress socketAddress = new InetSocketAddress(port);
      serverSocket = new ServerSocket();
      if (!isWindows())
      {
        serverSocket.setReuseAddress(true);
      }
      serverSocket.bind(socketAddress);
      canUseAsPort = true;
      serverSocket.close();
      /* Try to create a socket because sometimes even if we can create a server
       * socket there is already someone listening to the port (is the case
       * of products as Sun DS 6.0).
       */
      try
      {
        new Socket("localhost", port);
        canUseAsPort = false;
      } catch (IOException ioe)
      {
      }
    } catch (IOException ex)
    {
      canUseAsPort = false;
    } finally
    {
      try
      {
        if (serverSocket != null)
        {
          serverSocket.close();
        }
      } catch (Exception ex)
      {
      }
    }
    return canUseAsPort;
  }
  /**
   * Returns {@code true} if the provided port is a priviledged port,
   * {@code false} otherwise.
   * @param port the port we are analyzing.
   * @return {@code true} if the provided port is a priviledged port,
   * {@code false} otherwise.
   */
  public static boolean isPriviledgedPort(int port)
  {
    return (port <= 1024) && !isWindows();
  }
}
opends/src/statuspanel/org/opends/statuspanel/ServerStatusPooler.java
@@ -30,6 +30,7 @@
import java.io.File;
import java.util.HashSet;
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.util.Utils;
import org.opends.statuspanel.event.ServerStatusChangeEvent;
import org.opends.statuspanel.event.ServerStatusChangeListener;
@@ -280,7 +281,7 @@
    {
      desc.setStatus(ServerStatusDescriptor.ServerStatus.STOPPING);
    }
    else if (Utils.isServerRunning(Utils.getInstallPathFromClasspath()))
    else if (CurrentInstallStatus.isServerRunning())
    {
      desc.setStatus(ServerStatusDescriptor.ServerStatus.STARTED);
    }
opends/src/statuspanel/org/opends/statuspanel/StatusCli.java
@@ -37,6 +37,7 @@
import javax.swing.table.TableModel;
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.util.Utils;
import org.opends.server.core.DirectoryServer;
@@ -231,8 +232,7 @@
    }
    else
    {
      boolean isServerRunning =
        Utils.isServerRunning(Utils.getInstallPathFromClasspath());
      boolean isServerRunning = CurrentInstallStatus.isServerRunning();
      /* This is required to retrieve the ldap url to be used by the
       * ConfigFromLDAP class.
       */
@@ -355,7 +355,7 @@
    ServerStatusDescriptor desc = new ServerStatusDescriptor();
    desc.setAuthenticated((dn != null) && (pwd != null));
    if (Utils.isServerRunning(Utils.getInstallPathFromClasspath()))
    if (CurrentInstallStatus.isServerRunning())
    {
      desc.setStatus(ServerStatusDescriptor.ServerStatus.STARTED);
    }
opends/src/statuspanel/org/opends/statuspanel/StatusPanelController.java
@@ -39,6 +39,7 @@
import org.opends.server.core.DirectoryServer;
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.util.BackgroundTask;
import org.opends.quicksetup.util.HtmlProgressMessageFormatter;
@@ -581,8 +582,7 @@
        boolean running = false;
        for (int i=0; i<5 && !running; i++)
        {
          running = Utils.isServerRunning(
              Utils.getInstallPathFromClasspath());
          running = CurrentInstallStatus.isServerRunning();
        }
        if (!running)
@@ -706,8 +706,7 @@
          int nTries = 10;
          for (int i=0; i<nTries && !stopped; i++)
          {
            stopped = !Utils.isServerRunning(
                Utils.getInstallPathFromClasspath());
            stopped = !CurrentInstallStatus.isServerRunning();
            if (!stopped)
            {
              String msg =
opends/src/statuspanel/org/opends/statuspanel/ui/LoginDialog.java
@@ -48,6 +48,7 @@
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import org.opends.quicksetup.CurrentInstallStatus;
import org.opends.quicksetup.event.MinimumSizeComponentListener;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.util.BackgroundTask;
@@ -535,7 +536,7 @@
   */
  private boolean isServerRunning()
  {
    return Utils.isServerRunning(Utils.getInstallPathFromClasspath());
    return CurrentInstallStatus.isServerRunning();
  }
  /**