Este conteúdo não está disponível no idioma selecionado.
16.18. Classloaders
ModeShape is designed around extensions: sequencers, connectors, MIME type detectors, and class loader factories. The core part of ModeShape is relatively small and has few dependencies, while many of the "interesting" components are extensions that plug into and are used by different parts of the core or by layers above (such as the JCR implementation). The core does not really care what the extensions do or what external libraries they require, as long as the extension fulfills its end of the extension contract.
This means that you only need the core modules of ModeShape on the application classpath, while the extensions do not have to be on the application classpath. And because the core modules of ModeShape have few dependencies, the risk of ModeShape libraries conflicting with the application's are lower. Extensions, on the other hand, will likely have a lot of unique dependencies. By separating the core of ModeShape from the class loaders used to load the extensions, your application is isolated from the extensions and their dependencies.
Note
Of course, you can put all the JARs on the application classpath, too. This is what the examples in the ModeShape Getting Started Guide document do.
But in this case, how does ModeShape load all the extension classes? You may have noticed earlier that
ExecutionContext implements the ClassLoaderFactory interface with a single method:
public interface ClassLoaderFactory {
/**
* Get a class loader given the supplied classpath. The meaning of the classpath
* is implementation-dependent.
* @param classpath the classpath to use
* @return the class loader; may not be null
*/
ClassLoader getClassLoader( String... classpath );
}
This means that any component that has a reference to an
ExecutionContext has the ability to create a class loader with a supplied class path. As we'll see later, the connectors and sequencers are all defined with a class and optional class path. This is where that class path comes in.
The actual meaning of the class path, however, is a function of the implementation. ModeShape uses a StandardClassLoaderFactory that just loads the classes using the Thread's current context class loader (or, if there is none, delegates to the class loader that loaded the StandardClassLoaderFactory class). Of course, it is possible to implement other ClassLoaderFactory with other implementations. Then, just create a subcontext with your implementation:
ClassLoaderFactory myClassLoaderFactory = ...
ExecutionContext contextWithMyClassLoaderFactories = context.with(myClassLoaderFactory);