Package com.sun.enterprise.util
Class Utility
java.lang.Object
com.sun.enterprise.util.Utility
Handy class full of static functions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceUtility.RunnableWithException<E extends Exception> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intbytesToInt(byte[] array, int offset) Unmarshal a byte array to an integer.static longbytesToLong(byte[] array, int offset) Unmarshal a byte array to an long.static voidstatic <T> Tcoalesce(T... objects) Returns the first non-nullobject of the argument list, ornullif there is no such element.static char[]convertByteArrayToCharArray(byte[] byteArray, Charset charset) Convert the byte array to char array with respect to given charset.static byte[]convertCharArrayToByteArray(char[] charArray, Charset charset) Convert the char array to byte array with respect to given charset.static ClassLoaderGet the current thread's context class loader which is set to the CommonClassLoader by ApplicationServerstatic StringgetEnvOrProp(String name) Return the value for a given name from the System Properties or the Environmental Variables.static StringReturn the hostname of the local machine.static StringReturn the hostname of the local machine.static PropertiesgetPropertiesFromFile(String file) static voidintToBytes(int value, byte[] array, int offset) Marshal an integer to a byte array.static shortintToShort(int value) static voidinvokeApplicationMain(Class mainClass, String[] args) Verify and invoke main if present in the specified class.static voidinvokeSetMethod(Object obj, String prop, String value) static voidinvokeSetMethodCaseInsensitive(Object obj, String prop, String value) static booleanisAllEmpty(Object... values) Returns true if all values are empty, false if at least one value is not empty.static booleanstatic booleanisAnyEmpty(Object... values) Returnstrueif at least one value is empty.static booleanstatic booleanReturnstrueif the given value is null or is empty.static booleanReturnstrueif the given array is null or is empty.static booleanReturns true if the given string is null or is empty.static booleanisEmpty(Collection<?> collection) Returnstrueif the given collection is null or is empty.static <T> booleanisOneOf(T object, T... objects) Returnstrueif the given object equals one of the given objects.static ClassLoads the class with the common class loader.static voidlongToBytes(long value, byte[] array, int offset) Marshal an long to a byte array.static RemotelookupObject(String publishedName, Class anInterface) This is a convenience method to lookup a remote object by name within the naming context.static <E extends Exception>
voidrunWithContextClassLoader(ClassLoader contextClassLoader, Utility.RunnableWithException<E> action) Same asrunWithContextClassLoader(java.lang.ClassLoader, java.util.function.Supplier)but with an action that doesn't return anythingstatic <T> TrunWithContextClassLoader(ClassLoader contextClassLoader, Supplier<T> action) Run an action with a specific classloader as the context classloader.static ClassLoadersetContextClassLoader(ClassLoader classLoader) Utility routine for setting the context class loader.static voidstatic intshortToInt(short value) static byte[]Returns a byte array for the valid bytes in a ByteBuffer.static char[]Returns a character array for the valid characters in a CharBuffer.
-
Constructor Details
-
Utility
public Utility()
-
-
Method Details
-
checkJVMVersion
public static void checkJVMVersion() -
isEmpty
Returns true if the given string is null or is empty.- Parameters:
string- The string to be checked on emptiness.- Returns:
- True if the given string is null or is empty.
-
isEmpty
Returnstrueif the given array is null or is empty.- Parameters:
array- The array to be checked on emptiness.- Returns:
trueif the given array is null or is empty.
-
isEmpty
Returnstrueif the given collection is null or is empty.- Parameters:
collection- The collection to be checked on emptiness.- Returns:
trueif the given collection is null or is empty.
-
isEmpty
Returnstrueif the given value is null or is empty. Types of String, Collection, Map, Optional and Array are recognized. If none is recognized, then examine the emptiness of the toString() representation instead.- Parameters:
value- The value to be checked on emptiness.- Returns:
trueif the given value is null or is empty.
-
isAllEmpty
Returns true if all values are empty, false if at least one value is not empty.- Parameters:
values- the values to be checked on emptiness- Returns:
- True if all values are empty, false otherwise
-
isAnyEmpty
Returnstrueif at least one value is empty.- Parameters:
values- the values to be checked on emptiness- Returns:
trueif any value is empty andfalseif no values are empty
-
isAllNull
-
isAnyNull
-
isOneOf
Returnstrueif the given object equals one of the given objects.- Type Parameters:
T- The generic object type.- Parameters:
object- The object to be checked if it equals one of the given objects.objects- The argument list of objects to be tested for equality.- Returns:
trueif the given object equals one of the given objects.
-
coalesce
Returns the first non-nullobject of the argument list, ornullif there is no such element.- Type Parameters:
T- The generic object type.- Parameters:
objects- The argument list of objects to be tested for non-null.- Returns:
- The first non-
nullobject of the argument list, ornullif there is no such element.
-
getPropertiesFromFile
- Throws:
IOException
-
getLocalHost
Return the hostname of the local machine. -
getLocalAddress
Return the hostname of the local machine. -
lookupObject
This is a convenience method to lookup a remote object by name within the naming context.- Throws:
NamingException- if the object with that name could not be found.
-
toCharArray
Returns a character array for the valid characters in a CharBuffer.- Parameters:
cb-- Returns:
-
toByteArray
Returns a byte array for the valid bytes in a ByteBuffer.- Parameters:
bb-- Returns:
-
bytesToInt
public static int bytesToInt(byte[] array, int offset) Unmarshal a byte array to an integer. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.- Parameters:
array- The array of bytes.offset- The offset from which to start unmarshalling.
-
intToBytes
public static void intToBytes(int value, byte[] array, int offset) Marshal an integer to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.- Parameters:
array- The array of bytes.offset- The offset from which to start marshalling.
-
bytesToLong
public static long bytesToLong(byte[] array, int offset) Unmarshal a byte array to an long. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.- Parameters:
array- The array of bytes.offset- The offset from which to start unmarshalling.
-
longToBytes
public static void longToBytes(long value, byte[] array, int offset) Marshal an long to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.- Parameters:
array- The array of bytes.offset- The offset from which to start marshalling.
-
invokeApplicationMain
public static void invokeApplicationMain(Class mainClass, String[] args) throws InvocationTargetException, IllegalAccessException, ClassNotFoundException Verify and invoke main if present in the specified class. -
invokeSetMethod
public static void invokeSetMethod(Object obj, String prop, String value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException -
invokeSetMethodCaseInsensitive
public static void invokeSetMethodCaseInsensitive(Object obj, String prop, String value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException -
intToShort
public static short intToShort(int value) -
shortToInt
public static int shortToInt(short value) -
getClassLoader
Get the current thread's context class loader which is set to the CommonClassLoader by ApplicationServer- Returns:
- the thread's context classloader if it exists; else the system class loader.
-
loadClass
Loads the class with the common class loader.- Parameters:
className- the class name- Returns:
- the loaded class
- Throws:
ClassNotFoundException
-
setContextClassLoader
Utility routine for setting the context class loader.- Returns:
- previous class loader; can be the same instance.
-
runWithContextClassLoader
Run an action with a specific classloader as the context classloader. Sets the context classloader, calls the method, resets the context classloader to the previous classloader.- Parameters:
contextClassLoader- Classloader to be used as the context classloader during thi method callaction- A mathod to call with the classloader as the context classloader.Type- of a value returned from the supplied action and from this method- Returns:
- Value returned by the action method
-
runWithContextClassLoader
public static <E extends Exception> void runWithContextClassLoader(ClassLoader contextClassLoader, Utility.RunnableWithException<E> action) throws E Same asrunWithContextClassLoader(java.lang.ClassLoader, java.util.function.Supplier)but with an action that doesn't return anything- Throws:
E
-
setEnvironment
public static void setEnvironment() -
getEnvOrProp
Return the value for a given name from the System Properties or the Environmental Variables. The former overrides the latter.- Parameters:
name- - the name of the System Property or Environmental Variable- Returns:
- the value of the variable or null if it was not found
-
convertByteArrayToCharArray
public static char[] convertByteArrayToCharArray(byte[] byteArray, Charset charset) throws CharacterCodingException Convert the byte array to char array with respect to given charset.- Parameters:
byteArray-charset- null or "" means default charset- Throws:
CharacterCodingException
-
convertCharArrayToByteArray
public static byte[] convertCharArrayToByteArray(char[] charArray, Charset charset) throws CharacterCodingException Convert the char array to byte array with respect to given charset.- Parameters:
charArray-charset- null or "" means default charset- Throws:
CharacterCodingException
-