1 2 3 Previous Next 36 Replies Latest reply: Jan 14, 2009 3:06 PM by Kabir Khan Go to original post RSS
  • 15. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

     

    "adrian@jboss.org" wrote:

    Like I said before, if we need to make something more explicit in the Module api
    or some helper class to make your life easier then we can do that.

    Looking at the OSGi part of this, I think the main bit of missing information is to be able link back to the BaseClassLoader (maps to my new pools) from the DelegateLoader (used in the domain to find the loader in the imports). I cannot see any way to determine that?

  • 16. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

    BaseDelegateLoader.getBaseClassLoader() looks like exactly what I need, but it is package protected.

  • 17. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

    By making BaseDelegateLoader.getBaseClassLoader() public, and deploying a local snapshot, I was able to get all the tests I have so far to pass. That is probably not what I should be doing, so I am awaiting some input on how to get hold of the BaseClassLoader a DelegateLoader maps to.

  • 18. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

    From Adrian's comments on
    https://jira.jboss.org/jira/browse/JBCL-78
    (http://www.jboss.com/index.html?module=bb&op=viewtopic&t=148397) the above fix is not what I should be doing.

    Would it be possible to add a method somewhere along the lines of
    Module getModuleForClass(Module requestingModule, String classname)

    From the Module I can figure out the correct classpool to use in AOP.

    Alternatively, would I have to reimplement the matching rules for the modules in AOP?

  • 19. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

    Or maybe rather
    Module[] getModulesForClass(Module requestingModule, String classname)

  • 20. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

     

    "kabir.khan@jboss.com" wrote:

    Alternatively, would I have to reimplement the matching rules for the modules in AOP?


    The whole point is you don't know what the rules are.
    They are vary with the Module/ClassLoader implementation.

    Having a getModuleForClass() would make sense.
    Why would you want it to return multiple modules?

  • 21. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

     

    "adrian@jboss.org" wrote:

    Having a getModuleForClass() would make sense.
    Why would you want it to return multiple modules?


    I might have misunderstood, I just noticed that it is possible to have several DelegateLoaders. If it can return just one, all the better.

    I am not sure how this would be implemented internally in jboss-cl, but the implementation should not try to load up the class (as for example ClassLoadeDomain.findLoaderForClass does) since once we get the CtClass it will not be possible to weave it.

  • 22. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

     

    "kabir.khan@jboss.com" wrote:
    "adrian@jboss.org" wrote:

    Having a getModuleForClass() would make sense.
    Why would you want it to return multiple modules?


    I might have misunderstood, I just noticed that it is possible to have several DelegateLoaders. If it can return just one, all the better.


    There's multiple delegates because you can have multiple imports.


    I am not sure how this would be implemented internally in jboss-cl, but the implementation should not try to load up the class (as for example ClassLoadeDomain.findLoaderForClass does) since once we get the CtClass it will not be possible to weave it.


    Ok. I see. So you want it to do the class search and then tell you
    which module/classloader it belongs to, but not actually load the class.

    I can certainly add that to our classloader, but not every classloader implementation
    is going to have that feature.

    I've already done an implementation like findLoaderForClass which just depends upon
    using class.getClassLoader()

  • 23. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

    I've uploaded a 2.0.2-SNAPSHOT of jboss-cl for you to try.

    This has a Module.getModuleForClass() which at least for our classloader implementation
    doesn't load the class.

    See BaseClassLoader.getClassLoaderForClass() for how it works
    without exposing nasty things. ;-)

    You'll need to put its use in a privileged block since I require the
    new RuntimePermission("getClassLoader") with a security manager installed.

  • 24. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

     

    "adrian@jboss.org" wrote:
    I've uploaded a 2.0.2-SNAPSHOT of jboss-cl for you to try.

    This has a Module.getModuleForClass() which at least for our classloader implementation
    doesn't load the class.


    If somebody wants to integrate their classloader implementation (there are no
    others at the moment since we decied to drop the old UnifiedClassLoader :-)
    properly with AOP then they'll have to override this method to give the correct semantics.

  • 25. Re: Reimplementing ClassPools for AS
    Ales Justin Master

     

    "adrian@jboss.org" wrote:

    See BaseClassLoader.getClassLoaderForClass() for how it works
    without exposing nasty things. ;-)

    You can do this
     public ClassLoader foo(ClassLoader cl) throws Exception
     {
     ClassLoader fcl = null;
     if (cl instanceof BaseClassLoader)
     {
     BaseClassLoader bcl = (BaseClassLoader)cl;
     fcl = bcl.findClassLoader("com.acme.FooBar");
     }
     return fcl;
     }
    

    Intentional?

    I guess it doesn't do any harm ...

  • 26. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

     

    "alesj" wrote:

    I guess it doesn't do any harm ...


    Yes it does potential harm because you can get access to classes and resources
    from other classloaders that you shouldn't be allowed to.

    e.g. in your example you can load unexported classes from fcl which you can't do from cl

    That's why you need the RuntimePermission("getClassLoader") like you do
    for class.getClassLoader().

  • 27. Re: Reimplementing ClassPools for AS
    Ales Justin Master

     

    "adrian@jboss.org" wrote:

    That's why you need the RuntimePermission("getClassLoader") like you do
    for class.getClassLoader().

    But I guess if you have this permission, then you can bend the import/export rules?


  • 28. Re: Reimplementing ClassPools for AS
    Adrian Brock Master

     

    "alesj" wrote:
    "adrian@jboss.org" wrote:

    That's why you need the RuntimePermission("getClassLoader") like you do
    for class.getClassLoader().

    But I guess if you have this permission, then you can bend the import/export rules?


    Of course. If you've got enough rope you can hang yourself. ;-)

    Class<?> exportedClass = myClassLoader.loadClass("clazz.exported.from.elsewhere");
    ClassLoader exportingClassLoader = exportedClass.getClassLoader(); // PERMISSION CHECK HERE
    Class<?> notExported = exportingClassLoader.load("non.exported.class");
    


  • 29. Re: Reimplementing ClassPools for AS
    Kabir Khan Master

     

    "adrian@jboss.org" wrote:
    I've uploaded a 2.0.2-SNAPSHOT of jboss-cl for you to try.

    This has a Module.getModuleForClass() which at least for our classloader implementation
    doesn't load the class.


    Doesn't this load the class? From Module.java

     /**
     * Find the module that loads a class
     *
     * @param className the class name
     * @return the module or null if the class is not loaded by a registered module classloader
     * @throws ClassNotFoundException when the class is not found
     * @throws IllegalStateException when the module is not associated with a classloader
     */
     public Module getModuleForClass(String className) throws ClassNotFoundException
     {
     SecurityManager sm = System.getSecurityManager();
     if (sm != null)
     sm.checkPermission(new RuntimePermission("getClassLoader"));
    
     ClassLoader cl = getClassLoaderForClass(className);
    
     // Determine the module (if any) for the classloader
     if (cl != null)
     return modulesByClassLoader.get(cl);
     // Unknown
     return null;
     }
    
     /**
     * Get the classloader for a class name
     *
     * @param className the class name
     * @return the class
     * @throws ClassNotFoundException when the class is not found
     * @throws IllegalStateException when the module is not associated with a classloader
     */
     protected ClassLoader getClassLoaderForClass(String className) throws ClassNotFoundException
     {
     // Determine the classloader for this class
     final Class<?> clazz = loadClass(className);
     SecurityManager sm = System.getSecurityManager();
     if (sm != null)
     {
     return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
     {
     public ClassLoader run()
     {
     return clazz.getClassLoader();
     }
     });
     }
     return clazz.getClassLoader();
     }
    
     /**
     * Load a class for this module
     *
     * @param className the class name
     * @return the class
     * @throws ClassNotFoundException when the class is not found
     * @throws IllegalStateException when the module is not associated with a classloader
     */
     public Class<?> loadClass(String className) throws ClassNotFoundException
     {
     ClassLoader classLoader = getClassLoader();
     if (classLoader == null)
     throw new IllegalStateException("No classloader for this module " + this);
     return classLoader.loadClass(className);
     }
    

    Or does the uploaded snapshot have a different impl?