From 6a9c588d33e481e3ed6b3b5045e97b1431c02117 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 5 Oct 2012 16:35:40 +0200 Subject: [PATCH] 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. --- utils/src/com/cloud/utils/script/Script.java | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/utils/src/com/cloud/utils/script/Script.java b/utils/src/com/cloud/utils/script/Script.java index feed76489d6..a0f9e8e3e88 100755 --- a/utils/src/com/cloud/utils/script/Script.java +++ b/utils/src/com/cloud/utils/script/Script.java @@ -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 { 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)); }