Class Utils


  • public class Utils
    extends Object
    This class provides some static convenience methods of different nature.
    • Method Detail

      • canUseAsPort

        public static boolean canUseAsPort​(int port)
        Returns whether the provided port is free and we can use it.
        Parameters:
        port - the port we are analyzing.
        Returns:
        true if the provided port is free and we can use it, false otherwise.
      • isPrivilegedPort

        public static boolean isPrivilegedPort​(int port)
        Returns whether the provided port is a privileged port.
        Parameters:
        port - the port we are analyzing.
        Returns:
        true if the provided port is a privileged port, false otherwise.
      • supportsOption

        public static boolean supportsOption​(String option,
                                             String javaHome,
                                             String installPath)
        Tells whether the provided java installation supports a given option or not.
        Parameters:
        javaHome - the java installation path.
        option - the java option that we want to check.
        installPath - the install path of the server.
        Returns:
        true if the provided java installation supports a given option and false otherwise.
      • getPath

        public static String getPath​(String parentPath,
                                     String relativePath)
        Returns the absolute path for the given parentPath and relativePath.
        Parameters:
        parentPath - the parent path.
        relativePath - the relative path.
        Returns:
        the absolute path for the given parentPath and relativePath.
      • getScriptPath

        public static String getScriptPath​(String script)
        Returns the String that can be used to launch an script using Runtime.exec. This method is required because in Windows the script that contain a "=" in their path must be quoted.
        Parameters:
        script - the script name
        Returns:
        the absolute path for the given parentPath and relativePath.
      • getPath

        public static String getPath​(File f)
        Returns the absolute path for the given file. It tries to get the canonical file path. If it fails it returns the string representation.
        Parameters:
        f - File to get the path
        Returns:
        the absolute path for the given file.
      • isDescendant

        public static boolean isDescendant​(File descendant,
                                           File path)
        Returns whether the first provided path is under the second path in the file system.
        Parameters:
        descendant - the descendant candidate path.
        path - the path.
        Returns:
        true if the first provided path is under the second path in the file system; false otherwise or if either of the files are null
      • fileExists

        public static boolean fileExists​(String path)
        Returns whether the provided path is a file and exists.
        Parameters:
        path - the path that we are analyzing.
        Returns:
        true if the provided path is a file and exists and false otherwise.
      • directoryExistsAndIsNotEmpty

        public static boolean directoryExistsAndIsNotEmpty​(String path)
        Returns whether the provided path is a directory, exists and is not empty.
        Parameters:
        path - the path that we are analyzing.
        Returns:
        true if the provided path is a directory, exists and is not empty false otherwise.
      • isConfigurationDn

        public static boolean isConfigurationDn​(String dnStr)
        Returns whether the provided string is a configuration DN.
        Parameters:
        dnStr - the String we are analyzing.
        Returns:
        true if the provided string is a configuration DN and false otherwise.
      • areDnsEqual

        public static boolean areDnsEqual​(String dn1,
                                          String dn2)
        Returns whether the provided strings represent the same DN.
        Parameters:
        dn1 - the first dn to compare.
        dn2 - the second dn to compare.
        Returns:
        true if the provided strings represent the same DN and false otherwise.
      • createProtectedFile

        public static void createProtectedFile​(String path,
                                               String content)
                                        throws IOException,
                                               InterruptedException
        Creates a file on the specified path with the contents of the provided String. The file is protected, so that 'others' have no access to it.
        Parameters:
        path - the path where the file will be created.
        content - the String with the contents of the file.
        Throws:
        IOException - if something goes wrong.
        InterruptedException - if there is a problem changing the permissions of the file.
      • getMessageFromCollection

        public static org.forgerock.i18n.LocalizableMessage getMessageFromCollection​(Collection<org.forgerock.i18n.LocalizableMessage> col,
                                                                                     String separator)
        This is a helper method that gets a LocalizableMessage representation of the elements in the Collection of Messages. The LocalizableMessage will display the different elements separated by the separator String.
        Parameters:
        col - the collection containing the messages.
        separator - the separator String to be used.
        Returns:
        the message representation for the collection; null if col is null
      • getDefaultServerLocation

        public static String getDefaultServerLocation()
        Returns the default server location that will be proposed to the user in the installation.
        Returns:
        the default server location that will be proposed to the user in the installation.
      • getMessage

        public static org.forgerock.i18n.LocalizableMessage getMessage​(TopologyCacheException te)
        Gets a localized representation of the provide TopologyCacheException.
        Parameters:
        te - the exception.
        Returns:
        a localized representation of the provide TopologyCacheException.
      • isCli

        public static boolean isCli()
        Returns whether this is executed from command line.
        Returns:
        true if this is executed from command line and false otherwise.
      • getMessageForException

        public static org.forgerock.i18n.LocalizableMessage getMessageForException​(IOException e)
        Returns a message object for the given IOException. The code assume that we are trying to connect to the local server.
        Parameters:
        e - the IOException.
        Returns:
        a message object for the given IOException.
      • getInstallPathFromClasspath

        public static String getInstallPathFromClasspath()
        Returns the path of the installation of the directory server. Note that this method assumes that this code is being run locally.
        Returns:
        the path of the installation of the directory server.
      • getInstancePathFromInstallPath

        public static String getInstancePathFromInstallPath​(String installPath)
        Returns the path of the installation of the directory server. Note that this method assumes that this code is being run locally.
        Parameters:
        installPath - The installation path
        Returns:
        the path of the installation of the directory server.
      • getCommandLineMaxLineWidth

        public static int getCommandLineMaxLineWidth()
        Returns the max size in character of a line to be displayed in the command line.
        Returns:
        the max size in character of a line to be displayed in the command line.
      • setMacOSXMenuBar

        public static void setMacOSXMenuBar​(org.forgerock.i18n.LocalizableMessage appName)
        Puts Swing menus in the Mac OS menu bar, if using the Aqua look and feel, and sets the application name that is displayed in the application menu and in the dock.
        Parameters:
        appName - application name to display in the menu bar and the dock.
      • breakHtmlString

        public static String breakHtmlString​(CharSequence cs,
                                             int maxll)
        Inserts HTML break tags into d breaking it up so that ideally no line is longer than maxll assuming no single word is longer then maxll. If the string already contains HTML tags that cause a line break (e.g break and closing list item tags) they are respected by this method when calculating where to place new breaks to control the maximum line length.
        Parameters:
        cs - String to break
        maxll - int maximum line length
        Returns:
        String representing d with HTML break tags inserted
      • getEmptyPrintStream

        public static org.opends.quicksetup.util.EmptyPrintStream getEmptyPrintStream()
        Returns a printstream that does not write anything to standard output.
        Returns:
        a printstream that does not write anything to standard output.
      • getServerClock

        public static long getServerClock​(ConnectionWrapper conn)
        Returns the current time of a server in milliseconds.
        Parameters:
        conn - the connection to the server.
        Returns:
        the current time of a server in milliseconds.
      • isLocalHost

        public static boolean isLocalHost​(String host)
        Basic method to know if the host is local or not. This is only used to know if we can perform a port check or not.
        Parameters:
        host - the host to analyze.
        Returns:
        true if it is the local host and false otherwise.
      • getHtml

        public static String getHtml​(String text)
        Returns the HTML representation for a given text. without adding any kind of font or style elements. Just escapes the problematic characters (like '<') and transform the break lines into '\n' characters.
        Parameters:
        text - the source text from which we want to get the HTML representation
        Returns:
        the HTML representation for the given text.
      • getCustomizedObject

        public static <T> T getCustomizedObject​(String fieldName,
                                                T defaultValue,
                                                Class<T> valueClass)
        Tries to find a customized object in the customization class. If the customization class does not exist or it does not contain the field as the specified type of the object, returns the default value.
        Type Parameters:
        T - the type of the customized object.
        Parameters:
        fieldName - the name of the field representing an object in the customization class.
        defaultValue - the default value.
        valueClass - the class of the parameterized value.
        Returns:
        the customized object.
      • addWordBreaks

        public static String addWordBreaks​(String htmlString,
                                           int from,
                                           int spacing)
        Adds word break tags to the provided html string.
        Parameters:
        htmlString - the string.
        from - the first index to start the spacing from.
        spacing - the minimal spacing between word breaks.
        Returns:
        a string containing word breaks.
      • getDataDisplayString

        public static String getDataDisplayString​(UserData userInstallData)
        Returns the localized string describing the DataOptions chosen by the user.
        Parameters:
        userInstallData - the DataOptions of the user.
        Returns:
        the localized string describing the DataOptions chosen by the user.
      • getSecurityOptionsString

        public static String getSecurityOptionsString​(SecurityOptions ops,
                                                      boolean html)
        Returns a localized String representation of the provided SecurityOptions object.
        Parameters:
        ops - the SecurityOptions object from which we want to obtain the String representation.
        html - whether the resulting String must be in HTML or not.
        Returns:
        a localized String representation of the provided SecurityOptions object.
      • getFormattedEquivalentCommandLine

        public static String getFormattedEquivalentCommandLine​(List<String> cmd,
                                                               ProgressMessageFormatter formatter)
        Returns a String representation of the provided command-line.
        Parameters:
        cmd - the command-line arguments.
        formatter - the formatted to be used to create the String representation.
        Returns:
        a String representation of the provided command-line.
      • escapeCommandLineValue

        public static String escapeCommandLineValue​(String value)
        This method simply takes a value and tries to transform it (with escape or '"') characters so that it can be used in a command line.
        Parameters:
        value - the String to be treated.
        Returns:
        the transformed value.
      • getSetupEquivalentCommandLine

        public static List<String> getSetupEquivalentCommandLine​(UserData userData)
        Returns the equivalent setup CLI command-line. Note that this command-line does not cover all the replication part of the GUI install.
        Parameters:
        userData - the user data.
        Returns:
        the equivalent setup command-line.
      • getDsReplicationEquivalentCommandLines

        public static List<List<String>> getDsReplicationEquivalentCommandLines​(String subcommand,
                                                                                UserData userData)
        Returns the list of equivalent command-lines that must be executed to enable or initialize replication as the setup does.
        Parameters:
        subcommand - either "enable" or "initialize"
        userData - the user data.
        Returns:
        the list of equivalent command-lines that must be executed to enable or initialize replication as the setup does.
      • getDsConfigReplicationEnableEquivalentCommandLines

        public static List<List<String>> getDsConfigReplicationEnableEquivalentCommandLines​(UserData userData)
        Returns the equivalent dsconfig command-line required to configure the first replicated server in the topology.
        Parameters:
        userData - the user data.
        Returns:
        the equivalent dsconfig command-line required to configure the first replicated server in the topology.
      • parseIntOrDefault

        public static int parseIntOrDefault​(String value,
                                            int defaultValue)
        Returns the number held by the provided field value, or the provided default value if it does not hold a number.

        The panels validate their fields before using their values, so the default value is only returned when a field has not been validated beforehand.

        Parameters:
        value - the value to be parsed, which may be null
        defaultValue - the value to return when value does not hold a number
        Returns:
        the number held by the provided value