Reputation: 4851
We developed an application container that creates a new classloader for each independent application running in the container. When a specific application is invoked, the Thread's context classloader is set appropriately with the application's classloader.
Avoiding the use of ThreadLocal, is it possible to store properties within a classloader, such that you would be able to retrieve, in this case, application-specific properties directly from the classloader.
For example, I want to be able to somehow save and then later retrieve properties when accessing the context classloader:
Thread.currentThread().getContextClassLoader()
Is this possible? Or is ThreadLocal the only viable option?
Upvotes: 0
Views: 247
Reputation: 57757
Rather than casting the classloader, you can have it load a custom properties class, e.g.
public class AppClassloaderProperties
{
static Properties appProperties = loadAppProperties();
static private Properties loadAppProperties() {
// fetch app properties - does not need to be thread-safe, since each invocation
// of this method will be on a different .class instance
}
static public final Properties getApplicationProperties() {
// this method should be thread-safe, returning the immutable properties is simplest
return new Properties(appProperteis);
}
}
Since this class is loaded as part of the application's classloader, a new class is provided for each application. The AppClassloaderProperties
class for each application will be distinct. Each application can then get its classloader properties by calling
Properties props = AppClassloaderProperties.getApplicationProperties();
// use the properties
No need for thread locals or casting the current classloader.
Upvotes: 1
Reputation: 16152
How about subclassing the context classloader, extending it with the property support you need, then just casting the Thread.currentThread().getContextClassLoader()?
Upvotes: 0