Fix loading of external script so they will be loaded from the webapp classloader.

This change will allow the Script class to look for resources in the classpath of the webapp. This makes it possible to distribute the management server as a single prepackaged war. An added benefit is easier integration with IDE's that have the option to start webapps internally.

Also fixes a bug/feature in the URL handling were some components of the script path were translated to urlencoding. This  change means that files are more often found in the first two steps of the findScript method which saves some filesystem calls.
This commit is contained in:
Hugo Trippaers 2012-10-05 16:35:40 +02:00
parent e59726ad95
commit 6a9c588d33

View File

@ -23,6 +23,8 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -344,14 +346,23 @@ public class Script implements Callable<String> {
return file.getAbsolutePath();
}
/* url = Script.class.getClassLoader().getResource(path);
* s_logger.debug("Classpath resource: " + url);
* if (url != null) {
* file = new File(url.getFile());
* s_logger.debug("Absolute path = " + file.getAbsolutePath());
* return file.getAbsolutePath();
* }
*/
/**
* Look in WEB-INF/classes of the webapp
* URI workaround the URL encoding of url.getFile
*/
url = Script.class.getClassLoader().getResource(path + script);
s_logger.debug("Classpath resource: " + url);
if (url != null) {
try {
file = new File(new URI(url.toString()).getPath());
s_logger.debug("Absolute path = " + file.getAbsolutePath());
return file.getAbsolutePath();
}
catch (URISyntaxException e) {
s_logger.warn("Unable to convert " + url.toString() + " to a URI");
}
}
if (path.endsWith(File.separator)) {
path = path.substring(0, path.lastIndexOf(File.separator));
}