3 Replies Latest reply on Feb 9, 2012 10:25 AM by method_ben_qc

    ModuleClassLoader : How can I retrieve the classpath from the loader

    method_ben_qc

      In JBoss 4.2.3, I use this code for retrieving the classpath from the class loader:

       

      List<URL> classpathUrls = null;

       

      try

      {

                  ClassLoader classLoader = getClass().getClassLoader();

       

                  if (classLoader instanceof UnifiedClassLoader)

                  {

                      classpathUrls = new ArrayList<URL>(Arrays.asList(((org.jboss.mx.loading.UnifiedClassLoader)                 getClass().getClassLoader()).getClasspath()));

                  }

      }

      catch (ClassCastException e)

      {

           throw new InternalException(e);

      }

       

      But the class loader has been replaced by org.jboss.modules.ModuleClassLoader in JBoss AS 7. This class loader doesn't have the method getClasspath() because it doesn't extend RepositoryClassLoader.

       

      How can I do the same thing in JBoss AS 7 ?

       

      Thx

        • 1. Re: ModuleClassLoader : How can I retrieve the classpaht from the loader
          method_ben_qc

          Here's my situation:

           

          I deploy my application.ear in JBoss AS 7. In my apllication.ear, I have a StartupBean which it loads at startup and it scans my ear for retrieving all jars, files, etc of my ear (I do some custom validations on my classes, xml files, etc). As you can see in the previous post, this operation was possible in JBoss AS 4.2.3 because the class UnifiedClassLoader had the method getClasspath() then I got a list of a classpath URLs.

           

          I would like to find a way to retrieve all files of my application.ear. Do you have any idea which classes can I use ? Is there a way to get the jar files of my application.ear once it is deployed ?

          • 2. Re: ModuleClassLoader : How can I retrieve the classpaht from the loader
            alesj

            I would like to find a way to retrieve all files of my application.ear. Do you have any idea which classes can I use ? Is there a way to get the jar files of my application.ear once it is deployed ?

            Your best bet is reflection, and probably not even this will make it easy to get the needed info.

            This is all by design, so impl details are hidden from the user.

             

            But you should be easily able to move this logic into deployment processor,

            which has all of this info nicely exposed; but you'll  need to write your own AS7 Extension.

            • 3. Re: ModuleClassLoader : How can I retrieve the classpath from the loader
              method_ben_qc

              I find a solution to my problem by using the Virtual File System of JBoss. I have utility bean which is launched at the startup (@Singleton and @Startup) for scanning my application and make some validation on my classes. Here's my workaround that I use to find all my jars of my EAR (excluding lib folder):

               

                      if(getClass().getClassLoader() instanceof ModuleClassLoader)

                      {

                          try

                          {

                              Enumeration<URL> resourceURLs = classLoader.getResources("my/package");

                             

                              while(resourceURLs.hasMoreElements())

                              {

                                  URL resourceURL = resourceURLs.nextElement();

                                  String strResourceURL = resourceURL.toString();

                              

                                  if(StringUtils.contains(strResourceURL, ".jar") && !StringUtils.contains(strResourceURL, "/lib/")

                                  {

                                          URLConnection conn = resourceURL.openConnection();

                                          VirtualFile vf = (VirtualFile)conn.getContent();

                                          Set<Object> initializedElements = new HashSet<Object>();

                 

                                          String jarVFSPath = VFSUtils.getPhysicalURL(vf.getParent().getParent()).getFile();

                                         

                                          int idxJarExt = StringUtils.lastIndexOf(jarVFSPath, ".jar");

                                          int idxSlashAfterJar = StringUtils.indexOf(jarVFSPath, "/", idxJarExt);

                                          int idxSlashBeforeJar = StringUtils.lastIndexOf(jarVFSPath.substring(0,idxJarExt), "/");

                 

                                          String jarFileName2 = jarVFSPath.substring(idxSlashBeforeJar + 1, idxJarExt + 4);

                                          String jarFolder = jarVFSPath.substring(0, idxSlashAfterJar);

                                          String jarFullPath = jarFolder + "/" + jarFileName2;

                                         

                                          try

                                          {

                                              JarFile jarFile = getJarFile(jarFullPath);

                                              scanJar(jarFile, initializedElements, jarFileName2);

                                          }

                                          catch (FileNotFoundException e)

                                          {

                                              throw e;

                                          }

                                  }

                              }

                          }

                          catch (IOException e)

                          {

                              throw e;

                          }

                      }

               

              It may be cleaner but it's just the first version !