0

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?

Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
onejigtwojig
  • 4,771
  • 9
  • 32
  • 35

2 Answers2

1

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.

mdma
  • 56,943
  • 12
  • 94
  • 128
0

How about subclassing the context classloader, extending it with the property support you need, then just casting the Thread.currentThread().getContextClassLoader()?

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40