2 Replies Latest reply: Mar 31, 2006 3:31 AM by 843844 RSS

    ClassLoader problem

    843844
      Hello,

      I have a package containing a Class GenericService which is an abstract Class.
      This Class contains a generic method that uses the introspection to instantiate some business objects.

      I have a webapp containing a specific Service extending the GenericService.

      My package is deployed on the lib directory of my JBoss, so in the parent ClassLoader.
      My webapp is deployed in my deploy directory on my server, so a classis webapp deployment.

      When, in my webapp, I instantiate my specific service and that I try to invoke my generic method, I have a ClassLoader exception as the generic method is trying to instantiate business objects that are stored in my webapp.

      Isn't it possible to access the ClassLoader of my webapp as it is the specific service that is instantiated ?

      Thanks in advance for all your help...
      ____________________ _ _ _
      bgOnline
        • 1. Re: ClassLoader problem
          843844
          I don't know JBoss, but the problem seems to be that you are not using
          Thread.currentThread().getContextClassLoader()
          to load class in your GenericService class.

          If this is what you are doing...
          class GenericService {
           void genericMethod(String className) {
                Class.forName(className);
           }
          }
          then what happens is JVM will try to load className using the class-loader that defined GenericService, in this case what you called as the parent ClassLoader. That class loader only delegates to its parents to load className.

          Try doing the following:
          class GenericService {
           void genericMethod(String className) {
             ClassLoader cl = Thread.currentThread().getContextClassLoader();
              clazz = cl.loadClass(name);
             // now use clazz to call method...
           }
          } 
          If that worked, then use the following code which is robust as it does proper security check. This is described in section #8.2.5 of Java EE platform spec.
          class GenericService {
           void genericMethod(String className) {
             ClassLoader cl = getContextClassLoader();
             if (cl != null)
                 clazz = cl.loadClass(name);
              else
                 clazz = Class.forName(name); 
             // now use clazz to call method...
           }
           
           private ClassLoader getContextClassLoader() {
              return AccessController.doPrivileged(
                new PrivilegedAction<ClassLoader>() {
                    public ClassLoader run() {
                       ClassLoader cl = null;
                       try {
                             cl = Thread.currentThread().getContextClassLoader();
                       } catch (SecurityException ex) { }
                       return cl;
                   }
                });
            }
          }
          Let us know, if this worked.

          Thanks,
          Sahoo
          • 2. Re: ClassLoader problem
            843844
            Hello,

            Thanks a lot for your response, it works perfect !!

            Thanks yet...
            _____________________ _ _ _
            bgOnline