<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: classloader code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 22:43:34 GMT</pubDate>
    <description>DZone Snippets: classloader code</description>
    <item>
      <title>Which class file is loaded by the classloader ?</title>
      <link>http://snippets.dzone.com/posts/show/3719</link>
      <description>When using lots of third-party libraries, one problem might be that 2 of them package different versions of the same class, producing errors when method version conflicts happen.&lt;br /&gt;&lt;br /&gt;Here is a simple way to know the exact location used by the classloader to get your class :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;URL myClassURL = MyMysteryClass.class.getProtectionDomain().getCodeSource().getLocation();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Mar 2007 11:38:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3719</guid>
      <author>nivek (Kevin Gaudin)</author>
    </item>
    <item>
      <title>Functional JarClassLoader - warning, really awful</title>
      <link>http://snippets.dzone.com/posts/show/3527</link>
      <description>Some really awful functional style Java...&lt;br /&gt;&lt;br /&gt;Note that this depends on a bunch of other classes in the playground namespace. If you actually want to use it ask me and I'll send you the source - I didn't really feel like snippetting all the needed code. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package playground.library.classloader;&lt;br /&gt;&lt;br /&gt;import java.io.ByteArrayOutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.HashSet;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.List;&lt;br /&gt;import java.util.Set;&lt;br /&gt;import java.util.jar.JarEntry;&lt;br /&gt;import java.util.jar.JarFile;&lt;br /&gt;import playground.library.functional.FunctionalUtils;&lt;br /&gt;import playground.library.functional.Pair;&lt;br /&gt;import playground.library.functional.iterator.FilterIterator;&lt;br /&gt;import playground.library.functional.iterator.IteratorTransformer;&lt;br /&gt;import playground.library.functional.Predicate;&lt;br /&gt;import playground.library.functional.Transformer;&lt;br /&gt;import playground.library.functional.iterator.DelegatingIterator;&lt;br /&gt;import playground.library.functional.iterator.LinkingIterator;&lt;br /&gt;import playground.library.jar.JarEntryIterator;&lt;br /&gt;import playground.library.utils.IteratorUtils;&lt;br /&gt;import playground.library.utils.IOUtils;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * ClassLoader for loading from Jar files.&lt;br /&gt; *&lt;br /&gt; * @author david&lt;br /&gt; */&lt;br /&gt;public class JarClassLoader extends ClassLoader&lt;br /&gt;{&lt;br /&gt;    public JarClassLoader(ClassLoader parent) { super(parent); }    &lt;br /&gt;    public JarClassLoader()                   { super(); }&lt;br /&gt;    &lt;br /&gt;    private final List&lt;JarFile&gt;     files = new ArrayList&lt;JarFile&gt;();&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Provides an iterator over all pairs of (file, entry) of JarFiles associated&lt;br /&gt;     * with this class loader and entries of those jar files.&lt;br /&gt;     */&lt;br /&gt;    private Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt; getResources(){&lt;br /&gt;        return new LinkingIterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt;(&lt;br /&gt;            new IteratorTransformer&lt;JarFile, Iterator&lt;Pair&lt;JarFile,JarEntry&gt;&gt;&gt;(&lt;br /&gt;                new Transformer&lt;JarFile, Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt;&gt;(){&lt;br /&gt;                public Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt; transform(JarFile file){&lt;br /&gt;                    return FunctionalUtils.merge(file, new JarEntryIterator(file));}})&lt;br /&gt;           .transform(JarClassLoader.this.files.iterator()));}&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Searches through the available jar files to try and find a class with the given name.&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    protected Class&lt;?&gt; findClass(String name) throws ClassNotFoundException{&lt;br /&gt;        final Iterator&lt;Pair&lt;JarFile,JarEntry&gt;&gt; resources = this.getResources();&lt;br /&gt;        while (resources.hasNext()){&lt;br /&gt;            final Pair&lt;JarFile, JarEntry&gt; resource = resources.next();&lt;br /&gt;                if(resource.second.getName().equals(name)){&lt;br /&gt;                    try { return this.load(resource.first.getInputStream(resource.second));}&lt;br /&gt;                    catch (IOException e) { e.printStackTrace(); }}}&lt;br /&gt;        throw new ClassNotFoundException();}&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Loads the contents of the InputStream as a class. This doesn't validate&lt;br /&gt;     * the contents, so will fail messily if you pass it something that isn't a&lt;br /&gt;     * valid class file.&lt;br /&gt;     */&lt;br /&gt;    private Class&lt;?&gt; load(final InputStream input){&lt;br /&gt;        try {&lt;br /&gt;            final ByteArrayOutputStream output = new ByteArrayOutputStream();&lt;br /&gt;            final byte[] bytes = IOUtils.copy(input, new ByteArrayOutputStream()).toByteArray();&lt;br /&gt;            return this.defineClass(null, bytes, 0, bytes.length); }&lt;br /&gt;        catch (IOException e){ throw new RuntimeException(e); }&lt;br /&gt;        finally{&lt;br /&gt;            try{ input.close(); }&lt;br /&gt;            catch (IOException e){ throw new RuntimeException(e);}}}&lt;br /&gt;        &lt;br /&gt;    /**&lt;br /&gt;     * Provides an iterator which lazily loads the contents of the jar file as&lt;br /&gt;     * classes. Will also register the jar file with the class loader's file list.&lt;br /&gt;     * When the iterator is exhausted the jar file will be removed from the file list.&lt;br /&gt;     */&lt;br /&gt;    public Iterator&lt;Class&lt;?&gt;&gt; load(final JarFile file) throws IOException{&lt;br /&gt;        this.files.add(file);&lt;br /&gt;                &lt;br /&gt;        return  &lt;br /&gt;            new DelegatingIterator&lt;Class&lt;?&gt;&gt;(&lt;br /&gt;                new IteratorTransformer&lt;JarEntry, Class&lt;?&gt;&gt;(&lt;br /&gt;                    new Transformer&lt;JarEntry, Class&lt;?&gt;&gt;(){&lt;br /&gt;                        public Class transform(JarEntry stream){&lt;br /&gt;                            InputStream fileStream = null;&lt;br /&gt;&lt;br /&gt;                            try{return JarClassLoader.this.load(fileStream = file.getInputStream(stream)); }&lt;br /&gt;                            catch(IOException e){throw new RuntimeException(e);}&lt;br /&gt;                            finally{IOUtils.close(fileStream); }}})&lt;br /&gt;                .transform(&lt;br /&gt;                    new FilterIterator( &lt;br /&gt;                       new Predicate&lt;JarEntry&gt;(){ &lt;br /&gt;                           public boolean satisfies(JarEntry entry){&lt;br /&gt;                               return entry.getName().endsWith(".class");}},&lt;br /&gt;                       new JarEntryIterator(file)))){&lt;br /&gt;                @Override&lt;br /&gt;                public Class&lt;?&gt; next(){&lt;br /&gt;                    final Class&lt;?&gt; clazz = super.next();&lt;br /&gt;                    if (!this.hasNext()) JarClassLoader.this.files.remove(file);                    &lt;br /&gt;                    return clazz;}&lt;br /&gt;                &lt;br /&gt;                @Override&lt;br /&gt;                public boolean hasNext(){&lt;br /&gt;                    final boolean hasNext = super.hasNext();&lt;br /&gt;                    if (!hasNext) JarClassLoader.this.files.remove(file);&lt;br /&gt;                    return hasNext;}};}&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Loads all class files from the jar and returns a set of them.&lt;br /&gt;     */&lt;br /&gt;    public Set&lt;Class&lt;?&gt;&gt; loadAll(final JarFile file) throws IOException&lt;br /&gt;    {&lt;br /&gt;        return IteratorUtils.fill(new HashSet&lt;Class&lt;?&gt;&gt;(), this.load(file));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Feb 2007 09:55:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3527</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
  </channel>
</rss>
