public class ResourceLoader extends ClassLoader
InputStream configStream = ResourceLoader.getConfigurationRoot().getResourceAsStream("modules/myconfiguration.xml");
or
InputSource config = ResourceLoader.getConfigurationRoot().getInputSource("modules/myconfiguration.xml");
of if you need a list of all resources:
ResourceLoader builderLoader = new ResourceLoader("builders");
List list = builderLoader.getResourcePaths(ResourceLoader.XML_PATTERN, true)
When you want to place a configuration file then you have several options, wich are in order of preference:
getFiles(java.lang.String).getFiles(java.lang.String).
Resources which do not reside in the MMBase configuration repository, can also be handled. Those can be resolved relatively to the web root, using getWebRoot().
Resources can programmaticly created or changed by the use of createResourceAsStream(java.lang.String), or something like getWriter(java.lang.String).
If you want to check beforehand if a resource can be changed, then something like resourceLoader.getResource().openConnection().getDoOutput() can be used.
That is also valid if you want to check for existance. resourceLoader.getResource().openConnection().getDoInput().
If you want to remove a resource, you must write null to all URL's returned by findResources(java.lang.String) (Do for every URL:url.openConnection().getOutputStream().write(null);)
ResourceLoader is well aware of encodings. You can open XML's as Reader, and this will be done using the encoding specified in the XML itself. When saving an XML using a Writer, this will also be done using the encoding specified in the XML.
For property-files, the java-unicode-escaping is undone on loading, and applied on saving, so there is no need to think of that.
The Classloader itself reads the resources config/utils/resourceloader.xml. This
is mainly to attribute weights to certain resources. See getWeight(java.net.URL). This is used if a
resource is available more than once (e.g. in several distinct jars in WEB-INF/lib), to determin
the order they appear in getResourceList(java.lang.String). E.g. an mmbase application providing a
security implementation would give a large weight to its own
config/security/security.xml, the ratio being that if you install a security
implementation, you probably want to use it too.
Take a look e.g. at A typical resourceloader.xml. In this example every other resource in the same jar as the resourceloader.xml itself (the '!' when used as the first character of the key means 'replace this with the URL of this jar') is attributed a relatively big weight (default it is 0).
| Modifier and Type | Class and Description |
|---|---|
protected class |
ResourceLoader.AbstractFileURLStreamHandler |
protected class |
ResourceLoader.ApplicationContextFileURLStreamHandler |
protected class |
ResourceLoader.ClassLoaderURLStreamHandler |
protected class |
ResourceLoader.FileURLStreamHandler |
protected class |
ResourceLoader.NodeURLStreamHandler
URLStreamHandler for NodeConnections.
|
protected class |
ResourceLoader.PathURLStreamHandler
Extension URLStreamHandler, used for the 'sub' Handlers, entries of 'roots' in ResourceLoader are of this type.
|
protected class |
ResourceLoader.ServletResourceURLStreamHandler
URLStreamHandler based on the servletContext object of ResourceLoader
|
static class |
ResourceLoader.Type
The available ResourceLoaders wrapped into an enum, which makes them available dynamicly
(with
ResourceLoader.Type.valueOf(string).get()) |
| Modifier and Type | Field and Description |
|---|---|
protected static String |
CLASSLOADER_ROOT
Used when getting resources with normal class-loader.
|
static String |
DEFAULT_CONTEXT |
static String |
FILENAME_FIELD |
static String |
HANDLE_FIELD |
protected static String |
INDEX
Used when using getResourcePaths for normal class-loaders.
|
static String |
LASTMODIFIED_FIELD |
static URL |
NODE_URL_CONTEXT
Protocol prefix used by URL objects in this class.
|
protected static String |
PROTOCOL
Protocol prefix used by URL objects in this class.
|
protected static String |
RESOURCE_ROOT
Used for files, and servlet resources.
|
(package private) static String |
resourceBuilder |
static String |
RESOURCENAME_FIELD |
(package private) List<ResourceLoader.PathURLStreamHandler> |
roots |
static String |
TYPE_FIELD |
static Pattern |
XML_PATTERN
Can be used as an argument for
getResourcePaths(Pattern, boolean). |
| Modifier | Constructor and Description |
|---|---|
protected |
ResourceLoader()
This constructor instantiates a new root resource-loader.
|
protected |
ResourceLoader(ResourceLoader cl,
String context)
Instantiates a ResourceLoader for a 'sub directory' of given ResourceLoader.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
checkShadowedNewerResources(String name)
Logs warning if 'newer' resources are shadowed by older ones.
|
OutputStream |
createResourceAsStream(String name)
If you want to change a resource, or create one, then this method can be used.
|
boolean |
equals(Object o) |
protected URL |
findResource(String name)
If name starts with '/' or 'mm:/' the 'parent' resourceloader is used.
|
protected Enumeration<URL> |
findResources(String name) |
Set<String> |
getChildContexts(Pattern pattern,
boolean recursive)
Returns a set of context strings which can be used to instantiated new ResourceLoaders (resource loaders for directories)
(see
getChildResourceLoader(String)). |
ResourceLoader |
getChildResourceLoader(String context) |
static ResourceLoader |
getConfigurationRoot()
Singleton that returns the ResourceLoader for loading mmbase configuration
|
URL |
getContext()
Returns the 'context' for the ResourceLoader (an URL).
|
static String |
getDirectory(String path)
Utility method to return the 'directory' part of a resource-name.
|
static String |
getDirectoryName(String path)
Utility method to return the name of the directory of a resource-name.
|
Document |
getDocument(String name)
Returns the givens resource as a Document (parsed XML).
|
Document |
getDocument(String name,
boolean validation,
Class<?> baseClass)
Returns the givens resource as a Document (parsed XML).
|
static Document |
getDocument(URL url,
boolean validation,
Class<?> baseClass)
Static version of
getDocument(String, boolean, Class), can e.g. |
List<File> |
getFiles(String name)
Used by
ResourceWatcher. |
InputSource |
getInputSource(String name)
Returns the givens resource as an InputSource (XML streams).
|
static InputSource |
getInputSource(URL url)
Static version of
getInputSource(String), can e.g. |
static String |
getName(String path)
Utility method to return the name part of a resource-name (removed directory and 'extension').
|
ResourceLoader |
getParentResourceLoader()
Returns the 'parent' ResourceLoader.
|
Reader |
getReader(InputStream is,
String name)
A version of
getReader(String) which accepts the result of ClassLoader.getResourceAsStream(java.lang.String). |
Reader |
getReader(String name)
Returns a reader for a given resource.
|
URL |
getResource(String name) |
(package private) static NodeManager |
getResourceBuilder() |
List<URL> |
getResourceList(String name)
Returns a List, containing all URL's which may represent the
given resource.
|
(package private) Node |
getResourceNode(String name) |
Set<String> |
getResourcePaths(Pattern pattern,
boolean recursive)
Returns a set of 'sub resources' (read: 'files in the same directory'), which can succesfully be loaded by the ResourceLoader.
|
protected Set<String> |
getResourcePaths(Pattern pattern,
boolean recursive,
boolean directories)
|
static ResourceLoader |
getSystemRoot()
Singleton that returns the ResourceLoader for loading from the file-system, relative from
current working directory and falling back to the file system roots.
|
static ResourceLoader |
getWebDirectory(javax.servlet.http.HttpServletRequest request)
Returns the resource loader associated with the directory of the given request
|
static ResourceLoader |
getWebRoot()
Singleton that returns the ResourceLoader for witch the base path is the web root
|
static int |
getWeight(URL u)
Given an
URL, which can be an entry of the result of getResourceList(java.lang.String) or
ClassLoader.getResources(java.lang.String), returns its 'weight'. |
Writer |
getWriter(String name)
Returns a writer for a given resource, so that you can overwrite or create it.
|
int |
hashCode() |
static void |
init(javax.servlet.ServletContext sc)
Initializes the Resourceloader using a servlet-context (makes resolving relatively to WEB-INF/config possible).
|
static void |
initLogging() |
static void |
main(String[] argv)
For testing purposes only
|
protected URL |
newURL(String url)
Creates a new URL object, which is used to load resources.
|
static void |
setResourceBuilder(String b)
Sets the MMBase builder which must be used for resource.
|
(package private) URL |
shadowed(File f,
String name)
Determine wether File f is shadowed.
|
void |
storeDocument(String name,
Document doc)
Creates a resource for a given Document.
|
void |
storeSource(String name,
Source source,
DocumentType docType)
Creates a resource with given name for given Source.
|
String |
toInternalForm(String name)
Returns an abstract URL for a resource with given name,
findResource(name).toString() would give an 'external' form. |
static String |
toInternalForm(URL u) |
String |
toString() |
protected static boolean |
validateable(URL url)
sice MMBase-1.9
|
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSignersprotected static final String PROTOCOL
protected static final String RESOURCE_ROOT
protected static final String CLASSLOADER_ROOT
public static final URL NODE_URL_CONTEXT
protected static final String INDEX
public static final String RESOURCENAME_FIELD
public static final String TYPE_FIELD
public static final String FILENAME_FIELD
public static final String HANDLE_FIELD
public static final String LASTMODIFIED_FIELD
public static final String DEFAULT_CONTEXT
static String resourceBuilder
final List<ResourceLoader.PathURLStreamHandler> roots
public static final Pattern XML_PATTERN
getResourcePaths(Pattern, boolean). MMBase works mainly
with xml configuration files, so this comes in handy.protected ResourceLoader()
getConfigurationRoot() or getWebRoot().protected ResourceLoader(ResourceLoader cl, String context)
getChildResourceLoader(java.lang.String).public static void initLogging()
protected URL newURL(String url) throws MalformedURLException
MalformedURLExceptionpublic static void init(javax.servlet.ServletContext sc)
sc - The ServletContext used for determining the mmbase configuration directory. Or null.public static void setResourceBuilder(String b)
b - An String (this may be null if no such builder available)RuntimeException - if builder was set already.static NodeManager getResourceBuilder()
public static String getName(String path)
public static String getDirectory(String path)
public static String getDirectoryName(String path)
public static ResourceLoader getConfigurationRoot()
public static ResourceLoader getSystemRoot()
getConfigurationRoot() or getWebRoot().public static ResourceLoader getWebRoot()
public static ResourceLoader getWebDirectory(javax.servlet.http.HttpServletRequest request)
public URL getResource(String name)
getResource in class ClassLoaderprotected URL findResource(String name)
findResource in class ClassLoaderprotected Enumeration<URL> findResources(String name) throws IOException
findResources in class ClassLoaderIOExceptiongetResourceList(java.lang.String)public List<URL> getResourceList(String name)
getWeight(java.net.URL).public URL getContext()
public ResourceLoader getParentResourceLoader()
null if this ClassLoader has no
parent. You can create a ResourceLoader with a parent by getChildResourceLoader(String).public ResourceLoader getChildResourceLoader(String context)
context - a context relative to the current resource loaderResourceLoader(ResourceLoader, String)public Set<String> getResourcePaths(Pattern pattern, boolean recursive)
pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions applyrecursive - If true, then also subdirectories are searched.public Set<String> getChildContexts(Pattern pattern, boolean recursive)
getChildResourceLoader(String)).pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions applyrecursive - If true, then also subdirectories are searched.protected Set<String> getResourcePaths(Pattern pattern, boolean recursive, boolean directories)
pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions applyrecursive - If true, then also subdirectories are searched.directories - getChildContexts(Pattern, boolean) supplies true getResourcePaths(Pattern, boolean) supplies falsepublic OutputStream createResourceAsStream(String name) throws IOException
findResource(name).openConnection().getOutputStream()
If the given resource already existed, it will be overwritten, or shadowed, if it was not
writeable.IOException - If the Resource for some reason could not be created.public InputSource getInputSource(String name) throws IOException
name - The name of the resource to be loadednull otherwise.IOExceptionpublic static InputSource getInputSource(URL url) throws IOException
getInputSource(String), can e.g. be used in combination with getResourceList(String)IOExceptionpublic Document getDocument(String name) throws SAXException, IOException
name - The name of the resource to be loadednull if there is no such resource.SAXException - If the resource does not present parseable XML.IOExceptionpublic Document getDocument(String name, boolean validation, Class<?> baseClass) throws SAXException, IOException
name - The name of the resource to be loadedvalidation - If true, validate the xml. By dtd if one of the first lines starts with <!DOCTYPE, by XSD otherwisebaseClass - If validation is true, the base class to search for the validating xsd or dtdnull if there is no such resource.SAXException - If the resource does not present parseable XML.IOExceptionprotected static boolean validateable(URL url) throws IOException
IOExceptionpublic static Document getDocument(URL url, boolean validation, Class<?> baseClass) throws SAXException, IOException
getDocument(String, boolean, Class), can e.g. be used in combination with getResourceList(String)SAXExceptionIOExceptionpublic void storeSource(String name, Source source, DocumentType docType) throws IOException
docType - Document which must be used, or null if none.IOExceptioncreateResourceAsStream(String)public void storeDocument(String name, Document doc) throws IOException
name - Name of the resource.doc - The xml document which must be stored.IOExceptioncreateResourceAsStream(String)public Reader getReader(InputStream is, String name) throws IOException
getReader(String) which accepts the result of ClassLoader.getResourceAsStream(java.lang.String).IOExceptionpublic Reader getReader(String name) throws IOException
IOExceptionClassLoader.getResourceAsStream(String)public Writer getWriter(String name) throws IOException
getReader(java.io.InputStream, java.lang.String), but then inversed.IOExceptiongetReader(String),
createResourceAsStream(String)public String toInternalForm(String name)
findResource(name).toString() would give an 'external' form.public List<File> getFiles(String name)
ResourceWatcher. And by some deprecated code that wants to produce File objects.Node getResourceNode(String name)
ResourceWatcher.void checkShadowedNewerResources(String name)
URL shadowed(File f, String name)
name - Check for resource with this namef - The file to check for this resource.null if not shadowed.IllegalArgumentException - if file is not a file associated with the resource with given name.public String toString()
toString in class ObjectObject.toString()public boolean equals(Object o)
equals in class ObjectObject.equals(java.lang.Object)public int hashCode()
hashCode in class ObjectObject.hashCode()public static int getWeight(URL u)
URL, which can be an entry of the result of getResourceList(java.lang.String) or
ClassLoader.getResources(java.lang.String), returns its 'weight'. This is the weight that is used for the order
in the resource list (heavier ones appear earlier).public static void main(String[] argv)
MMBase 1.9-SNAPSHOT - ${javadoctimestamp}