-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When building with multiple avro versions, the default URLClassLoader may load a wrong resource file. Use a custom ChildFirstClassLoader to avoid this issue. Also set nop slf4j logger for avro-compiler classpath.
- Loading branch information
1 parent
46f5124
commit bba598d
Showing
5 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
plugin/src/main/java/com/github/sbt/avro/ChildFirstClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.github.sbt.avro; | ||
|
||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
/** | ||
* An almost trivial no-fuss implementation of a class loader | ||
* following the child-first delegation model. | ||
* | ||
* @author <a href="http://www.qos.ch/log4j/">Ceki Gulcu</a> | ||
*/ | ||
public class ChildFirstClassLoader extends URLClassLoader { | ||
|
||
public ChildFirstClassLoader(URL[] urls) { | ||
super(urls); | ||
} | ||
|
||
public ChildFirstClassLoader(URL[] urls, ClassLoader parent) { | ||
super(urls, parent); | ||
} | ||
|
||
public void addURL(URL url) { | ||
super.addURL(url); | ||
} | ||
|
||
public Class loadClass(String name) throws ClassNotFoundException { | ||
return loadClass(name, false); | ||
} | ||
|
||
/** | ||
* We override the parent-first behavior established by | ||
* java.lang.Classloader. | ||
* <p> | ||
* The implementation is surprisingly straightforward. | ||
*/ | ||
protected Class loadClass(String name, boolean resolve) | ||
throws ClassNotFoundException { | ||
|
||
//System.out.println("ChildFirstClassLoader("+name+", "+resolve+")"); | ||
|
||
// First, check if the class has already been loaded | ||
Class c = findLoadedClass(name); | ||
|
||
// if not loaded, search the local (child) resources | ||
if (c == null) { | ||
try { | ||
c = findClass(name); | ||
} catch(ClassNotFoundException cnfe) { | ||
// ignore | ||
} catch(SecurityException se) { | ||
// ignore | ||
} | ||
} | ||
|
||
// if we could not find it, delegate to parent | ||
// Note that we don't attempt to catch any ClassNotFoundException | ||
if (c == null) { | ||
if (getParent() != null) { | ||
c = getParent().loadClass(name); | ||
} else { | ||
c = getSystemClassLoader().loadClass(name); | ||
} | ||
} | ||
|
||
if (resolve) { | ||
resolveClass(c); | ||
} | ||
|
||
return c; | ||
} | ||
|
||
/** | ||
* Override the parent-first resource loading model established by | ||
* java.lang.Classloader with child-first behavior. | ||
*/ | ||
public URL getResource(String name) { | ||
URL url = findResource(name); | ||
|
||
// if local search failed, delegate to parent | ||
if(url == null) { | ||
url = getParent().getResource(name); | ||
} | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters