@PublicAPI(stability=UNCOMMITTED, mayInstantiate=false, mayExtend=false, mayInvoke=true) public final class StaticUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ALL_OPERATIONAL_ATTRIBUTES
The pattern that asks a search request to return all operational attributes.
|
static String[] |
ALL_USER_AND_OPERATIONAL_ATTRIBUTES
The pattern that asks a search request to return all user and operational attributes.
|
static String |
ALL_USER_ATTRIBUTES
The pattern that asks a search request to return all user attributes.
|
static String |
HOST_NAME_FILE
The relative path where the setup stores the name of the host the user provides.
|
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.
|
static String |
OPENDJ_JAVA_HOME
Specific environment variable used by the scripts to find java.
|
Modifier and Type | Method and Description |
---|---|
static void |
addMissingSuperiorObjectClasses(Entry entry)
Add all of the superior objectclasses to the specified entry if they are not already present.
|
static Entry |
addRdnAttributes(Entry entry)
Add the RDN attribute(s) to the provided entry.
|
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 |
checkVersionMismatch()
Checks if the binary version is the same than the instance version.
|
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 Entry |
createEntry(Dn dn,
Schema schema)
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 |
formatDateTimeString(TemporalAccessor instant)
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 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 String |
getHostNameForCertificate(String installationRoot)
Returns the host name to be used to create self-signed certificates.
|
static String |
getObjectClassName(Rdn rdn)
Best effort to find structural object class name of the provided base entry RDN.
|
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg1<?> 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 |
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 |
isPrivilegedPort(int port)
Returns
true if the provided port is a privileged port, false otherwise. |
static boolean |
isServerRunning()
Returns whether the server located in the provided path is running.
|
static void |
moveDirectory(Path sourceDirectory,
Path targetDirectory)
Moves the indicated directory to the specified directory by recursively copying and then removing the existing
directory.
|
static File |
moveFileSafely(File source,
File target)
Attempts to move a file atomically and consistently.
|
static boolean |
needsBase64Encoding(ByteSequence valueBytes)
Indicates whether the provided value needs to be base64-encoded if it is represented in LDIF form.
|
static boolean |
needsBase64Encoding(String valueString)
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 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 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 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 String |
toLowerCase(String s)
Returns a lower-case string representation of a given string, verifying for null input string.
|
static Collection<Pattern> |
toPatterns(Collection<String> regExps)
Returns a collection of patterns representing provided regular expression strings.
|
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.
|
static boolean |
validatePatterns(Collection<String> blackListRegExps,
Collection<String> whiteListRegExps,
Collection<LocalizableMessage> unacceptableReasons)
Returns whether all the provided black and white list strings are valid regular expression patterns by using
Pattern.compile(String) . |
public static final String OPENDJ_JAVA_HOME
public static final String HOST_NAME_FILE
public static final int INT_SIZE
public static final int LONG_SIZE
public static final int KB
Example usage:
int _10KB = 10 * KB;
public static final int MB
Example usage:
int _10MB = 10 * MB;
public static final String ALL_USER_ATTRIBUTES
public static final String ALL_OPERATIONAL_ATTRIBUTES
public static final String[] ALL_USER_AND_OPERATIONAL_ATTRIBUTES
public static byte[] getBytes(String s)
String.getBytes(String)
for ASCII strings.s
- The string to convert to a UTF-8 byte array.public static String decodeUTF8(byte[] bytes)
try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { // Should never happen: UTF-8 is always supported. throw new RuntimeException(e); }
bytes
- The byte array to be decoded as a UTF-8 string.public static String byteToHex(byte b)
b
- The byte for which to retrieve the hexadecimal string representation.public static String byteToLowerHex(byte b)
b
- The byte for which to retrieve the hexadecimal string representation.public static String bytesToHexNoSpace(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(ByteSequence b)
b
- The byte sequence containing the data.public static String bytesToColonDelimitedHex(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(ByteBuffer b)
b
- The byte buffer containing the data.public static void byteArrayToHexPlusAscii(StringBuilder buffer, byte[] b, int indent)
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.public static int compare(byte[] a, byte[] a2)
a
- The first byte array to be compared.a2
- The second byte array to be compared.public static LocalizableMessage getExceptionMessage(Throwable t)
t
- The Throwable
object for which to retrieve the message.public static String stackTraceToSingleLineString(Throwable t)
t
- The exception for which to retrieve the stack trace.public static String stackTraceToString(Throwable t)
t
- The exception for which to retrieve the stack trace.public static boolean stackTraceContainsCause(Throwable throwable, Class<? extends Throwable> searchedCause)
throwable
- exception that may contain the causesearchedCause
- class of the cause to look for. Any subclass will match.public static String getBacktrace()
public static String getBacktrace(Throwable t)
getBacktrace
without any
arguments if an exception has already been thrown.t
- The exception for which to obtain the backtrace.public static boolean isDigit(char c)
c
- The character for which to make the determination.true
if the provided character represents a numeric digit, or false
if not.public static boolean isAlpha(char c)
c
- The character for which to make the determination.true
if the provided value is an uppercase or lowercase ASCII alphabetic character, or
false
if it is not.public static boolean isHexDigit(char c)
c
- The character for which to make the determination.true
if the provided character represents a hexadecimal digit, or false
if not.public static boolean isHexDigit(byte b)
b
- The byte for which to make the determination.true
if the provided byte represents a hexadecimal digit, or false
if not.public static byte[] hexStringToByteArray(String hexString) throws ParseException
hexString
- The hexadecimal string to convert to a byte array.ParseException
- If the provided string contains invalid hexadecimal digits or does not contain an even number of
digits.public static boolean needsBase64Encoding(ByteSequence valueBytes)
valueBytes
- The binary representation of the attribute value for which to make the determination.true
if the value needs to be base64-encoded if it is represented in LDIF form, or false
if not.public static boolean needsBase64Encoding(String valueString)
valueString
- The string representation of the attribute value for which to make the determination.true
if the value needs to be base64-encoded if it is represented in LDIF form, or false
if not.public static boolean isAddressInUse(InetAddress address, int port, boolean allowReuse)
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.true
if the provided TCP address is already in use, or false
otherwise.public static String toLowerCase(String s)
s
- the mixed case stringStaticUtils.toLowerCase(String)
public static void toLowerCase(ByteSequence b, StringBuilder buffer, boolean trim)
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.StaticUtils.toLowerCase(ByteSequence, StringBuilder)
public static String toUpperCase(String s)
s
- The string for which to obtain the uppercase representation.public static void toUpperCase(byte[] b, StringBuilder buffer, boolean trim)
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.public static StringBuilder toRFC3641StringValue(StringBuilder builder, String string)
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.
builder
- The string builder.string
- The string to escape and append.public static boolean recursiveDelete(File file)
file
- The file or directory to be removed.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.public static boolean deleteFiles(Iterable<File> files)
Does not handle directories.
files
- The files to delete.true
if deletion is successful for all files, false otherwisepublic static File moveFileSafely(File source, File target) throws IOException
Files.move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)
, but enhances it
by first ensuring that the source file has been fsync'd to disk. It then attempts to move the file atomically, if
supported by the underlying file-system, and then falls back to a non-atomic move.source
- The path of the file to be moved.target
- The new path.IOException
- If an I/O error occurs.public static void moveDirectory(Path sourceDirectory, Path targetDirectory) throws IOException
sourceDirectory
- The directory to move to the target directory.targetDirectory
- The new directory into which the source files and directories should be moved.IOException
- If a problem occurs while attempting to move the directory.public static void renameFile(File fileToRename, File target) throws IOException
fileToRename
- The file to rename.target
- The file to which fileToRename
will be moved.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.public static List<String> getFileNames(File[] files)
files
- The files to sort and get the names ofpublic static File getFileForPath(String path)
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.path
- The path string to be retrieved as a File
File
object that corresponds to the specified path.public static File getFileForPath(String path, ServerContext serverContext)
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.path
- The path string to be retrieved as a File
.serverContext
- The server context.File
object that corresponds to the specified path.public static Entry createEntry(Dn dn)
dn
- The DN to use for the entry.public static Entry createEntry(Dn dn, Schema schema)
dn
- The DN to use for the entry.schema
- the schema from which to create the entrypublic static String getObjectClassName(Rdn rdn)
public static LocalizableMessage secondsToTimeString(long numSeconds)
numSeconds
- The number of seconds to be converted to a more user-friendly value.public static Date parseDateTimeString(String timeStr) throws ParseException
Date
.timeStr
- string formatted appropriatelytimeStr
is nullParseException
- if there was a problem converting the string to a Date
.public static String formatDateTimeString(Date date)
date
- to format; null if date
is nullpublic static String formatDateTimeString(TemporalAccessor instant)
instant
- to format; null if date
is nullpublic static boolean isEmailAddress(String addr)
addr
- to validatetrue
indicates that the string is a syntactically correct email addresspublic static Entry addRdnAttributes(Entry entry)
entry
- the entrypublic static void addMissingSuperiorObjectClasses(Entry entry)
entry
- The entry where to add the missing superior object classes.public static void sleep(long millis)
Thread.sleep(long)
, surrounding it with the mandatory try
/
catch(InterruptedException)
block.millis
- the length of time to sleep in millisecondspublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg1<?> desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static void extractZipArchive(File zipFile, File targetDirectory, List<String> executableDirectories, List<String> executableFiles) throws IOException
A file is set to executable if one or more of the following three conditions apply:
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")IOException
- If zip archive can't be read or target files can be written.public static boolean isPrivilegedPort(int port)
true
if the provided port is a privileged port, false
otherwise.port
- the port we are analyzing.true
if the provided port is a privileged port, false
otherwise.public static String getHostNameForCertificate(String installationRoot) throws UnknownHostException
InetAddress
is used.installationRoot
- the path where the server is installed.UnknownHostException
- if a host name could not be used.public static void checkVersionMismatch() throws com.forgerock.opendj.cli.ClientException
com.forgerock.opendj.cli.ClientException
- Sends an exception if the version mismatch.public static Collection<Pattern> toPatterns(Collection<String> regExps)
regExps
- A collection of strings to be converted into regular expression patterns.Pattern
s associated to the provided regular expression strings.PatternSyntaxException
- If a provided string cannot be parsed as a valid regular expression Pattern
(see Pattern.compile()
for more details).public static boolean validatePatterns(Collection<String> blackListRegExps, Collection<String> whiteListRegExps, Collection<LocalizableMessage> unacceptableReasons)
Pattern.compile(String)
. All validation errors will be added to the provided
collection of localized messages.blackListRegExps
- A Collection
of strings which represents the black list regular expression patterns to validate.whiteListRegExps
- A Collection
of strings which represents the white list regular expression patterns to validate.unacceptableReasons
- The Collection
where messages will be added for each invalid regular expression patterns.true
if all provided strings can be parsed as a valid regular expression Pattern
,
false
otherwise.public static boolean isServerRunning()
true
if the server located in the provided path is running, false
otherwise.Copyright 2010-2022 ForgeRock AS.