be.erwinsmout
Class MyResource

java.lang.Object
  extended by be.erwinsmout.MyResource

public abstract class MyResource
extends java.lang.Object

Author:
Erwin

Method Summary
static java.lang.String getPackageResourceBaseName(java.lang.Class<? extends java.lang.Object> clazz, java.lang.String resourceTypeName)
          Gets the base name for the resource of the indicated type corresponding to the package of the given class.
static java.lang.String getResourceBaseName(java.lang.Class<? extends java.lang.Object> clazz, java.lang.String resourceTypeName)
          Gets the base name for the resource of the indicated type corresponding to the given class.
static java.io.InputStream getResourceInputStream(java.lang.Class<?> clazz, java.lang.String resourceName)
          Attempts to find a resource of the given name, and returns an input stream for reading that resource if it can be found.
static java.io.InputStream getResourceInputStream(java.lang.String resourceName)
          Attempts to find a resource of the given name, and returns an input stream for reading that resource if it can be found.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPackageResourceBaseName

public static java.lang.String getPackageResourceBaseName(java.lang.Class<? extends java.lang.Object> clazz,
                                                          java.lang.String resourceTypeName)
Gets the base name for the resource of the indicated type corresponding to the package of the given class. This is as follows (first rule that applies) :
  1. full packagename does not end with '<resourceTypeName>' (case insensitive) ===> full packagename, with either of dots and forward slashes as separators, suffixed with '.<resourceTypeName>' (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties")
  2. full packagename ends with '.<resourceTypeName>', or is equal to '<resourceTypeName>' (both case insensitive) ===> full packagename, with either of dots and forward slashes as separators (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties", or just "properties")
  3. full packagename ends with '<resourceTypeName>', not preceded by a dot (i.e. X.Y.Z<resourceTypeName>) ===> X.Y.Z, with either of dots and forward slashes as separators, suffixed with '.<resourceTypeName>' (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties")

Parameters:
clazz - the class for which the corresponding properties resource name is to be returned
resourceTypeName - indication of the type of resource, e.g. 'properties' or 'keystore'
Returns:
the base name for the resource of the indicated type corresponding to the given class.

getResourceBaseName

public static java.lang.String getResourceBaseName(java.lang.Class<? extends java.lang.Object> clazz,
                                                   java.lang.String resourceTypeName)
Gets the base name for the resource of the indicated type corresponding to the given class. This is as follows (first rule that applies) :
  1. full classname does not end with '<resourceTypeName>' (case insensitive) ===> full classname, suffixed with '.<resourceTypeName>' (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties")
  2. full classname ends with '.<resourceTypeName>', or is equal to '<resourceTypeName>' (both case insensitive) ===> full classname, with either of dots and forward slashes as separators (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties", or just "properties")
  3. full classname ends with '<resourceTypeName>', not preceded by a dot (i.e. X.Y.Z<resourceTypeName>) ===> X.Y.Z, with either of dots and forward slashes as separators, suffixed with '.<resourceTypeName>' (e.g. "com.foo.Bar.properties" and "com/foo/Bar.properties")

Parameters:
clazz - the class for which the corresponding properties resource name is to be returned
resourceTypeName - indication of the type of resource, e.g. 'properties' or 'keystore'
Returns:
the base name for the resource of the indicated type corresponding to the given class.

getResourceInputStream

public static java.io.InputStream getResourceInputStream(java.lang.Class<?> clazz,
                                                         java.lang.String resourceName)
                                                  throws NotFoundException
Attempts to find a resource of the given name, and returns an input stream for reading that resource if it can be found. The search path for finding the resource is as follows :
  1. The classpath that is available to the ClassLoader of the clazz class. That path is searched using the given class' getResourceAsStream() method, using the modified resourcename. The 'modified resourcename' is different from the resourcename itself only if the resourcename starts with the same tokens as the clazz' package name (in which case the dot-separated package name prefix in the resourcename is replaced with a forward-slash-separated '/' name, and a forward slash is prepended. Following cases are thus possible (clazz is com.foo.Bar in each case):
    • resourcename com.foo.Bar - classpath search is for /com/foo/Bar
    • resourcename com.foo.Bar.properties - classpath search is for /com/foo/Bar.properties
    • resourcename net.foo.Bar.properties - classpath search is for net.foo.Bar.properties (implying that java will conduct a search for /com/foo/net.foo.Bar.properties)
    • resourcename Bar.properties - classpath search is for Bar.properties (implying that java will conduct a search for /com/foo/Bar.properties)
  2. A file of the same name as the requested resource in the directory pointed to by the user.dir property.
  3. A file of the same name as the requested resource in the directory pointed to by the user.home property.

Parameters:
clazz - The class object representing the class of the object for which the resource is to be located
resourceName - The name of the resource to be searched
Returns:
An inputstream for reading the requested resource
Throws:
NotFoundException - If no resource of the indicated name could be found

getResourceInputStream

public static java.io.InputStream getResourceInputStream(java.lang.String resourceName)
                                                  throws NotFoundException
Attempts to find a resource of the given name, and returns an input stream for reading that resource if it can be found. The search path for finding the resource is as follows :
  1. A file of the same name as the requested resource in the directory pointed to by the user.dir property.
  2. A file of the same name as the requested resource in the directory pointed to by the user.home property.

Parameters:
resourceName - The name of the resource to be searched
Returns:
An inputstream for reading the requested resource
Throws:
NotFoundException - If no resource of the indicated name could be found