Uploaded image for project: 'James Server'
  1. James Server
  2. JAMES-418

Loader uses wrong method to obtain class loader/doesn't set context class loader

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.2.0
    • 2.3.0
    • James Core
    • None

    Description

      I had difficulty loading resources from my classes directory. In reviewing the Loader source code, I see two problems:
      1) It uses this.class.getClassLoader(), rather than the preferred/standard Thread.currentThread.getContextClassLoader(). This is not a problem right now, as the Apache James developers have control over the entire application, but could become a difficult bug to track down in the future. In addition, as soon as you start adding multiple class loaders, chaining class loaders with parents becomes impossible (see next point) because it sets the same class loader as multiple parents. In the source code, all instances of this.getClass().getClassLoader() (or whatever.getClass().getClassLoader()) should be replaced by Thread.currentThread().getContextClassLoader()
      2) The greater problem is that it does not call Thread.currentThread().setContextClassLoader(classLoader). This means that any code that uses the standard method Thread.currentThread().getContextClassLoader() (as my code does) will not get the correct class loader, and thus will not be able to load the appropriate resources. In fact, I was getting the primary class loader, which only loads the Phoenix Jar. I had to add into my code the following (the class loader's parent is already set):
      Thread.currentThread().setContextClassLoader(MyClass.class.getClassLoader());
      By the nature of Java class loaders, it is expected that the thread's Context ClassLoader is always kept current, and that any new class loaders are added to the chain. I think that this change should be made in the Apache James source code.

      Attachments

        1. cornerstone-datasources-impl-2.1.jar
          9 kB
          Stefano Bagnara
        2. phoenix-trunk.zip
          2.13 MB
          Stefano Bagnara

        Activity

          People

            bago Stefano Bagnara
            ben.lindahl Ben Lindahl
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: