JCL icon indicating copy to clipboard operation
JCL copied to clipboard

Some suggestions

Open zenzei opened this issue 13 years ago • 1 comments

Hi kamranzafar!

First, JCL is really great! I made some small modifications to JCL for my Webstart project. I don't know if they could be useful to others so I post them here

The first one is some kind of destroy method for JarClassLoader. It's useful for me because I have some kind of multigame and use this method to unload claseses and resources from JarClassLoader when not in use. (after seeing the unloadClass of JarClassLoader, maybe the unloading of resources could be done with the unloadClass(key) method too).

    public void destroy() {
      for (String key : getLoadedClasses().keySet())
        unloadClass(key);
      for (String key : getLoadedResources().keySet())
        classpathResources.unload(key);
    }

The second one is more related to the webstart and the ProtectionDomain/CodeSource. For that working I had to update public Class loadClass(String className, boolean resolveIt) of LocalLoader, to include in the defineClass method the protectionDomain (Maybe this is related to #1 issue Set ProtectionDomain/CodeSource??.

result = defineClass( className, classBytes, 0, classBytes.length, getClass().getProtectionDomain());

I think it could also be helpful a constructor for JarClassLoader that receives a parent class loader, for example to do something like this.

jcl = new JarClassLoader(new SecureClassLoader() {
      @Override
      protected PermissionCollection getPermissions(CodeSource codesource) {
        PermissionCollection pcol = super.getPermissions(codesource);
        pcol.add(new AllPermission());
        return (pcol);
      }
  });

The last one, I don't implement it yet, but is a public Enumeration<URL> getResources(String name) throws IOException method for each of the ProxyClassLoader implementations. I think the implementations could be something like:

  • SystemLoader
public Enumeration<URL> getResources(String name) throws IOException 
{
    return getSystemResources(name);
}
  • ParentLoader
public Enumeration<URL> getResources(String name) throws IOException 
{
     return getParent().getResources(name);
}
  • CurrentLoader
public Enumeration<URL> getResources(String name) throws IOException 
{
    return getClass().getClassLoader().getResources(name);
}
  • ThreadContextLoader
public Enumeration<URL> getResources(String name) throws IOException 
{
    return Thread.currentThread().getContextClassLoader().getResources(name);
}

Tell me what you think about them as I can learn from your knowledge in classloading!!!

zenzei avatar Sep 28 '12 02:09 zenzei

Thankyou for your contribution, I will see if I can include it in the next release.

kamranzafar avatar Oct 01 '12 21:10 kamranzafar