| java.lang.Object java.lang.ClassLoader org.mmbase.util.ResourceLoader
ResourceLoader | public class ResourceLoader extends ClassLoader (Code) | | 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:
- Place it as on object in 'resources' builder (if such a builder is present)
- Place it in the directory identified by the 'mmbase.config' setting (A system property or web.xml setting).
- Place it in the directory WEB-INF/config. If this is a real directory (you are not in a war), then the resource will also be returned by
ResourceLoader.getFiles .
-
Place it in the class-loader path of your app-server, below the 'org.mmbase.config' package.
For tomcat this boils down to the following list (Taken from tomcat 5 class-loader howto)
- Bootstrap classes of your JVM
- System class loader classses
- /WEB-INF/classes of your web application. If this is a real directory (you are not in a war), then the resource will also be returned by
ResourceLoader.getFiles .
- /WEB-INF/lib/*.jar of your web application
- $CATALINA_HOME/common/classes
- $CATALINA_HOME/common/endorsed/*.jar
- $CATALINA_HOME/common/lib/*.jar
- $CATALINA_BASE/shared/classes
- $CATALINA_BASE/shared/lib/*.jar
Resources which do not reside in the MMBase configuration repository, can also be handled. Those can be resolved relatively to the web root, using
ResourceLoader.getWebRoot() .
Resources can programmaticly created or changed by the use of
ResourceLoader.createResourceAsStream , or something like
ResourceLoader.getWriter .
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
ResourceLoader.findResources (Do for every URL:url.openConnection().getOutputStream().write(null); )
Encodings
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.
author: Michiel Meeuwissen since: MMBase-1.8 version: $Id: ResourceLoader.java,v 1.52 2007/12/17 11:36:05 michiel Exp $ |
Inner Class :abstract protected class PathURLStreamHandler extends URLStreamHandler | |
Inner Class :protected class FileURLStreamHandler extends PathURLStreamHandler | |
Inner Class :protected class NodeURLStreamHandler extends PathURLStreamHandler | |
Inner Class :protected class ServletResourceURLStreamHandler extends PathURLStreamHandler | |
Inner Class :protected class ClassLoaderURLStreamHandler extends PathURLStreamHandler | |
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 | |
void | checkShadowedNewerResources(String name) Logs warning if 'newer' resources are shadowed by older ones. | public OutputStream | createResourceAsStream(String name) If you want to change a resource, or create one, then this method can be used. | public boolean | equals(Object o) | protected URL | findResource(String name) If name starts with '/' or 'mm:/' the 'parent' resourceloader is used.
Otherwise the name is resolved relatively. | protected Enumeration<URL> | findResources(String name) | public 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
ResourceLoader.getChildResourceLoader(String) ). | public ResourceLoader | getChildResourceLoader(String context) | public static synchronized ResourceLoader | getConfigurationRoot() | public URL | getContext() Returns the 'context' for the ResourceLoader (an URL). | public static String | getDirectory(String path) Utility method to return the 'directory' part of a resource-name.
Used e.g. | public static String | getDirectoryName(String path) Utility method to return the name of the directory of a resource-name. | public Document | getDocument(String name) Returns the givens resource as a Document (parsed XML). | public Document | getDocument(String name, boolean validation, Class> baseClass) Returns the givens resource as a Document (parsed XML). | public static Document | getDocument(URL url, boolean validation, Class> baseClass) Static version of
ResourceLoader.getDocument(String,boolean,Class) , can e.g. | public List<File> | getFiles(String name) Used by
ResourceWatcher . | public InputSource | getInputSource(String name) Returns the givens resource as an InputSource (XML streams). | public static InputSource | getInputSource(URL url) Static version of
ResourceLoader.getInputSource(String) , can e.g. | public static String | getName(String path) Utility method to return the name part of a resource-name (removed directory and 'extension').
Used e.g. | public ResourceLoader | getParentResourceLoader() Returns the 'parent' ResourceLoader. | public Reader | getReader(String name) Returns a reader for a given resource. | public List<URL> | getResourceList(String name) Returns a List, containing all URL's which may represent the
given resource. | Node | getResourceNode(String name) | public 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.
Parameters: pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions apply Parameters: recursive - If true, then also subdirectories are searched. | protected Set<String> | getResourcePaths(Pattern pattern, boolean recursive, boolean directories) | public static synchronized 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. | public static synchronized ResourceLoader | getWebDirectory(HttpServletRequest request) | public static synchronized ResourceLoader | getWebRoot() | public Writer | getWriter(String name) Returns a writer for a given resource, so that you can overwrite or create it. | public int | hashCode() | public static synchronized void | init(ServletContext sc) Initializes the Resourceloader using a servlet-context (makes resolving relatively to WEB-INF/config possible).
Parameters: sc - The ServletContext used for determining the mmbase configuration directory. | public static void | initLogging() | public static void | main(String[] argv) | protected URL | newURL(String url) Creates a new URL object, which is used to load resources. | public static void | setResourceBuilder(NodeManager b) Sets the MMBase builder which must be used for resource. | URL | shadowed(File f, String name) Determine wether File f is shadowed.
Parameters: name - Check for resource with this name Parameters: file - The file to check for this resource. | public void | storeDocument(String name, Document doc) Creates a resource for a given Document. | public void | storeSource(String name, Source source, DocumentType docType) Creates a resource with given name for given Source. | public String | toInternalForm(String name) Returns an abstract URL for a resource with given name, findResource(name).toString() would give an 'external' form. | public static String | toInternalForm(URL u) | public String | toString() |
CLASSLOADER_ROOT | final protected static String CLASSLOADER_ROOT(Code) | | Used when getting resources with normal class-loader.
|
DEFAULT_CONTEXT | final public static String DEFAULT_CONTEXT(Code) | | |
FILENAME_FIELD | final public static String FILENAME_FIELD(Code) | | |
INDEX | final protected static String INDEX(Code) | | Used when using getResourcePaths for normal class-loaders.
|
LASTMODIFIED_FIELD | final public static String LASTMODIFIED_FIELD(Code) | | |
NODE_URL_CONTEXT | final public static URL NODE_URL_CONTEXT(Code) | | Protocol prefix used by URL objects in this class.
|
PROTOCOL | final protected static String PROTOCOL(Code) | | Protocol prefix used by URL objects in this class.
|
RESOURCENAME_FIELD | final public static String RESOURCENAME_FIELD(Code) | | |
RESOURCE_ROOT | final protected static String RESOURCE_ROOT(Code) | | Used for files, and servlet resources.
|
TYPE_CONFIG | final public static int TYPE_CONFIG(Code) | | |
TYPE_WEB | final public static int TYPE_WEB(Code) | | |
checkShadowedNewerResources | void checkShadowedNewerResources(String name)(Code) | | Logs warning if 'newer' resources are shadowed by older ones.
|
createResourceAsStream | public OutputStream createResourceAsStream(String name) throws IOException(Code) | | If you want to change a resource, or create one, then this method can be used. Specify the
desired resource-name and you get an OutputStream back, to which you must write.
This is a shortcut to findResource(name).openConnection().getOutputStream()
If the given resource already existed, it will be overwritten, or shadowed, if it was not
writeable.
throws: IOException - If the Resource for some reason could not be created. |
findResource | protected URL findResource(String name)(Code) | | If name starts with '/' or 'mm:/' the 'parent' resourceloader is used.
Otherwise the name is resolved relatively. (For the root ResourceLoader that is the same as starting with /)
|
getChildContexts | public Set<String> getChildContexts(Pattern pattern, boolean recursive)(Code) | | Returns a set of context strings which can be used to instantiated new ResourceLoaders (resource loaders for directories)
(see
ResourceLoader.getChildResourceLoader(String) ).
Parameters: pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions apply Parameters: recursive - If true, then also subdirectories are searched. |
getConfigurationRoot | public static synchronized ResourceLoader getConfigurationRoot()(Code) | | Singleton that returns the ResourceLoader for loading mmbase configuration
|
getContext | public URL getContext()(Code) | | Returns the 'context' for the ResourceLoader (an URL).
|
getDirectory | public static String getDirectory(String path)(Code) | | Utility method to return the 'directory' part of a resource-name.
Used e.g. when loading builders in MMBase.
|
getDirectoryName | public static String getDirectoryName(String path)(Code) | | Utility method to return the name of the directory of a resource-name. This does not include
any /-chars any more.
since: MMBase-1.8.2 |
getDocument | public Document getDocument(String name) throws org.xml.sax.SAXException, IOException(Code) | | Returns the givens resource as a Document (parsed XML). This can come in handly, because most
configuration is in XML.
Parameters: name - The name of the resource to be loaded The Document if succesful, null if there is no such resource. throws: SAXException - If the resource does not present parseable XML. throws: IOException - |
getDocument | public Document getDocument(String name, boolean validation, Class> baseClass) throws org.xml.sax.SAXException, IOException(Code) | | Returns the givens resource as a Document (parsed XML). This can come in handly, because most
configuration is in XML.
Parameters: name - The name of the resource to be loaded Parameters: validation - If true , validate the xml. By dtd if one of the first lines starts with <!DOCTYPE, by XSD otherwise Parameters: baseClass - If validation is true , the base class to search for the validating xsd or dtd The Document if succesful, null if there is no such resource. throws: SAXException - If the resource does not present parseable XML. throws: IOException - |
getFiles | public List<File> getFiles(String name)(Code) | | Used by
ResourceWatcher . And by some deprecated code that wants to produce File objects.
A List of all files associated with the resource. |
getInputSource | public InputSource getInputSource(String name) throws IOException(Code) | | Returns the givens resource as an InputSource (XML streams). ResourceLoader is often used for
XML.
The System ID is set, otherwise you could as wel do new InputSource(r.getResourceAsStream());
Parameters: name - The name of the resource to be loaded The InputSource if succesfull, null otherwise. |
getName | public static String getName(String path)(Code) | | Utility method to return the name part of a resource-name (removed directory and 'extension').
Used e.g. when loading builders in MMBase.
|
getReader | public Reader getReader(String name) throws IOException(Code) | | Returns a reader for a given resource. This performs the tricky task of finding the encoding.
Resources are actually InputStreams (byte arrays), but often they are quite text-oriented
(like e.g. XML's or property-files), so this method may be useful.
A resource is supposed to be a property-file if it's name ends in ".properties", it is
supposed to be XML if it's content starts with <?xml.
See Also: ResourceLoader.getResourceAsStream(String) |
getResourceList | public List<URL> getResourceList(String name)(Code) | | Returns a List, containing all URL's which may represent the
given resource. This can be used to show what resource whould be loaded and what resource
whould be masked, or one can also simply somehow 'merge' all these resources.
|
getResourcePaths | public Set<String> getResourcePaths(Pattern pattern, boolean recursive)(Code) | | Returns a set of 'sub resources' (read: 'files in the same directory'), which can succesfully be loaded by the ResourceLoader.
Parameters: pattern - A Regular expression pattern to which the file-name must match, or null if no restrictions apply Parameters: recursive - If true, then also subdirectories are searched. A Set of Strings which can be successfully loaded with the resourceloader. |
getWebRoot | public static synchronized ResourceLoader getWebRoot()(Code) | | Singleton that returns the ResourceLoader for witch the base path is the web root
|
init | public static synchronized void init(ServletContext sc)(Code) | | Initializes the Resourceloader using a servlet-context (makes resolving relatively to WEB-INF/config possible).
Parameters: sc - The ServletContext used for determining the mmbase configuration directory. Or null . |
initLogging | public static void initLogging()(Code) | | |
main | public static void main(String[] argv)(Code) | | For testing purposes only
|
newURL | protected URL newURL(String url) throws MalformedURLException(Code) | | Creates a new URL object, which is used to load resources. First a normal java.net.URL is
instantiated, if that fails, we check for the 'mmbase' protocol. If so, a URL is instantiated
with a URLStreamHandler which can handle that.
If that too fails, it should actually already be a MalformedURLException, but we try
supposing it is some existing file and return a file: URL. If no such file, only then a
MalformedURLException is thrown.
|
setResourceBuilder | public static void setResourceBuilder(NodeManager b)(Code) | | Sets the MMBase builder which must be used for resource.
The builder must have an URL and a HANDLE field.
This method can be called only once.
Parameters: b - An MMObjectBuilder (this may be null if no such builder available) throws: RuntimeException - if builder was set already. |
shadowed | URL shadowed(File f, String name)(Code) | | Determine wether File f is shadowed.
Parameters: name - Check for resource with this name Parameters: file - The file to check for this resource. The URL for the shadowing resource, or null if not shadowed. throws: IllegalArgumentException - if file is not a file associated with the resource with given name. |
toInternalForm | public String toInternalForm(String name)(Code) | | Returns an abstract URL for a resource with given name, findResource(name).toString() would give an 'external' form.
|
Methods inherited from java.lang.ClassLoader | public synchronized void clearAssertionStatus()(Code)(Java Doc) final protected Class> defineClass(byte[] b, int off, int len) throws ClassFormatError(Code)(Java Doc) final protected Class> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError(Code)(Java Doc) final protected Class> defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError(Code)(Java Doc) final protected Class> defineClass(String name, java.nio.ByteBuffer b, ProtectionDomain protectionDomain) throws ClassFormatError(Code)(Java Doc) protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException(Code)(Java Doc) protected Class> findClass(String name) throws ClassNotFoundException(Code)(Java Doc) protected String findLibrary(String libname)(Code)(Java Doc) final protected Class> findLoadedClass(String name)(Code)(Java Doc) protected URL findResource(String name)(Code)(Java Doc) protected Enumeration<URL> findResources(String name) throws IOException(Code)(Java Doc) final protected Class> findSystemClass(String name) throws ClassNotFoundException(Code)(Java Doc) protected Package getPackage(String name)(Code)(Java Doc) protected Package[] getPackages()(Code)(Java Doc) final public ClassLoader getParent()(Code)(Java Doc) public URL getResource(String name)(Code)(Java Doc) public InputStream getResourceAsStream(String name)(Code)(Java Doc) public Enumeration<URL> getResources(String name) throws IOException(Code)(Java Doc) public static ClassLoader getSystemClassLoader()(Code)(Java Doc) public static URL getSystemResource(String name)(Code)(Java Doc) public static InputStream getSystemResourceAsStream(String name)(Code)(Java Doc) public static Enumeration<URL> getSystemResources(String name) throws IOException(Code)(Java Doc) public Class> loadClass(String name) throws ClassNotFoundException(Code)(Java Doc) protected synchronized Class> loadClass(String name, boolean resolve) throws ClassNotFoundException(Code)(Java Doc) final protected void resolveClass(Class> c)(Code)(Java Doc) public synchronized void setClassAssertionStatus(String className, boolean enabled)(Code)(Java Doc) public synchronized void setDefaultAssertionStatus(boolean enabled)(Code)(Java Doc) public synchronized void setPackageAssertionStatus(String packageName, boolean enabled)(Code)(Java Doc) final protected void setSigners(Class> c, Object[] signers)(Code)(Java Doc)
|
|
|