com.ibm.are.common
Class Utils

java.lang.Object
  extended by com.ibm.are.common.Utils

public final class Utils
extends java.lang.Object

The Utils class provides a variety of utility methods used by other ARE classes.

Version:
1.0
Author:
IBM

Field Summary
static java.lang.String COPYRIGHT
          Copyright for class bytecode
static java.lang.String lineSep
          Line separator character
 
Method Summary
static boolean classImplementsInterface(java.lang.Class theClass, java.lang.String interfaceName, java.util.logging.Logger logger)
          Checks if the given class implements the specified interface
static java.lang.String classNameOnly(java.lang.Class theClass)
          Returns only the class name for the given class by removing the package part of the class name.
static void copyFile(java.lang.String srcFileName, java.lang.String destFileName)
          Performs a binary copy of the contents of the source file into the destination file
static java.util.logging.Logger createEmptyLogger()
          Creates an empty logger, with no log handlers.
static java.util.logging.Logger createLogger(java.lang.String packageName, java.lang.String logFileName, int maxSize, int maxNumber)
          Creates a new logger with the given specifications
static boolean dirExists(java.lang.String path)
          Determines if the directory specified by the given path exists
static java.lang.String ensureDoesNotEndWithSlash(java.lang.String path)
          Ensures that the given path does not end with a slash character.
static java.lang.String ensureEndsWithSlash(java.lang.String path)
          Ensures that the given path ends with a slash character.
static java.lang.String fileDirFromPath(java.lang.String entirePath)
          Returns the directory from the fully qualified path
static boolean fileExists(java.lang.String path)
          Determines if the file specified by the given path exists
static java.lang.String fileNameFromPath(java.lang.String entirePath)
          Returns the file name from the fully qualified path
static java.lang.reflect.Constructor findConstructor(java.lang.String className)
          Finds the empty, no parameter constructor in the given class.
static java.lang.reflect.Constructor findConstructor(java.lang.String className, java.lang.Class[] args)
          Finds the constructor that matches the specified argument signature in the given class.
static java.lang.reflect.Field findField(java.lang.String className, java.lang.String fieldName)
          Finds the specified field in the given class.
static java.lang.reflect.Method findMethod(java.lang.Object invokeObj, java.lang.String methodName)
          Finds the specified method in the given instance of a class
static java.lang.reflect.Method findMethod(java.lang.Object invokeObj, java.lang.String methodName, java.lang.Class[] args)
          Finds the specified method in the given instance of a class
static java.lang.reflect.Method findMethod(java.lang.String className, java.lang.String methodName)
          Finds the specified method in the given class.
static java.lang.reflect.Method findMethod(java.lang.String className, java.lang.String methodName, java.lang.Class[] args)
          Finds the specified method in the given class.
static java.util.logging.Logger getGlobalServiceLogger()
          Retrieves a reference to the global ARE logger.
static java.lang.Object getInstance(java.lang.String className)
          Creates and returns a new instance of the specified class
static java.util.List listFiles(java.lang.String dir)
          Returns a list of the contents of the specified directory.
static java.util.List listFilesInDirTree(java.lang.String rootDir, boolean followSymlinks)
          Returns a list of the contents of the specified directory tree.
static boolean parseBoolean(java.lang.String inputBoolean)
          Parses a string representation of a boolean, and converts it into a a primitive boolean value.
static void printContents(java.io.PrintStream out, java.util.Iterator theList, java.lang.String header, java.lang.String footer)
          Prints the contents of the iterator to the specified stream.
static void printContents(java.io.PrintWriter out, java.util.Iterator theList, java.lang.String header, java.lang.String footer)
          Prints the contents of the iterator to the specified writer.
static void printContents(java.io.PrintWriter out, java.util.Iterator theList, java.lang.String header, java.lang.String footer, java.lang.String encoding)
          Prints the contents of the iterator to the specified writer.
static java.util.List readFile(java.lang.String fileName, java.lang.String encoding)
          Reads the contents of the file, interpreting its contents using the specified character encoding.
static java.lang.String[] trimAll(java.lang.String[] inputList)
          Trims each string in the given array.
static void writeFile(java.lang.String fileName, java.lang.String encoding, java.util.List contents)
          Writes the specified contents to the specified file using the provided character encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
Copyright for class bytecode

See Also:
Constant Field Values

lineSep

public static final java.lang.String lineSep
Line separator character

Method Detail

fileExists

public static boolean fileExists(java.lang.String path)
Determines if the file specified by the given path exists

Parameters:
path - The file path
Returns:
true if the file exists, false if it does not

dirExists

public static boolean dirExists(java.lang.String path)
Determines if the directory specified by the given path exists

Parameters:
path - The directory path
Returns:
true if the directory exists, false if it does not

ensureEndsWithSlash

public static java.lang.String ensureEndsWithSlash(java.lang.String path)
Ensures that the given path ends with a slash character. The code will ensure that the appropriate slash character is added, if needed, based on the current platform.

Parameters:
path - The path to ensure ends with a slash
Returns:
The path with a slash at the end. If the path already ended with a slash, the original path string is returned.

ensureDoesNotEndWithSlash

public static java.lang.String ensureDoesNotEndWithSlash(java.lang.String path)
Ensures that the given path does not end with a slash character.

Parameters:
path - The path to ensure does not end with a slash
Returns:
The path with no slash at the end. If the path did not end with a slash, the original path string is returned.

trimAll

public static java.lang.String[] trimAll(java.lang.String[] inputList)
Trims each string in the given array. The elements are trimmed by invoking the java.lang.String.trim method on each element in the array.

Parameters:
inputList - The array of strings to trim
Returns:
A newly allocated string array that contains each trimmed element in the original array.

classNameOnly

public static java.lang.String classNameOnly(java.lang.Class theClass)
Returns only the class name for the given class by removing the package part of the class name.

Parameters:
theClass - The class to retrieve the name of
Returns:
Only the class name, with the package part of the class name removed.

fileNameFromPath

public static java.lang.String fileNameFromPath(java.lang.String entirePath)
Returns the file name from the fully qualified path

Parameters:
entirePath - The path to return the file name from
Returns:
The file name from the fully qualified path

fileDirFromPath

public static java.lang.String fileDirFromPath(java.lang.String entirePath)
Returns the directory from the fully qualified path

Parameters:
entirePath - The path to return the directory from
Returns:
The directory from the fully qualified path. If the path does not include a directory portion, a zero length string is returned.

listFiles

public static java.util.List listFiles(java.lang.String dir)
Returns a list of the contents of the specified directory. Each item in the list is not just a file name, but rather a fully qualified path.

Parameters:
dir - The directory to list the contents of
Returns:
A list of the contents of the specified directory

listFilesInDirTree

public static java.util.List listFilesInDirTree(java.lang.String rootDir,
                                                boolean followSymlinks)
Returns a list of the contents of the specified directory tree. Each item in the list is not just a file name, but rather a fully qualified path.

Parameters:
rootDir - The directory tree root
followSymlinks - Whether or not symbolic links are followed. This is only applicable for symbolic links to directories.
Returns:
A list of the contents of the specified directory tree

parseBoolean

public static boolean parseBoolean(java.lang.String inputBoolean)
Parses a string representation of a boolean, and converts it into a a primitive boolean value.

Parameters:
inputBoolean - The string representation of a boolean
Returns:
A primitive boolean value (true or false) based on the input string

copyFile

public static void copyFile(java.lang.String srcFileName,
                            java.lang.String destFileName)
Performs a binary copy of the contents of the source file into the destination file

Parameters:
srcFileName - The file to copy from
destFileName - The file to copy into

readFile

public static java.util.List readFile(java.lang.String fileName,
                                      java.lang.String encoding)
Reads the contents of the file, interpreting its contents using the specified character encoding. The contents are placed into a list and returned.

Parameters:
fileName - The name of the file to read
encoding - The character encoding of the text in the file. If no encoding needs to be specified, null can be passed for this parameter, in which case the system property file.encoding will be used.
Returns:
A list containing the entire contents of the file

writeFile

public static void writeFile(java.lang.String fileName,
                             java.lang.String encoding,
                             java.util.List contents)
Writes the specified contents to the specified file using the provided character encoding.

Parameters:
fileName - The name of the file to write the contents to
encoding - The character encoding of the text written to the file. If no encoding needs to be specified, null can be passed for this parameter, in which case the system property file.encoding will be used.
contents - A list of the contents to write to the file. The contents will be written in the precise order they are stored in the list.

getInstance

public static java.lang.Object getInstance(java.lang.String className)
                                    throws java.lang.Exception
Creates and returns a new instance of the specified class

Parameters:
className - The name of the class to create a new instance of
Returns:
A new instance of the specified class
Throws:
java.lang.Exception - If an exception occurs while instantiating a new instance of the specified class.

printContents

public static void printContents(java.io.PrintStream out,
                                 java.util.Iterator theList,
                                 java.lang.String header,
                                 java.lang.String footer)
Prints the contents of the iterator to the specified stream. An optional header and footer can also be printed, with the header printed prior to the iterator contents, and the footer printed after the iterator contents.

Parameters:
out - The stream to print to
theList - An iterator over a list of items to print to the stream
header - An optional header that is printed to the stream prior to printing the items in the iterator. If no header is desired, null can be passed for this parameter.
footer - An optional footer that is printed to the stream after printing the items in the iterator. If no footer is desired, null can be passed for this parameter.

printContents

public static void printContents(java.io.PrintWriter out,
                                 java.util.Iterator theList,
                                 java.lang.String header,
                                 java.lang.String footer)
Prints the contents of the iterator to the specified writer. An optional header and footer can also be printed, with the header printed prior to the iterator contents, and the footer printed after the iterator contents.

Parameters:
out - The writer to print to
theList - An iterator over a list of items to print to the writer
header - An optional header that is printed to the writer prior to printing the items in the iterator. If no header is desired, null can be passed for this parameter.
footer - An optional footer that is printed to the writer after printing the items in the iterator. If no footer is desired, null can be passed for this parameter.

printContents

public static void printContents(java.io.PrintWriter out,
                                 java.util.Iterator theList,
                                 java.lang.String header,
                                 java.lang.String footer,
                                 java.lang.String encoding)
Prints the contents of the iterator to the specified writer. An optional header and footer can also be printed, with the header printed prior to the iterator contents, and the footer printed after the iterator contents.

Parameters:
out - The writer to print to
theList - An iterator over a list of items to print to the writer
header - An optional header that is printed to the writer prior to printing the items in the iterator. If no header is desired, null can be passed for this parameter.
footer - An optional footer that is printed to the writer after printing the items in the iterator. If no footer is desired, null can be passed for this parameter.
encoding - The encoding of the text that will be written to the writer

findField

public static java.lang.reflect.Field findField(java.lang.String className,
                                                java.lang.String fieldName)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.SecurityException,
                                                java.lang.NoSuchFieldException
Finds the specified field in the given class.

Parameters:
className - The class name
fieldName - The name of the field to find in the class
Returns:
A reflection object that represents the field
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the field cannot be accessed
java.lang.NoSuchFieldException - If the field cannot be found in the specified class

findConstructor

public static java.lang.reflect.Constructor findConstructor(java.lang.String className)
                                                     throws java.lang.ClassNotFoundException,
                                                            java.lang.SecurityException,
                                                            java.lang.NoSuchMethodException
Finds the empty, no parameter constructor in the given class.

Parameters:
className - The class name
Returns:
A reflection object that represents the constructor
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the constructor cannot be accessed
java.lang.NoSuchMethodException - If the constructor cannot be found in the specified class

findConstructor

public static java.lang.reflect.Constructor findConstructor(java.lang.String className,
                                                            java.lang.Class[] args)
                                                     throws java.lang.ClassNotFoundException,
                                                            java.lang.SecurityException,
                                                            java.lang.NoSuchMethodException
Finds the constructor that matches the specified argument signature in the given class.

Parameters:
className - The class name
args - The constructor argument types
Returns:
A reflection object that represents the constructor
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the constructor cannot be accessed
java.lang.NoSuchMethodException - If the constructor cannot be found in the specified class

findMethod

public static java.lang.reflect.Method findMethod(java.lang.String className,
                                                  java.lang.String methodName)
                                           throws java.lang.ClassNotFoundException,
                                                  java.lang.SecurityException,
                                                  java.lang.NoSuchMethodException
Finds the specified method in the given class.

Parameters:
className - The class name
methodName - The name of the method to find in the class
Returns:
A reflection object that represents the method
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the method cannot be accessed
java.lang.NoSuchMethodException - If the method cannot be found in the specified class

findMethod

public static java.lang.reflect.Method findMethod(java.lang.String className,
                                                  java.lang.String methodName,
                                                  java.lang.Class[] args)
                                           throws java.lang.ClassNotFoundException,
                                                  java.lang.SecurityException,
                                                  java.lang.NoSuchMethodException
Finds the specified method in the given class.

Parameters:
className - The class name
methodName - The name of the method to find in the class
args - The method argument types
Returns:
A reflection object that represents the method
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the method cannot be accessed
java.lang.NoSuchMethodException - If the method cannot be found in the specified class

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Object invokeObj,
                                                  java.lang.String methodName)
                                           throws java.lang.ClassNotFoundException,
                                                  java.lang.SecurityException,
                                                  java.lang.NoSuchMethodException
Finds the specified method in the given instance of a class

Parameters:
invokeObj - The object to use when searching for the method
methodName - The name of the method to find in the class
Returns:
A reflection object that represents the method
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the method cannot be accessed
java.lang.NoSuchMethodException - If the method cannot be found in the specified class

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Object invokeObj,
                                                  java.lang.String methodName,
                                                  java.lang.Class[] args)
                                           throws java.lang.ClassNotFoundException,
                                                  java.lang.SecurityException,
                                                  java.lang.NoSuchMethodException
Finds the specified method in the given instance of a class

Parameters:
invokeObj - The object to use when searching for the method
methodName - The name of the method to find in the class
args - The method argument types
Returns:
A reflection object that represents the method
Throws:
java.lang.ClassNotFoundException - If the specified class cannot be found
java.lang.SecurityException - If the method cannot be accessed
java.lang.NoSuchMethodException - If the method cannot be found in the specified class

classImplementsInterface

public static boolean classImplementsInterface(java.lang.Class theClass,
                                               java.lang.String interfaceName,
                                               java.util.logging.Logger logger)
Checks if the given class implements the specified interface

Parameters:
theClass - The class to check
interfaceName - The interface to check for
logger - Optional. Logger (fine) for class interface details
Returns:
true if the class implements the interface, false if it does not

getGlobalServiceLogger

public static java.util.logging.Logger getGlobalServiceLogger()
Retrieves a reference to the global ARE logger. This is the same logger that is used and shared by all ARE plugins.

Returns:
A reference to the global ARE logger

createEmptyLogger

public static java.util.logging.Logger createEmptyLogger()
Creates an empty logger, with no log handlers. Typically used when a valid logger object is required, but no logging is desired.

Returns:
An empty logger with no log handlers

createLogger

public static java.util.logging.Logger createLogger(java.lang.String packageName,
                                                    java.lang.String logFileName,
                                                    int maxSize,
                                                    int maxNumber)
Creates a new logger with the given specifications

Parameters:
packageName - Package name associated with the logger
logFileName - Name of the log file
maxSize - Maximum size, in bytes, that the log file can grow to
maxNumber - Maximum number of log files that can be created before the oldest log file is deleted.
Returns:
The newly created logger