|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.ClassLoader
org.mmbase.util.ResourceLoader
MMBase resource loader, for loading config-files and those kind of things. It knows about MMBase config file locations. I read http://www.javaworld.com/javaqa/2003-08/02-qa-0822-urls.html. Programmers should do something like this if they need a configuration file:
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.
| Nested Class Summary | |
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 |
| Field Summary | |
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. |
static String |
RESOURCENAME_FIELD
|
static int |
TYPE_CONFIG
|
static String |
TYPE_FIELD
|
static int |
TYPE_WEB
|
static Pattern |
XML_PATTERN
Can be used as an argument for getResourcePaths(Pattern, boolean). |
| Constructor Summary | |
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. |
| Method Summary | |
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 |
findResources(String name)
|
Set |
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. |
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 |
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(String name)
Returns a reader for a given resource. |
List |
getResourceList(String name)
Returns a List, containing all URL's which may represent the given resource. |
Set |
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 |
getResourcePaths(Pattern pattern,
boolean recursive,
boolean directories)
Used by getResourcePaths(Pattern, boolean) and getChildContexts(Pattern, boolean) |
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(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 |
Writer |
getWriter(String name)
Returns a writer for a given resource, so that you can overwrite or create it. |
int |
hashCode()
|
static void |
init(ServletContext sc)
Initializes the Resourceloader using a servlet-context (makes resolving relatively to WEB-INF/config possible). |
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(NodeManager b)
Sets the MMBase builder which must be used for resource. |
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()
|
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
protected 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
public static final int TYPE_CONFIG
public static final int TYPE_WEB
public static final Pattern XML_PATTERN
getResourcePaths(Pattern, boolean). MMBase works mainly
with xml configuration files, so this comes in handy.
| Constructor Detail |
protected ResourceLoader()
getConfigurationRoot() or getWebRoot().
protected ResourceLoader(ResourceLoader cl,
String context)
getChildResourceLoader(java.lang.String).
| Method Detail |
protected URL newURL(String url)
throws MalformedURLException
MalformedURLExceptionpublic static void init(ServletContext sc)
sc - The ServletContext used for determining the mmbase configuration directory. Or null.public static void setResourceBuilder(NodeManager b)
b - An MMObjectBuilder (this may be null if no such builder available)
RuntimeException - if builder was set already.public static String getName(String path)
public static String getDirectory(String path)
public static ResourceLoader getConfigurationRoot()
public static ResourceLoader getSystemRoot()
getConfigurationRoot() or getWebRoot().
public static ResourceLoader getWebRoot()
public static ResourceLoader getWebDirectory(HttpServletRequest request)
protected URL findResource(String name)
protected Enumeration findResources(String name)
throws IOException
IOExceptiongetResourceList(java.lang.String)public List getResourceList(String name)
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 loader
ResourceLoader(ResourceLoader, String)
public Set 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 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 getResourcePaths(Pattern pattern,
boolean recursive,
boolean directories)
getResourcePaths(Pattern, boolean) and getChildContexts(Pattern, boolean)
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 - getResourceContext supplies true getResourcePaths supplies false
public 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 loaded
null otherwise.
IOException
public static InputSource getInputSource(URL url)
throws IOException
getInputSource(String), can e.g. be used in combination with getResourceList(String)
IOException
public Document getDocument(String name)
throws SAXException,
IOException
name - The name of the resource to be loaded
null if there is no such resource.
SAXException - If the resource does not present parseable XML.
IOException
public 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 serach for the validating xsd or dtd
null if there is no such resource.
SAXException - If the resource does not present parseable XML.
IOException
public 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)
SAXException
IOException
public 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(String name)
throws IOException
IOExceptionClassLoader.getResourceAsStream(String)
public Writer getWriter(String name)
throws IOException
getReader(java.lang.String), but then inversed.
IOExceptiongetReader(String),
createResourceAsStream(String)public String toInternalForm(String name)
findResource(name).toString() would give an 'external' form.
public static String toInternalForm(URL u)
public List getFiles(String name)
ResourceWatcher. And by some deprecated code that wants to produce File objects.
public String toString()
Object.toString()public boolean equals(Object o)
Object.equals(java.lang.Object)public int hashCode()
Object.hashCode()public static void main(String[] argv)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||