Class StaticUtils


  • @PublicAPI(stability=UNCOMMITTED,
               mayInstantiate=false,
               mayExtend=false,
               mayInvoke=true)
    public final class StaticUtils
    extends Object
    This class defines a number of static utility methods that may be used throughout the server. Note that because of the frequency with which these methods are expected to be used, very little debug logging will be performed to prevent the log from filling up with unimportant calls and to reduce the impact that debugging may have on performance.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE
      Size of buffer used to copy/move/extract files.
      static int INT_SIZE
      The number of bytes of a Java int.
      static int KB
      Number of bytes in a Kibibyte.
      static int LONG_SIZE
      The number of bytes of a Java long.
      static int MB
      Number of bytes in a Mebibyte.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addSuperiorObjectClasses​(Map<ObjectClass,​String> objectClasses)
      Add all of the superior objectclasses to the specified objectclass map if they don't already exist.
      static ArrayList<String> arrayToList​(String... stringArray)
      Retrieves an array list containing the contents of the provided array.
      static void byteArrayToHexPlusAscii​(StringBuilder buffer, byte[] b, int indent)
      Appends a string representation of the provided byte array to the given buffer using the specified indent.
      static String bytesToColonDelimitedHex​(byte[] b)
      Retrieves a string representation of the contents of the provided byte array using hexadecimal characters and a colon between each byte.
      static String bytesToHex​(byte[] b)
      Retrieves a string representation of the contents of the provided byte array using hexadecimal characters and a space between each byte.
      static String bytesToHex​(ByteBuffer b)
      Retrieves a string representation of the contents of the provided byte buffer using hexadecimal characters and a space between each byte.
      static String bytesToHex​(ByteSequence b)
      Retrieves a string representation of the contents of the provided byte sequence using hexadecimal characters and a space between each byte.
      static String bytesToHexNoSpace​(byte[] b)
      Retrieves a string representation of the contents of the provided byte array using hexadecimal characters with no space between each byte.
      static String byteToHex​(byte b)
      Retrieves a string representation of the provided byte in hexadecimal.
      static String byteToLowerHex​(byte b)
      Retrieves a string representation of the provided byte in hexadecimal.
      static void checkOnlyOneArgPresent​(Argument... args)
      Checks that no more that one of a set of arguments is present.
      static void close​(Closeable... closeables)
      Closes the provided Closeable's ignoring any errors which occurred.
      static void close​(Collection<? extends Closeable> closeables)
      Closes the provided Closeable's ignoring any errors which occurred.
      static int compare​(byte[] a, byte[] a2)
      Compare two byte arrays for order.
      static Entry createEntry​(DN dn)
      Creates a new, blank entry with the given DN.
      static String decodeUTF8​(byte[] bytes)
      Returns the provided byte array decoded as a UTF-8 string without throwing an UnsupportedEncodingException.
      static boolean deleteFiles​(Iterable<File> files)
      Deletes all provided files.
      static void extractZipArchive​(File zipFile, File targetDirectory, List<String> executableDirectories, List<String> executableFiles)
      Extracts the provided zip archive to the provided target directory.
      static String formatDateTimeString​(Date date)
      Formats a Date to String representation in "yyyyMMddHHmmss'Z'".
      static String getBacktrace()
      Retrieves a backtrace for the current thread consisting only of filenames and line numbers that may be useful in debugging the origin of problems that should not have happened.
      static String getBacktrace​(Throwable t)
      Retrieves a backtrace for the provided exception consisting of only filenames and line numbers that may be useful in debugging the origin of problems.
      static byte[] getBytes​(String s)
      Construct a byte array containing the UTF-8 encoding of the provided string.
      static org.forgerock.i18n.LocalizableMessage getExceptionMessage​(Throwable t)
      Retrieves the best human-readable message for the provided exception.
      static File getFileForPath​(String path)
      Retrieves a File object corresponding to the specified path.
      static File getFileForPath​(String path, ServerContext serverContext)
      Retrieves a File object corresponding to the specified path.
      static List<String> getFileNames​(File[] files)
      Returns the sorted list of names of provided files.
      static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg, org.forgerock.i18n.LocalizableMessageDescriptor.Arg0 desc)
      Test if the provided message corresponds to the provided descriptor.
      static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg, org.forgerock.i18n.LocalizableMessageDescriptor.Arg1<?> desc)
      Test if the provided message corresponds to the provided descriptor.
      static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg, org.forgerock.i18n.LocalizableMessageDescriptor.Arg3<?,​?,​?> desc)
      Test if the provided message corresponds to the provided descriptor.
      static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg, org.forgerock.i18n.LocalizableMessageDescriptor.Arg7<?,​?,​?,​?,​?,​?,​?> desc)
      Test if the provided message corresponds to the provided descriptor.
      static byte[] hexStringToByteArray​(String hexString)
      Converts the provided hexadecimal string to a byte array.
      static boolean isAddressInUse​(InetAddress address, int port, boolean allowReuse)
      Indicates whether the provided TCP address is already in use.
      static boolean isAlpha​(char c)
      Indicates whether the provided character is an ASCII alphabetic character.
      static boolean isClassAvailable​(String className)
      Returns true if the class is available in the classpath.
      static boolean isDigit​(char c)
      Indicates whether the provided character is a numeric digit.
      static boolean isEmailAddress​(String addr)
      Indicates whether a string represents a syntactically correct email address.
      static boolean isHexDigit​(byte b)
      Indicates whether the provided byte represents a hexadecimal digit.
      static boolean isHexDigit​(char c)
      Indicates whether the provided character is a hexadecimal digit.
      static boolean isOEMVersion()
      Returns true if the version of the server is an OEM one, and therefore doesn't support the JE backend.
      static boolean isSelfConnection​(Socket socket)
      Indicates whether the provided connected socket is connected to itself, i.e. its local and remote endpoints are identical.
      static boolean isValidSchemaElement​(String element, int startPos, int endPos, org.forgerock.i18n.LocalizableMessageBuilder invalidReason)
      Indicates whether the provided string contains a name or OID for a schema element like an attribute type or objectclass.
      static void moveFile​(File fileToMove, File targetDirectory)
      Moves the indicated file to the specified directory by creating a new file in the target directory, copying the contents of the existing file, and removing the existing file.
      static boolean needsBase64Encoding​(String valueString)
      Indicates whether the provided value needs to be base64-encoded if it is represented in LDIF form.
      static boolean needsBase64Encoding​(ByteSequence valueBytes)
      Indicates whether the provided value needs to be base64-encoded if it is represented in LDIF form.
      static Date parseDateTimeString​(String timeStr)
      Converts a string representing a time in "yyyyMMddHHmmss.SSS'Z'" or "yyyyMMddHHmmss" to a Date.
      static boolean recursiveDelete​(File file)
      Attempts to delete the specified file or directory.
      static void renameFile​(File fileToRename, File target)
      Renames the source file to the target file.
      static org.forgerock.i18n.LocalizableMessage secondsToTimeString​(long numSeconds)
      Retrieves a user-friendly string that indicates the length of time (in days, hours, minutes, and seconds) in the specified number of seconds.
      static void sleep​(long millis)
      Calls Thread.sleep(long), surrounding it with the mandatory try / catch(InterruptedException) block.
      static List<String> splittedStringAsList​(String str, String delim)  
      static boolean stackTraceContainsCause​(Throwable throwable, Class<? extends Throwable> searchedCause)
      Check if the stack trace of provided exception contains a given cause.
      static String stackTraceToSingleLineString​(Throwable t)
      Retrieves a stack trace from the provided exception as a single-line string.
      static String stackTraceToString​(Throwable t)
      Retrieves a string representation of the stack trace for the provided exception.
      static <T> Iterable<T> toIterable​(Iterator<T> iterator)
      Returns an Iterable returning the passed in Iterator.
      static String toLowerCase​(String s)
      Returns a lower-case string representation of a given string, verifying for null input string.
      static void toLowerCase​(ByteSequence b, StringBuilder buffer, boolean trim)
      Appends a lower-case string representation of a given ByteSequence to a StringBuilder, verifying for null input.
      static StringBuilder toRFC3641StringValue​(StringBuilder builder, String string)
      Append a string to a string builder, escaping any double quotes according to the StringValue production in RFC 3641.
      static void toUpperCase​(byte[] b, StringBuilder buffer, boolean trim)
      Appends an uppercase string representation of the contents of the given byte array to the provided buffer, optionally trimming leading and trailing spaces.
      static String toUpperCase​(String s)
      Retrieves an uppercase representation of the given string.
    • Field Detail

      • INT_SIZE

        public static final int INT_SIZE
        The number of bytes of a Java int. A Java int is 32 bits, i.e. 4 bytes.
        See Also:
        Constant Field Values
      • LONG_SIZE

        public static final int LONG_SIZE
        The number of bytes of a Java long. A Java int is 64 bits, i.e. 8 bytes.
        See Also:
        Constant Field Values
      • BUFFER_SIZE

        public static final int BUFFER_SIZE
        Size of buffer used to copy/move/extract files.
        See Also:
        Constant Field Values
      • KB

        public static final int KB
        Number of bytes in a Kibibyte.

        Example usage:

         int _10KB = 10 * KB;
         
        See Also:
        Constant Field Values
      • MB

        public static final int MB
        Number of bytes in a Mebibyte.

        Example usage:

         int _10MB = 10 * MB;
         
        See Also:
        Constant Field Values
    • Method Detail

      • getBytes

        public static byte[] getBytes​(String s)
        Construct a byte array containing the UTF-8 encoding of the provided string. This is significantly faster than calling String.getBytes(String) for ASCII strings.
        Parameters:
        s - The string to convert to a UTF-8 byte array.
        Returns:
        Returns a byte array containing the UTF-8 encoding of the provided string.
      • decodeUTF8

        public static String decodeUTF8​(byte[] bytes)
        Returns the provided byte array decoded as a UTF-8 string without throwing an UnsupportedEncodingException. This method is equivalent to:
         try
         {
           return new String(bytes, "UTF-8");
         }
         catch (UnsupportedEncodingException e)
         {
           // Should never happen: UTF-8 is always supported.
           throw new RuntimeException(e);
         }
         
        Parameters:
        bytes - The byte array to be decoded as a UTF-8 string.
        Returns:
        The decoded string.
      • byteToHex

        public static String byteToHex​(byte b)
        Retrieves a string representation of the provided byte in hexadecimal.
        Parameters:
        b - The byte for which to retrieve the hexadecimal string representation.
        Returns:
        The string representation of the provided byte in hexadecimal.
      • byteToLowerHex

        public static String byteToLowerHex​(byte b)
        Retrieves a string representation of the provided byte in hexadecimal.
        Parameters:
        b - The byte for which to retrieve the hexadecimal string representation.
        Returns:
        The string representation of the provided byte in hexadecimal using lowercase characters.
      • bytesToHexNoSpace

        public static String bytesToHexNoSpace​(byte[] b)
        Retrieves a string representation of the contents of the provided byte array using hexadecimal characters with no space between each byte.
        Parameters:
        b - The byte array containing the data.
        Returns:
        A string representation of the contents of the provided byte array using hexadecimal characters.
      • bytesToHex

        public static String bytesToHex​(byte[] b)
        Retrieves a string representation of the contents of the provided byte array using hexadecimal characters and a space between each byte.
        Parameters:
        b - The byte array containing the data.
        Returns:
        A string representation of the contents of the provided byte array using hexadecimal characters.
      • bytesToHex

        public static String bytesToHex​(ByteSequence b)
        Retrieves a string representation of the contents of the provided byte sequence using hexadecimal characters and a space between each byte.
        Parameters:
        b - The byte sequence containing the data.
        Returns:
        A string representation of the contents of the provided byte sequence using hexadecimal characters.
      • bytesToColonDelimitedHex

        public static String bytesToColonDelimitedHex​(byte[] b)
        Retrieves a string representation of the contents of the provided byte array using hexadecimal characters and a colon between each byte.
        Parameters:
        b - The byte array containing the data.
        Returns:
        A string representation of the contents of the provided byte array using hexadecimal characters.
      • bytesToHex

        public static String bytesToHex​(ByteBuffer b)
        Retrieves a string representation of the contents of the provided byte buffer using hexadecimal characters and a space between each byte.
        Parameters:
        b - The byte buffer containing the data.
        Returns:
        A string representation of the contents of the provided byte buffer using hexadecimal characters.
      • byteArrayToHexPlusAscii

        public static void byteArrayToHexPlusAscii​(StringBuilder buffer,
                                                   byte[] b,
                                                   int indent)
        Appends a string representation of the provided byte array to the given buffer using the specified indent. The data will be formatted with sixteen hex bytes in a row followed by the ASCII representation, then wrapping to a new line as necessary.
        Parameters:
        buffer - The buffer to which the information is to be appended.
        b - The byte array containing the data to write.
        indent - The number of spaces to indent the output.
      • compare

        public static int compare​(byte[] a,
                                  byte[] a2)
        Compare two byte arrays for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
        Parameters:
        a - The first byte array to be compared.
        a2 - The second byte array to be compared.
        Returns:
        Returns a negative integer, zero, or a positive integer if the first byte array is less than, equal to, or greater than the second.
      • getExceptionMessage

        public static org.forgerock.i18n.LocalizableMessage getExceptionMessage​(Throwable t)
        Retrieves the best human-readable message for the provided exception. For exceptions defined in the OpenDJ project, it will attempt to use the message (combining it with the message ID if available). For some exceptions that use encapsulation (e.g., InvocationTargetException), it will be unwrapped and the cause will be treated. For all others, the
        Parameters:
        t - The Throwable object for which to retrieve the message.
        Returns:
        The human-readable message generated for the provided exception.
      • stackTraceToSingleLineString

        public static String stackTraceToSingleLineString​(Throwable t)
        Retrieves a stack trace from the provided exception as a single-line string.
        Parameters:
        t - The exception for which to retrieve the stack trace.
        Returns:
        A stack trace from the provided exception as a single-line string.
      • stackTraceToString

        public static String stackTraceToString​(Throwable t)
        Retrieves a string representation of the stack trace for the provided exception.
        Parameters:
        t - The exception for which to retrieve the stack trace.
        Returns:
        A string representation of the stack trace for the provided exception.
      • stackTraceContainsCause

        public static boolean stackTraceContainsCause​(Throwable throwable,
                                                      Class<? extends Throwable> searchedCause)
        Check if the stack trace of provided exception contains a given cause.
        Parameters:
        throwable - exception that may contain the cause
        searchedCause - class of the cause to look for. Any subclass will match.
        Returns:
        true if and only if the given cause is found as a cause of any level in the provided exception.
      • getBacktrace

        public static String getBacktrace()
        Retrieves a backtrace for the current thread consisting only of filenames and line numbers that may be useful in debugging the origin of problems that should not have happened. Note that this may be an expensive operation to perform, so it should only be used for error conditions or debugging.
        Returns:
        A backtrace for the current thread.
      • getBacktrace

        public static String getBacktrace​(Throwable t)
        Retrieves a backtrace for the provided exception consisting of only filenames and line numbers that may be useful in debugging the origin of problems. This is less expensive than the call to getBacktrace without any arguments if an exception has already been thrown.
        Parameters:
        t - The exception for which to obtain the backtrace.
        Returns:
        A backtrace from the provided exception.
      • isDigit

        public static boolean isDigit​(char c)
        Indicates whether the provided character is a numeric digit.
        Parameters:
        c - The character for which to make the determination.
        Returns:
        true if the provided character represents a numeric digit, or false if not.
      • isAlpha

        public static boolean isAlpha​(char c)
        Indicates whether the provided character is an ASCII alphabetic character.
        Parameters:
        c - The character for which to make the determination.
        Returns:
        true if the provided value is an uppercase or lowercase ASCII alphabetic character, or false if it is not.
      • isHexDigit

        public static boolean isHexDigit​(char c)
        Indicates whether the provided character is a hexadecimal digit.
        Parameters:
        c - The character for which to make the determination.
        Returns:
        true if the provided character represents a hexadecimal digit, or false if not.
      • isHexDigit

        public static boolean isHexDigit​(byte b)
        Indicates whether the provided byte represents a hexadecimal digit.
        Parameters:
        b - The byte for which to make the determination.
        Returns:
        true if the provided byte represents a hexadecimal digit, or false if not.
      • hexStringToByteArray

        public static byte[] hexStringToByteArray​(String hexString)
                                           throws ParseException
        Converts the provided hexadecimal string to a byte array.
        Parameters:
        hexString - The hexadecimal string to convert to a byte array.
        Returns:
        The byte array containing the binary representation of the provided hex string.
        Throws:
        ParseException - If the provided string contains invalid hexadecimal digits or does not contain an even number of digits.
      • needsBase64Encoding

        public static boolean needsBase64Encoding​(ByteSequence valueBytes)
        Indicates whether the provided value needs to be base64-encoded if it is represented in LDIF form.
        Parameters:
        valueBytes - The binary representation of the attribute value for which to make the determination.
        Returns:
        true if the value needs to be base64-encoded if it is represented in LDIF form, or false if not.
      • needsBase64Encoding

        public static boolean needsBase64Encoding​(String valueString)
        Indicates whether the provided value needs to be base64-encoded if it is represented in LDIF form.
        Parameters:
        valueString - The string representation of the attribute value for which to make the determination.
        Returns:
        true if the value needs to be base64-encoded if it is represented in LDIF form, or false if not.
      • isValidSchemaElement

        public static boolean isValidSchemaElement​(String element,
                                                   int startPos,
                                                   int endPos,
                                                   org.forgerock.i18n.LocalizableMessageBuilder invalidReason)
        Indicates whether the provided string contains a name or OID for a schema element like an attribute type or objectclass.
        Parameters:
        element - The string containing the substring for which to make the determination.
        startPos - The position of the first character that is to be checked.
        endPos - The position of the first character after the start position that is not to be checked.
        invalidReason - The buffer to which the invalid reason is to be appended if a problem is found.
        Returns:
        true if the provided string contains a valid name or OID for a schema element, or false if it does not.
      • isAddressInUse

        public static boolean isAddressInUse​(InetAddress address,
                                             int port,
                                             boolean allowReuse)
        Indicates whether the provided TCP address is already in use.
        Parameters:
        address - IP address of the TCP address for which to make the determination.
        port - TCP port number of the TCP address for which to make the determination.
        allowReuse - Whether TCP address reuse is allowed when making the determination.
        Returns:
        true if the provided TCP address is already in use, or false otherwise.
      • isSelfConnection

        public static boolean isSelfConnection​(Socket socket)
        Indicates whether the provided connected socket is connected to itself, i.e. its local and remote endpoints are identical.

        Connecting to a local port from the TCP ephemeral range while nothing listens on it can make the kernel pick that very port as the local port of the connecting socket: TCP simultaneous open then "establishes" the connection to itself (observed on Linux). Such a socket occupies the listen port its target service is about to bind, so callers retrying connections to a temporarily stopped service must detect and close it.

        Parameters:
        socket - a connected socket
        Returns:
        true if the socket is connected to itself
      • toLowerCase

        public static String toLowerCase​(String s)
        Returns a lower-case string representation of a given string, verifying for null input string.
        Parameters:
        s - the mixed case string
        Returns:
        a lower-case string
        See Also:
        StaticUtils.toLowerCase(String)
      • toLowerCase

        public static void toLowerCase​(ByteSequence b,
                                       StringBuilder buffer,
                                       boolean trim)
        Appends a lower-case string representation of a given ByteSequence to a StringBuilder, verifying for null input.
        Parameters:
        b - The byte array for which to obtain the lowercase string representation.
        buffer - The buffer to which the lowercase form of the string should be appended.
        trim - Indicates whether leading and trailing spaces should be omitted from the string representation.
        See Also:
        }
      • toUpperCase

        public static String toUpperCase​(String s)
        Retrieves an uppercase representation of the given string. This implementation presumes that the provided string will contain only ASCII characters and is optimized for that case. However, if a non-ASCII character is encountered it will fall back on a more expensive algorithm that will work properly for non-ASCII characters.
        Parameters:
        s - The string for which to obtain the uppercase representation.
        Returns:
        The uppercase representation of the given string.
      • toUpperCase

        public static void toUpperCase​(byte[] b,
                                       StringBuilder buffer,
                                       boolean trim)
        Appends an uppercase string representation of the contents of the given byte array to the provided buffer, optionally trimming leading and trailing spaces. This implementation presumes that the provided string will contain only ASCII characters and is optimized for that case. However, if a non-ASCII character is encountered it will fall back on a more expensive algorithm that will work properly for non-ASCII characters.
        Parameters:
        b - The byte array for which to obtain the uppercase string representation.
        buffer - The buffer to which the uppercase form of the string should be appended.
        trim - Indicates whether leading and trailing spaces should be omitted from the string representation.
      • toRFC3641StringValue

        public static StringBuilder toRFC3641StringValue​(StringBuilder builder,
                                                         String string)
        Append a string to a string builder, escaping any double quotes according to the StringValue production in RFC 3641.

        In RFC 3641 the StringValue production looks like this:

            StringValue       = dquote *SafeUTF8Character dquote
            dquote            = %x22 ; " (double quote)
            SafeUTF8Character = %x00-21 / %x23-7F /   ; ASCII minus dquote
                                dquote dquote /       ; escaped double quote
                                %xC0-DF %x80-BF /     ; 2 byte UTF-8 character
                                %xE0-EF 2(%x80-BF) /  ; 3 byte UTF-8 character
                                %xF0-F7 3(%x80-BF)    ; 4 byte UTF-8 character
         

        That is, strings are surrounded by double-quotes and any internal double-quotes are doubled up.

        Parameters:
        builder - The string builder.
        string - The string to escape and append.
        Returns:
        Returns the string builder.
      • arrayToList

        public static ArrayList<String> arrayToList​(String... stringArray)
        Retrieves an array list containing the contents of the provided array.
        Parameters:
        stringArray - The string array to convert to an array list.
        Returns:
        An array list containing the contents of the provided array.
      • recursiveDelete

        public static boolean recursiveDelete​(File file)
        Attempts to delete the specified file or directory. If it is a directory, then any files or subdirectories that it contains will be recursively deleted as well.
        Parameters:
        file - The file or directory to be removed.
        Returns:
        true if the specified file and any subordinates are all successfully removed, or false if at least one element in the subtree could not be removed or file does not exists.
      • deleteFiles

        public static boolean deleteFiles​(Iterable<File> files)
        Deletes all provided files.

        Does not handle directories.

        Parameters:
        files - The files to delete.
        Returns:
        true if deletion is successful for all files, false otherwise
      • moveFile

        public static void moveFile​(File fileToMove,
                                    File targetDirectory)
                             throws IOException
        Moves the indicated file to the specified directory by creating a new file in the target directory, copying the contents of the existing file, and removing the existing file. The file to move must exist and must be a file. The target directory must exist, must be a directory, and must not be the directory in which the file currently resides.
        Parameters:
        fileToMove - The file to move to the target directory.
        targetDirectory - The directory into which the file should be moved.
        Throws:
        IOException - If a problem occurs while attempting to move the file.
      • renameFile

        public static void renameFile​(File fileToRename,
                                      File target)
                               throws IOException
        Renames the source file to the target file. If the target file exists it is first deleted. The rename and delete operation return values are checked for success and if unsuccessful, this method throws an exception.
        Parameters:
        fileToRename - The file to rename.
        target - The file to which fileToRename will be moved.
        Throws:
        IOException - If a problem occurs while attempting to rename the file. On the Windows platform, this typically indicates that the file is in use by this or another application.
      • getFileNames

        public static List<String> getFileNames​(File[] files)
        Returns the sorted list of names of provided files.
        Parameters:
        files - The files to sort and get the names of
        Returns:
        the sorted list of file names
      • getFileForPath

        public static File getFileForPath​(String path)
        Retrieves a File object corresponding to the specified path. If the given path is an absolute path, then it will be used. If the path is relative, then it will be interpreted as if it were relative to the Directory Server root.
        Parameters:
        path - The path string to be retrieved as a File
        Returns:
        A File object that corresponds to the specified path.
      • getFileForPath

        public static File getFileForPath​(String path,
                                          ServerContext serverContext)
        Retrieves a File object corresponding to the specified path. If the given path is an absolute path, then it will be used. If the path is relative, then it will be interpreted as if it were relative to the Directory Server root.
        Parameters:
        path - The path string to be retrieved as a File.
        serverContext - The server context.
        Returns:
        A File object that corresponds to the specified path.
      • createEntry

        public static Entry createEntry​(DN dn)
        Creates a new, blank entry with the given DN. It will contain only the attribute(s) contained in the RDN. The choice of objectclasses will be based on the RDN attribute. If there is a single RDN attribute, then the following mapping will be used:
        • c attribute :: country objectclass
        • dc attribute :: domain objectclass
        • o attribute :: organization objectclass
        • ou attribute :: organizationalUnit objectclass

        Any other single RDN attribute types, or any case in which there are multiple RDN attributes, will use the untypedObject objectclass. If the RDN includes one or more attributes that are not allowed in the untypedObject objectclass, then the extensibleObject class will also be added. Note that this method cannot be used to generate an entry with an empty or null DN.
        Parameters:
        dn - The DN to use for the entry.
        Returns:
        The entry created with the provided DN.
      • secondsToTimeString

        public static org.forgerock.i18n.LocalizableMessage secondsToTimeString​(long numSeconds)
        Retrieves a user-friendly string that indicates the length of time (in days, hours, minutes, and seconds) in the specified number of seconds.
        Parameters:
        numSeconds - The number of seconds to be converted to a more user-friendly value.
        Returns:
        The user-friendly representation of the specified number of seconds.
      • checkOnlyOneArgPresent

        public static void checkOnlyOneArgPresent​(Argument... args)
                                           throws ArgumentException
        Checks that no more that one of a set of arguments is present. This utility should be used after argument parser has parsed a set of arguments.
        Parameters:
        args - to test for the presence of more than one
        Throws:
        ArgumentException - if more than one of args is present and containing an error message identifying the arguments in violation
      • parseDateTimeString

        public static Date parseDateTimeString​(String timeStr)
                                        throws ParseException
        Converts a string representing a time in "yyyyMMddHHmmss.SSS'Z'" or "yyyyMMddHHmmss" to a Date.
        Parameters:
        timeStr - string formatted appropriately
        Returns:
        Date object; null if timeStr is null
        Throws:
        ParseException - if there was a problem converting the string to a Date.
      • formatDateTimeString

        public static String formatDateTimeString​(Date date)
        Formats a Date to String representation in "yyyyMMddHHmmss'Z'".
        Parameters:
        date - to format; null if date is null
        Returns:
        string representation of the date
      • isEmailAddress

        public static boolean isEmailAddress​(String addr)
        Indicates whether a string represents a syntactically correct email address.
        Parameters:
        addr - to validate
        Returns:
        boolean where true indicates that the string is a syntactically correct email address
      • addSuperiorObjectClasses

        public static void addSuperiorObjectClasses​(Map<ObjectClass,​String> objectClasses)
        Add all of the superior objectclasses to the specified objectclass map if they don't already exist. Used by add and import-ldif to add missing superior objectclasses to entries that don't have them.
        Parameters:
        objectClasses - A Map of objectclasses.
      • close

        public static void close​(Closeable... closeables)
        Closes the provided Closeable's ignoring any errors which occurred.
        Parameters:
        closeables - The closeables to be closed, which may be null.
      • close

        public static void close​(Collection<? extends Closeable> closeables)
        Closes the provided Closeable's ignoring any errors which occurred.
        Parameters:
        closeables - The closeables to be closed, which may be null.
      • sleep

        public static void sleep​(long millis)
        Calls Thread.sleep(long), surrounding it with the mandatory try / catch(InterruptedException) block.
        Parameters:
        millis - the length of time to sleep in milliseconds
      • hasDescriptor

        public static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg,
                                            org.forgerock.i18n.LocalizableMessageDescriptor.Arg0 desc)
        Test if the provided message corresponds to the provided descriptor.
        Parameters:
        msg - The i18n message.
        desc - The message descriptor.
        Returns:
        true if message corresponds to descriptor
      • hasDescriptor

        public static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg,
                                            org.forgerock.i18n.LocalizableMessageDescriptor.Arg1<?> desc)
        Test if the provided message corresponds to the provided descriptor.
        Parameters:
        msg - The i18n message.
        desc - The message descriptor.
        Returns:
        true if message corresponds to descriptor
      • hasDescriptor

        public static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg,
                                            org.forgerock.i18n.LocalizableMessageDescriptor.Arg3<?,​?,​?> desc)
        Test if the provided message corresponds to the provided descriptor.
        Parameters:
        msg - The i18n message.
        desc - The message descriptor.
        Returns:
        true if message corresponds to descriptor
      • hasDescriptor

        public static boolean hasDescriptor​(org.forgerock.i18n.LocalizableMessage msg,
                                            org.forgerock.i18n.LocalizableMessageDescriptor.Arg7<?,​?,​?,​?,​?,​?,​?> desc)
        Test if the provided message corresponds to the provided descriptor.
        Parameters:
        msg - The i18n message.
        desc - The message descriptor.
        Returns:
        true if message corresponds to descriptor
      • toIterable

        public static <T> Iterable<T> toIterable​(Iterator<T> iterator)
        Returns an Iterable returning the passed in Iterator. THis allows using methods returning Iterators with foreach statements.

        For example, consider a method with this signature:

        public Iterator<String> myIteratorMethod();

        Classical use with for or while loop:

         for (Iterator<String> it = myIteratorMethod(); it.hasNext();)
         {
           String s = it.next();
           // use it
         }
        
         Iterator<String> it = myIteratorMethod();
         while(it.hasNext();)
         {
           String s = it.next();
           // use it
         }
         
        Improved use with foreach:
         for (String s : StaticUtils.toIterable(myIteratorMethod()))
         {
         }
         
        Type Parameters:
        T - the generic type of the passed in Iterator and for the returned Iterable.
        Parameters:
        iterator - the Iterator that will be returned by the Iterable.
        Returns:
        an Iterable returning the passed in Iterator
      • isOEMVersion

        public static boolean isOEMVersion()
        Returns true if the version of the server is an OEM one, and therefore doesn't support the JE backend.
        Returns:
        true if the version of the server is an OEM version and false otherwise.
      • isClassAvailable

        public static boolean isClassAvailable​(String className)
        Returns true if the class is available in the classpath.
        Parameters:
        className - the string representing the class to check.
        Returns:
        true if the class is available in the classpath and false otherwise.
      • extractZipArchive

        public static void extractZipArchive​(File zipFile,
                                             File targetDirectory,
                                             List<String> executableDirectories,
                                             List<String> executableFiles)
                                      throws IOException
        Extracts the provided zip archive to the provided target directory.

        A file is set to executable if one or more of the following three conditions apply:

        • It ends with a suffix identified as executable file suffix (.sh, .bat, .exe)
        • It is listed in the provided executableFiles
        • It is included in a directory that is listed in the provided executableDirectories
        Parameters:
        zipFile - The zip file to extract.
        targetDirectory - The target directory for the content of the archive.
        executableDirectories - List of extracted directories which should have all their files set as executable. Each directory must be provided as a path relative to the target directory (e.g. "opendj/bin")
        executableFiles - List of individual files which should be set as executable. Each file must be provided as a path relative to the target directory (e.g. "opendj/setup")
        Throws:
        IOException - If zip archive can't be read or target files can be written.