开发者

Classloader specific properties

开发者 https://www.devze.com 2023-02-23 22:41 出处:网络
We developed an application container that creates a new classloader for each independent application running in the container.When a speci开发者_JS百科fic application is invoked, the Thread\'s contex

We developed an application container that creates a new classloader for each independent application running in the container. When a speci开发者_JS百科fic 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消