org.snapscript.studio.agent.ClassPathUpdater Maven / Gradle / Ivy
package org.snapscript.studio.agent;
import java.io.File;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
public class ClassPathUpdater {
private static final String ADD_URL_METHOD = "addURL";
public static List parseClassPath(String dependencies) throws Exception {
Reader source = new StringReader(dependencies);
LineNumberReader reader = new LineNumberReader(source);
List files = new ArrayList();
try {
String line = reader.readLine();
while(line != null) {
String token = line.trim();
int length = token.length();
if(length > 0) {
if(!token.startsWith("#")) {
File file = new File(token);
files.add(file);
}
}
line = reader.readLine();
}
} finally {
reader.close();
}
return files;
}
public static ClassLoader updateClassPath(String dependencies) throws Exception {
URLClassLoader loader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod(ADD_URL_METHOD, URL.class);
List files = parseClassPath(dependencies);
int size = files.size();
if(size > 0) {
for(int i = 0; i < size; i++){
File file = files.get(i);
URI location = file.toURI();
URL path = location.toURL();
method.setAccessible(true);
method.invoke(loader, path);
}
}
return loader;
}
}