Created
August 16, 2012 01:11
-
-
Save xumingming/3365260 to your computer and use it in GitHub Desktop.
TopologyClassLoader
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
public class TopologyClassLoader extends URLClassLoader { | |
private String name; | |
public void setName(String name) { | |
this.name = name; | |
} | |
public static TopologyClassLoader create(Collection<String> urls) { | |
URL[] urlArr = string2Url(urls); | |
return new TopologyClassLoader(urlArr); | |
} | |
public static TopologyClassLoader create(String urls) { | |
URL[] urlArr = string2Url(urls.split(File.separator)); | |
return new TopologyClassLoader(urlArr); | |
} | |
public TopologyClassLoader(URL[] urls){ | |
super(urls); | |
} | |
public TopologyClassLoader(URL[] urls, ClassLoader parent){ | |
super(urls, parent); | |
} | |
public TopologyClassLoader(ClassLoader parent){ | |
//super(string2Url(System.getProperty("topology.classpath").split(File.separator)), parent); | |
super(new URL[]{}, parent); | |
System.out.println("TopologyClassLoader: java.system.class.loader: " + System.getProperty("java.system.class.loader")); | |
System.out.println("TopologyClassLoader: topology.classpath: " + System.getProperty("topology.classpath")); | |
} | |
@Override | |
public synchronized Class<?> loadClass(String name) | |
throws ClassNotFoundException { | |
System.out.println("Loader: " + this.name + ", CLASSNAME: " + name); | |
Class<?> clazz = this.findLoadedClass(name); | |
if (clazz == null) { | |
// first find the class from the topology's dependencies | |
try { | |
clazz = this.findClass(name); | |
} catch (ClassNotFoundException e) { | |
// ignore it. | |
} | |
if (clazz == null) { | |
// then delegate to parent loader to load | |
clazz = this.getParent().loadClass(name); | |
} | |
} | |
return clazz; | |
} | |
public Enumeration<URL> getResources(String name) throws IOException { | |
Enumeration<URL> ret = this.findResources(name); | |
if (ret == null || !ret.hasMoreElements()) { | |
ret = this.getParent().getResources(name); | |
} | |
return ret; | |
} | |
/** | |
* Takes a collection of file path, return an array of corresponding URL. | |
* @param urls | |
* @return | |
*/ | |
protected static URL[] string2Url(Collection<String> urls) { | |
URL[] urlArr = new URL[urls.size()]; | |
int idx = 0; | |
for (String url : urls) { | |
try { | |
System.out.println("processing url: " + url); | |
urlArr[idx] = new File(url).toURI().toURL(); | |
idx++; | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
} | |
return urlArr; | |
} | |
protected static URL[] string2Url(String[] urls) { | |
URL[] urlArr = new URL[urls.length]; | |
int idx = 0; | |
for (String url : urls) { | |
try { | |
urlArr[idx] = new File(url).toURI().toURL(); | |
idx++; | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
} | |
return urlArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment