Table of Contents
getClass().getClassLoader().getResource("java/lang/String.class").toString());Such an URL typically looks like this then:
jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar!/java/lang/String.class
Actually ClassLoaders are not restricted to loading classes, but they can load any type of 'resource', as long as they are presented in the 'CLASS PATH' of the JVM. Which in practice means that these resources must be either present in a 'jar' in WEB-INF/lib or as a file in WEB-INF/classes. You can e.g. place configuration like so. The result of
getClass().getClassLoader().getResource("oscache.properties").toString());
file:/Users/mmbase/mmbase18/WEB-INF/classes/oscache.properties
Resources that cannot be loaded by the ClassLoader of a web application but are part of the web-application, can be loaded with the 'getResource(String)' method on the ServletContext object. E.g. lets get the same resource, but now on this way. Like this, with a jsp scriptlet:
pageContext.getServletContext().getResource("/WEB-INF/classes/oscache.properties")
jndi:/localhost/mm18/WEB-INF/classes/oscache.properties.
So, there are several methods to load resources, and every method has its own advantages and disadvantages. A file can easily be changed and 'watched' for changes, a classloader has a provision for 'multiple' resources with the same name (every jar can contain a copy of the same file), and the servletContext method can access resources, even if the 'war' is still packed .
The idea of the MMBase Resource Loader is now, that MMBase decides which way of loading a certain resource is the best. A good way is a way that works, so methods that don't work are ignored. If for example the MMBase resource 'builders/core/object.xml' is not available as a File (which we like most, because they can easily be changed and updated), then the resource can be accessed as such. If it is not available as a file, then it tries to do it with a ClassLoader or with a ServletContext. For more details, see also the javadoc of org.mmbase.util.ResourceLoader
The MMBase Resource Loader itself extends from ClassLoader. So, it works like a Class Loader, and it basicly serves to produces URL's or InputStreams for you. We add a few convience methods like getting a Document immediately (because we have to deal with XML so much...)
ResourceLoader configLoader = ResourceLoader.getConfigurationRoot();There are also ResourceLoaders for resolving relative to the web-application root, or the file system root. Furthermore new ResourceLoaders can be instantiated which are relative to one of these staticly defined root ResourceLoaders:
ResourceLoader builderLoader = ResourceLoader.getConfigurationRoot().getChildResourceLoader("builders");
This is part of the MMBase documentation.
For questions and remarks about this documentation mail to: documentation@mmbase.org