What is the best way to load a class or resource from a plugin?
What is the best way to load a resource from the classpath?
Because of the different ways that application servers deal with class-loading, just calling
this.getClass().getResourceAsStream() might not work the same everywhere Confluence is deployed. To help, we have a utility method that checks the various classloaders in a predictable order:
Because plugins may be dynamically loaded, each plugin may have its own classloader, separate from the main Confluence application. This makes loading resources like properties files from inside a plugin JAR a little tricky.
If the class from which you are loading the resource is in the same jar as the resource file itself (i.e. it's all part of the same plugin), you can use
ClassLoaderUtils as above, and everything will work fine.
However, if you are trying to load the file from a different plugin, or from the main application code, you'll need an instance of the
pluginManager from spring:
An example of modules and resources
When writing your own ModuleDescriptor you might want developers that depend on your ModuleDescriptor write something that looks like this in their atlassian-plugin.xml:
They would then have a ModuleDescriptor with an init() method that looks something like this:
There are two important function invocations in this example:
This method makes it easy to extract resource declarations from Atlassian Plugins 2 Module Descriptor XML definitions. You should use this for your own resource declarations.
This is the preferred method of extracting a resource from another plugin.
Use this to load resources from other plugins that have been defined inside atlassian-plugin.xml files.