Openmrs dynamic class and resource loading

Hello all;

I have been going through the platform and wanting to understand how classes and resources are dynamically loaded into the jvm at runtime basing on their demand .I see openmrs uses the Java EE delegation model (correct me if am wrong on this) were it looks into the local loader first for the needed classes and resources before delegating to the parent class loader. The main class loader used here is the OpenmrsClassLoader as the ContextClassLoader in each thread of the running instance of openmrs , the ModuleClassLoader being used for loading classes and resources in modules running on openmrs.

I have a few questions that i need some enlightenment on.

a) The OpenmrsClassLoader is always aware of all module class loaders in openmrs through the loadClass() method ,under what circumstances does it attempt to load classes form them since the URLClassLoader is parent to the ModuleClassLoader and i would expected the delegation to be passed to the parent if the child class loader can’t find the resource/class in its classpath in the Java EE delegation model ?. In otherword when does the openmrs class loader chip in to load classes in modules which i think are loaded by the module class loader

b) During refreshing of the spring context does the refresh go through the modules first ?, if so then does it only refresh and load classes in the modules api and then wait for the refreshing of the openmrs applicationContext to load the service classes?

Help me understand the magic happening behind the scenes here .

@dkayiwa @ibacher @mksd @wyclif

2 Likes

The Openmrs classloader will first delegate to module classloaders before it falls back to its parent web app classloader(or system classloader outside of the web environment) which is actually the opposite of the recommended delegation model for classloaders, but just like in a servlet container this is intentional to allow a module to provide a different version of a library to override one provided by the container or core.

There is no such thing as refreshing the application context to go through modules, after the module classes are loaded, the application context gets refreshed for the entire application at once which should include module spring beans.

3 Likes

thanks @wyclif for explaining the relationship between the openmrs classloader and the module class loader and how they work around,it clear now … :+1: