Changeset 3760


Ignore:
Timestamp:
Sep 20, 2007, 1:35:57 PM (15 years ago)
Author:
Martin Svensson
Message:

Fixes #364, Uses the existing JarClassLoader?, when reading/loading jar-files instead of an internal class

Location:
trunk/src/core/net/sf/basedb/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/JarClassLoader.java

    r3679 r3760  
    121121    }
    122122    return cl;
     123  }
     124 
     125 
     126  /**
     127    Get a new class loader for the specified jar file. 
     128      @param jarPath The path to the jar file
     129      @return A class loader object
     130      @throws IOException If the jar file can't be loaded
     131  */
     132  public static final ClassLoader newInstance(String jarPath)
     133    throws IOException
     134  {
     135    return new JarClassLoader(jarPath);
    123136  }
    124137 
  • trunk/src/core/net/sf/basedb/util/PluginInfo.java

    r3733 r3760  
    139139      Element rootElement = XMLUtil.getValidatedXML(is , installDtdURL, null).getRootElement();
    140140      String jarname = rootElement.getAttributeValue("jarname");
    141       JarReader jarReader = JarReader.getNewInstance(jar);
     141      ClassLoader jarLoader = JarClassLoader.newInstance(jarFile.getPath());
    142142      if (!jarname.equalsIgnoreCase(jarFile.getName()))
    143143      {
     
    156156        boolean hasConfigs = (configElement != null) && configElement.equals(hasConfigsValue);
    157157               
    158         Class c = jarReader.loadClass(classname);       
     158        Class c = jarLoader.loadClass(classname);       
    159159        if (versionCompatible(baseVersion) && Plugin.class.isAssignableFrom(c))
    160160        {     
     
    272272    return majorCompatible && minorCompatible;
    273273  }
    274    
    275   /**
    276     A private class to get information about plugin classes
    277     inside a jar file.
    278       @author Martin
    279       @version 2.5
    280    */
    281   private static class JarReader
    282     extends ClassLoader
    283   {
    284     /**
    285       The jar file for this reader.
    286      */
    287     private JarFile jar;
    288      
    289     /**
    290         @param jar The jar file that should be read
    291      */
    292     private JarReader(JarFile jar)
    293     {
    294       super(Thread.currentThread().getContextClassLoader());
    295       this.jar = jar;     
    296     }
    297    
    298     private static JarReader getNewInstance (JarFile jarFile)
    299     {
    300       return new JarReader(jarFile);
    301     }
    302    
    303     /*
    304       (non-Javadoc)
    305         @see java.lang.ClassLoader#findClass(java.lang.String)
    306     */
    307     protected Class<?> findClass(String name)
    308       throws ClassNotFoundException
    309     {
    310       byte[] b;
    311       try
    312       {
    313         InputStream in = null;
    314         JarEntry jarEntry = jar.getJarEntry(classNameToPath(name));
    315         if (jarEntry == null)
    316         {
    317           throw new ClassNotFoundException(name+" in file "+jar.getName());
    318         }
    319         in = jar.getInputStream(jarEntry);
    320         b = new byte[(int)jarEntry.getSize()];
    321         in.read(b, 0, b.length);       
    322       }     
    323       catch(IOException ex)
    324       {
    325         throw new ClassNotFoundException(name, ex);
    326       }     
    327       return defineClass(name, b, 0, b.length);
    328     }
    329    
    330     /**
    331       Convert a class name to a file path. Ie. replace dots with slahses and add .class
    332       to the end: net.sf.basedb.util.JarClassLoader -> net/sf/basedb/util/JarClassLoader.class
    333     */
    334     private String classNameToPath(String className)
    335     {
    336       return className.replaceAll("\\.", "/")+".class";
    337     }
    338   }
    339274}
Note: See TracChangeset for help on using the changeset viewer.