Reputation: 220762
I would like to compile and load new classes at runtime within a weblogic 10.3 server. Class loading seems to be somewhat straightforward:
class ClassFileManager
extends ForwardingJavaFileManager<StandardJavaFileManager> {
Map<String, JavaClassObject> classes = new HashMap<String, JavaClassObject>();
public ClassFileManager(StandardJavaFileManager standardManager) {
super(standardManager);
}
@Override
public ClassLoader getClassLoader(Location location) {
return new SecureClassLoader(currentThread().getContextClassLoader()) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = classes.get(name).getBytes();
return super.defineClass(name, b, 0, b.length);
}
};
}
@Override
public JavaFileObject getJavaFileForOutput(
Location location, String className, Kind kind, FileObject sibling)
throws IOException {
JavaClassObject result = new JavaClassObject(className, kind);
classes.put(className, result);
return result;
}
}
The simplest way to perform class loading seems to be to initialise a SecureClassLoader
and have it use the contextClassLoader
as the parent.
But when setting up the -classpath
option for the JDK's runtime compiler, I cannot seem to find a "context classpath" in a string form. The following is a bit of a hack that works "well enough":
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ClassFileManager fileManager =
new ClassFileManager(compiler.getStandardFileManager(null, null, null));
List<String> options = new ArrayList<String>();
options.add("-classpath");
options.add(System.getProperty("java.class.path") + ";" +
getClass().getProtectionDomain()
.getCodeSource().getLocation()
.toURI().toString()
.replace("file:/", "").replace("/", "\\"));
But it doesn't generate the complete class path of the context class loader. How can I do it, reliably? Can I?
Upvotes: 5
Views: 3080
Reputation: 67287
I suggest you make your custom JavaFileManager
aware of the context classloader and specify it as an argument to JavaCompiler.getTask
, along the lines of @anttix's idea.
For more info and a sample implementation including explanation (which is too verbose to repeat here) see blog post Using built-in JavaCompiler with a custom classloader.
Upvotes: 1
Reputation: 7779
WebLogic 10.3.6 has a fairly complex ClassLoader
implementation. Fortunately the classloader used for web applications exposes a getClassPath
method.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
String classPath = ((weblogic.utils.classloaders.GenericClassLoader)cl).getClassPath();
// Once we have a classpath it's standard procedure
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null);
List<String> optionList = new ArrayList<String>();
optionList.addAll(Arrays.asList("-classpath", classPath));
compiler.getTask(null, sfm, null, optionList, null, sources).call();
Upvotes: 2
Reputation: 7779
The Open Source Jasper JSP compiler used by Tomcat interrogates context URLClassLoader to generate a classpath string that is passed to the compiler.
If WebLogic does not expose getURLs
method, an alternative is to use a custom implementation of JavaFileManager
that uses context classloader getResource()
method to fetch class files.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager customFileManager = ...
compiler.getTask(null, customFileManager, null, null, null, sources).call();
There is a complete example available here.
Upvotes: 1
Reputation: 41
Since "WebApplicationClassLoader" is a kind of "URLClassLoader", maybe you can use this code snippet.
ClassLoader classLoader = getClass().getClassLoader();
System.out.println("ClassLoader: " + classLoader);
URLClassLoader urlClassLoader = (URLClassLoader)classLoader;
URL[] urls = urlClassLoader.getURLs();
for (URL u : urls) {
System.out.println("url: " + u);
}
This code lists all jars and directories in classpath.
Upvotes: 0
Reputation: 41
Maybe this can help you. It works for my project on WebLogic.
String getClassPath() {
final String BASE_PATH = "<your_project_folder_name>";
String path = "";
String classPathProperty = System.getProperty("java.class.path");
if (classPathProperty != null) {
path = classPathProperty + File.pathSeparator;
}
URL classLocation = this.getClass().getProtectionDomain().getCodeSource().getLocation();
URL classesLocation = this.getClass().getClassLoader().getResource("/");
if (classesLocation == null) {
path = path + classLocation.getPath();
}
else {
String classesLocationPath = classesLocation.getPath();
String libsLocationPath = classesLocationPath + "../lib";
File libsLocation = new File(libsLocationPath);
if (libsLocation.exists() == false) {
libsLocationPath = URLDecoder.decode(classesLocationPath + "../" + BASE_PATH + "/WEB-INF/lib/");
libsLocation = new File(libsLocationPath);
}
File[] filesInLibraryPath = libsLocation.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
if (filesInLibraryPath != null) {
for (File libraryFile : filesInLibraryPath) {
libsLocationPath += File.pathSeparator + URLDecoder.decode(libraryFile.getAbsolutePath());
}
}
path = path +
classLocation.getPath() + File.pathSeparator +
classesLocationPath + File.pathSeparator +
libsLocationPath;
path = URLDecoder.decode(path);
}
return path;
}
Upvotes: 1