com.sibvisions.util.type
Class ResourceUtil

java.lang.Object
  extended by com.sibvisions.util.type.ResourceUtil

public final class ResourceUtil
extends Object

The ResourceUtil contains resource and class dependent utility methods.


Method Summary
static void addResourceArchive(int pIndex, IResourceArchive pArchive)
          Adds a resource archive to the given position in the list of known archives.
static void addResourceArchive(IResourceArchive pArchive)
          Adds a resource archive to the list of known archives.
static boolean getAccessibleBoolean(String pName, boolean pDefaultValue)
          Gets the boolean value of a system property, if the property is accessible.
static String getAccessibleProperty(String pName, String pDefaultValue)
          Gets the string value of a system property, if the property is accessible.
static File getFileForClass(ClassLoader pLoader, String pClassName)
          Searches the file in which a class is stored.
static File getFileForClass(String pClassName)
          Searches the file in which a class is stored.
static String getFqClassName(Object pScope)
          Returns the full qualified class name for an object.
static Class<?>[] getInterfaces(Class<?> pClass, boolean pCheckInterfaces, Class<?>... pIncluded)
          Gets interfaces from a specific class and all parent classes, recursively.
static void getInterfaces(List<Class<?>> pInterfaces, Class<?> pClass, boolean pCheckInterfaces, Class<?>... pIncluded)
          Gets interfaces from a specific class and all parent classes, recursively.
static String getLocationForClass(ClassLoader pLoader, String pClassName)
          Searches the location where a class is stored.
static String getLocationForClass(String pClassName)
          Searches the location where a class is stored.
static String getName(Class<?> pClass)
          Gets the name of a class.
static String getPackage(Class<?> pClass)
          Gets the package name from a class independent of Class.getPackage().
static String getPathForClass(String pClassName)
          Searches the directory where a class is stored.
static URL getResource(ClassLoader pLoader, String pResource)
          Finds a resource with a given name.
static URL getResource(String pResource)
          Finds a resource with a given name.
static List<IResourceArchive> getResourceArchives()
          Gets the current list of known resource archives.
static InputStream getResourceAsStream(ClassLoader pLoader, String pResource)
          Finds a resource with a given name.
static InputStream getResourceAsStream(String pResource)
          Finds a resource with a given name.
static ClassLoader getResourceClassLoader(Object pObject)
          Get the current class loader for a given object.
static void removeAllResourceArchives()
          Removes all resource archives from the list of known archives.
static void removeResourceArchive(IResourceArchive pArchive)
          Removes a resource archive from the list of known archives.
static void setDefaultClassLoader(ClassLoader pClassLoader)
          Sets the default class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addResourceArchive

public static void addResourceArchive(IResourceArchive pArchive)
Adds a resource archive to the list of known archives.

Parameters:
pArchive - the resource archive

addResourceArchive

public static void addResourceArchive(int pIndex,
                                      IResourceArchive pArchive)
Adds a resource archive to the given position in the list of known archives.

Parameters:
pIndex - the list index
pArchive - the resource archive

removeResourceArchive

public static void removeResourceArchive(IResourceArchive pArchive)
Removes a resource archive from the list of known archives.

Parameters:
pArchive - the resource archive

removeAllResourceArchives

public static void removeAllResourceArchives()
Removes all resource archives from the list of known archives.


getResourceArchives

public static List<IResourceArchive> getResourceArchives()
Gets the current list of known resource archives.

Returns:
the list of resource archives

getResourceAsStream

public static InputStream getResourceAsStream(String pResource)
Finds a resource with a given name. The search strategy is
  1. search a file with pResource as absolute path
  2. extract the filename of pResource, if it's an absolute path, and search in the classes or lib directory where this util class is stored
  3. delegate the search to the ClassLoader

Parameters:
pResource - one of the following: /package/<classname>.class, C:\temp\config.xml, /tmp/config.xml
Returns:
the resource stream or null if no resource with this name is found
See Also:
getResourceAsStream(ClassLoader, String)

getResource

public static URL getResource(String pResource)
Finds a resource with a given name. The search strategy is
  1. search a file with pResource as absolute path
  2. extract the filename of pResource, if it's an absolute path, and search in the classes or lib directory where this util class is stored
  3. delegate the search to the ClassLoader

Parameters:
pResource - one of the following: /package/<classname>.class, C:\temp\config.xml, /tmp/config.xml
Returns:
the resource URL or null if no resource with this name is found
See Also:
getResource(ClassLoader, String)

getResourceAsStream

public static InputStream getResourceAsStream(ClassLoader pLoader,
                                              String pResource)
Finds a resource with a given name. The search strategy is
  1. search a file with pResource as absolute path
  2. extract the filename of pResource, if it's an absolute path, and search in the classes or lib directory where this util class is stored
  3. delegate the search to the ClassLoader

Parameters:
pLoader - a specific class loader or null to use the system class loader.
pResource - one of the following: /package/<classname>.class, C:\temp\config.xml, /tmp/config.xml
Returns:
the resource stream or null if no resource with this name is found

getResource

public static URL getResource(ClassLoader pLoader,
                              String pResource)
Finds a resource with a given name. The search strategy is
  1. search a file with pResource as absolute path
  2. extract the filename of pResource, if it's an absolute path, and search in the classes or lib directory where this util class is stored
  3. delegate the search to the ClassLoader

Parameters:
pLoader - a specific class loader or null to use the system class loader.
pResource - one of the following: /package/<classname>.class, C:\temp\config.xml, /tmp/config.xml
Returns:
the resource URL or null if no resource with this name is found

getPathForClass

public static String getPathForClass(String pClassName)
Searches the directory where a class is stored. If the class is part of a jar archive then the directory in which the jar is stored will be used.

Parameters:
pClassName - z.B.: /package/<class>.class
Returns:
directory in which the class is stored or null if the class cann not be found

getLocationForClass

public static String getLocationForClass(String pClassName)
Searches the location where a class is stored. It can be a directory or jar file.

Parameters:
pClassName - z.B.: /package/<class>.class
Returns:
diretory, jar file or null if the class can not be found in the classpath
See Also:
getLocationForClass(ClassLoader, String)

getLocationForClass

public static String getLocationForClass(ClassLoader pLoader,
                                         String pClassName)
Searches the location where a class is stored. It can be a directory or jar file.

Parameters:
pLoader - a specific class loader or null to use the system class loader.
pClassName - z.B.: /package/<class>.class
Returns:
diretory, jar file or null if the class can not be found in the classpath

getFileForClass

public static File getFileForClass(String pClassName)
Searches the file in which a class is stored. It can be a class- or jar file.

Parameters:
pClassName - z.B.: /package/<class>.class
Returns:
class-, jar file or null if the class can not be found in the classpath
See Also:
getFileForClass(ClassLoader, String)

getFileForClass

public static File getFileForClass(ClassLoader pLoader,
                                   String pClassName)
Searches the file in which a class is stored. It can be a class- or jar file.

Parameters:
pLoader - a specific class loader or null to use the system class loader
pClassName - z.B.: /package/<class>.class
Returns:
class-, jar file or null if the class can not be found in the classpath

getFqClassName

public static String getFqClassName(Object pScope)
Returns the full qualified class name for an object.

Parameters:
pScope - a class or object instance
Returns:
the full qualified class name e.g. /package/<class>.class

getName

public static String getName(Class<?> pClass)
Gets the name of a class. It checks if the class is an anonymous or inner class.

Parameters:
pClass - class from which you want the name
Returns:
full qualified class name e.g. com.sibvisions.rad.util.type.ResourceUtil

getAccessibleProperty

public static String getAccessibleProperty(String pName,
                                           String pDefaultValue)
Gets the string value of a system property, if the property is accessible. Some properties are not accessible in applets. In that case the default value will be returned.

Parameters:
pName - the name of the system property
pDefaultValue - the default value if the property is null or is not accessible
Returns:
the value of the system property, if accessible and not is null, otherwise the pDefaultValue

getAccessibleBoolean

public static boolean getAccessibleBoolean(String pName,
                                           boolean pDefaultValue)
Gets the boolean value of a system property, if the property is accessible. Some properties are not accessible in applets. In that case the default value will be returned.

Parameters:
pName - the name of the system property
pDefaultValue - the default value if the property is null or is not accessible
Returns:
the value of the system property, if accessible and not is null, otherwise the pDefaultValue

getPackage

public static String getPackage(Class<?> pClass)
Gets the package name from a class independent of Class.getPackage().

Parameters:
pClass - the loaded class
Returns:
the package name for the class

setDefaultClassLoader

public static void setDefaultClassLoader(ClassLoader pClassLoader)
Sets the default class loader. The default class loader will be used if not specific class loader is defined when loading resources.

Parameters:
pClassLoader - a class loader

getResourceClassLoader

public static ClassLoader getResourceClassLoader(Object pObject)
Get the current class loader for a given object. If a default class loader is set, the default class loader is returned. If no default class loader is set, the class loader of the object is used. If the object is null the class loader of this util class is returned.

Parameters:
pObject - the object
Returns:
the class loader

getInterfaces

public static Class<?>[] getInterfaces(Class<?> pClass,
                                       boolean pCheckInterfaces,
                                       Class<?>... pIncluded)
Gets interfaces from a specific class and all parent classes, recursively. It also is possible to get the parent interfaces from an interface.

Parameters:
pClass - the start class
pCheckInterfaces - true to get all parent interfaces from a detected interface
pIncluded - the allowed interfaces. A detected interface must be an instance of at least one interface in the included list. The list won't be used for super interfaces.
Returns:
the list of found interfaces

getInterfaces

public static void getInterfaces(List<Class<?>> pInterfaces,
                                 Class<?> pClass,
                                 boolean pCheckInterfaces,
                                 Class<?>... pIncluded)
Gets interfaces from a specific class and all parent classes, recursively. It also is possible to get the parent interfaces from an interface.

Parameters:
pInterfaces - all found interfaces
pClass - the start class
pCheckInterfaces - true to get all parent interfaces from a detected interface
pIncluded - the allowed interfaces. A detected interface must be an instance of at least one interface in the included list. The list won't be used for super interfaces.


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.