org.fuin.utils4j
Class Utils4J

java.lang.Object
  extended by org.fuin.utils4j.Utils4J

public final class Utils4J
extends java.lang.Object

Common utility methods for use in Java applications and libraries.


Nested Class Summary
static interface Utils4J.UnzipInputStreamWrapper
          Wraps a given input stream into another one an returns it.
 
Method Summary
static void addToClasspath(java.lang.String url)
          Adds an URL to the classpath.
static void addToClasspath(java.net.URL url)
          Adds an URL to the classpath.
static void checkNotNull(java.lang.String name, java.lang.Object value)
          Checks if a variable is not null and throws an IllegalNullArgumentException if this rule is violated.
static void checkValidDir(java.io.File dir)
          Check if the argument is an existing directory.
static void checkValidFile(java.io.File file)
          Check if the argument is an existing file.
static boolean containsURL(java.net.URL[] urls, java.net.URL url)
          Checks if the array or URLs contains the given URL.
static java.lang.String createHash(java.io.File file)
          Creates an MD5 hash from a file.
static java.lang.Object createInstance(java.lang.String className)
          Create an instance with Class.forName(..) and wrap all exceptions into RuntimeExceptions.
static java.net.URL createUrl(java.net.URL baseUrl, java.lang.String path, java.lang.String filename)
          Creates an URL based on a directory a relative path and a filename.
static java.lang.String getPackagePath(java.lang.Class clasz)
          Returns the package path of a class.
static java.lang.String getRelativePath(java.io.File baseDir, java.io.File dir)
          Returns a relative path based on a base directory.
static java.net.URL getResource(java.lang.Class clasz, java.lang.String name)
          Get the path to a resource located in the same package as a given class.
static java.io.File getUserHomeDir()
          Returns the user home directory and checks if it is valid and exists.
static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName, java.lang.Class[] argTypes, java.lang.Object[] args)
          Calls a method with reflection and maps all errors into one exception.
static java.util.Properties loadProperties(java.lang.Class clasz, java.lang.String filename)
          Load properties from classpath.
static java.util.Properties loadProperties(java.io.File file)
          Load properties from a file.
static java.util.Properties loadProperties(java.net.URL baseUrl, java.lang.String filename)
          Load a file from an directory.
static void saveProperties(java.io.File file, java.util.Properties props, java.lang.String comment)
          Save properties to a file.
static void unzip(java.io.File zipFile, java.io.File destDir)
          Unzips a file into a given directory.
static void unzip(java.io.File zipFile, java.io.File destDir, Utils4J.UnzipInputStreamWrapper wrapper, Cancelable cancelable)
          Unzips a file into a given directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPackagePath

public static java.lang.String getPackagePath(java.lang.Class clasz)
Returns the package path of a class.

Parameters:
clasz - Class to determine the path for.
Returns:
Package path for the class.

getResource

public static java.net.URL getResource(java.lang.Class clasz,
                                       java.lang.String name)
Get the path to a resource located in the same package as a given class.

Parameters:
clasz - Class with the same package where the resource is located.
name - Filename of the resource.
Returns:
Resource URL.

loadProperties

public static java.util.Properties loadProperties(java.lang.Class clasz,
                                                  java.lang.String filename)
Load properties from classpath.

Parameters:
clasz - Class in the same package as the properties file - Cannot be null.
filename - Name of the properties file (without path) - Cannot be null.
Returns:
Properties.

loadProperties

public static java.util.Properties loadProperties(java.io.File file)
Load properties from a file.

Parameters:
file - Properties file - Cannot be null.
Returns:
Properties.

checkValidFile

public static void checkValidFile(java.io.File file)
Check if the argument is an existing file. If the check fails an IllegalArgumentException is thrown.

Parameters:
file - File to check - Cannot be null.

checkValidDir

public static void checkValidDir(java.io.File dir)
Check if the argument is an existing directory. If the check fails an IllegalArgumentException is thrown.

Parameters:
dir - Directory to check - Cannot be null.

saveProperties

public static void saveProperties(java.io.File file,
                                  java.util.Properties props,
                                  java.lang.String comment)
Save properties to a file.

Parameters:
file - Destination file - Cannot be null.
props - Properties to save - Cannot be null.
comment - Comment for the file.

createInstance

public static java.lang.Object createInstance(java.lang.String className)
Create an instance with Class.forName(..) and wrap all exceptions into RuntimeExceptions.

Parameters:
className - Full qualified class name.
Returns:
New instance of the class.

addToClasspath

public static void addToClasspath(java.lang.String url)
Adds an URL to the classpath.

Parameters:
url - URL to add.

containsURL

public static boolean containsURL(java.net.URL[] urls,
                                  java.net.URL url)
Checks if the array or URLs contains the given URL.

Parameters:
urls - Array of URLs.
url - URL to find.
Returns:
If the URL is in the array TRUE else FALSE.

createHash

public static java.lang.String createHash(java.io.File file)
Creates an MD5 hash from a file.

Parameters:
file - File to create an hash for.
Returns:
Hash as text.

createUrl

public static java.net.URL createUrl(java.net.URL baseUrl,
                                     java.lang.String path,
                                     java.lang.String filename)
Creates an URL based on a directory a relative path and a filename.

Parameters:
baseUrl - Directory URL.
path - Relative path inside the base URL.
filename - Filename without path.
Returns:
URL.

getRelativePath

public static java.lang.String getRelativePath(java.io.File baseDir,
                                               java.io.File dir)
Returns a relative path based on a base directory. If the dir is not inside baseDir an IllegalArgumentException is thrown.

Parameters:
baseDir - Base directory the path is relative to.
dir - Directory inside the base directory.
Returns:
Path of dir relative to baseDir. If both are equal an empty string is returned.

loadProperties

public static java.util.Properties loadProperties(java.net.URL baseUrl,
                                                  java.lang.String filename)
Load a file from an directory.

Parameters:
baseUrl - Directory URL.
filename - Filename without path.
Returns:
Properties.

addToClasspath

public static void addToClasspath(java.net.URL url)
Adds an URL to the classpath.

Parameters:
url - URL to add.

checkNotNull

public static void checkNotNull(java.lang.String name,
                                java.lang.Object value)
Checks if a variable is not null and throws an IllegalNullArgumentException if this rule is violated.

Parameters:
name - Name of the variable to be displayed in an error message.
value - Value to check for null.

invoke

public static java.lang.Object invoke(java.lang.Object obj,
                                      java.lang.String methodName,
                                      java.lang.Class[] argTypes,
                                      java.lang.Object[] args)
                               throws InvokeMethodFailedException
Calls a method with reflection and maps all errors into one exception.

Parameters:
obj - The object the underlying method is invoked from.
methodName - Name of the Method.
argTypes - The list of parameters.
args - Arguments the arguments used for the method call.
Returns:
The result of dispatching the method represented by this object on obj with parameters args.
Throws:
InvokeMethodFailedException - Invoking the method failed for some reason.

unzip

public static void unzip(java.io.File zipFile,
                         java.io.File destDir)
                  throws java.io.IOException
Unzips a file into a given directory. WARNING: Only relative path entries are allowed inside the archive!

Parameters:
zipFile - Source ZIP file - Cannot be null and must be a valid ZIP file.
destDir - Destination directory - Cannot be null and must exist.
Throws:
java.io.IOException - Error unzipping the file.

unzip

public static void unzip(java.io.File zipFile,
                         java.io.File destDir,
                         Utils4J.UnzipInputStreamWrapper wrapper,
                         Cancelable cancelable)
                  throws java.io.IOException
Unzips a file into a given directory. WARNING: Only relative path entries are allowed inside the archive!

Parameters:
zipFile - Source ZIP file - Cannot be null and must be a valid ZIP file.
destDir - Destination directory - Cannot be null and must exist.
wrapper - Callback interface to give the caller the chance to wrap the ZIP input stream into another one. This is useful for example to display a progress bar - Can be null if no wrapping is required.
cancelable - Signals if the unzip should be canceled - Can be null if no cancel option is required.
Throws:
java.io.IOException - Error unzipping the file.

getUserHomeDir

public static java.io.File getUserHomeDir()
Returns the user home directory and checks if it is valid and exists. If not a IllegalStateException is thrown.

Returns:
Directory.


Copyright © 2009 Future Invent Informationsmanagement GmbH. All Rights Reserved.