Skip to content

Commit 6a9c588

Browse files
committed
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.
1 parent e59726a commit 6a9c588

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

utils/src/com/cloud/utils/script/Script.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.InputStreamReader;
2424
import java.io.PrintWriter;
2525
import java.io.StringWriter;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
2628
import java.net.URL;
2729
import java.util.ArrayList;
2830
import java.util.List;
@@ -344,14 +346,23 @@ public static String findScript(String path, String script) {
344346
return file.getAbsolutePath();
345347
}
346348

347-
/* url = Script.class.getClassLoader().getResource(path);
348-
* s_logger.debug("Classpath resource: " + url);
349-
* if (url != null) {
350-
* file = new File(url.getFile());
351-
* s_logger.debug("Absolute path = " + file.getAbsolutePath());
352-
* return file.getAbsolutePath();
353-
* }
354-
*/
349+
/**
350+
* Look in WEB-INF/classes of the webapp
351+
* URI workaround the URL encoding of url.getFile
352+
*/
353+
url = Script.class.getClassLoader().getResource(path + script);
354+
s_logger.debug("Classpath resource: " + url);
355+
if (url != null) {
356+
try {
357+
file = new File(new URI(url.toString()).getPath());
358+
s_logger.debug("Absolute path = " + file.getAbsolutePath());
359+
return file.getAbsolutePath();
360+
}
361+
catch (URISyntaxException e) {
362+
s_logger.warn("Unable to convert " + url.toString() + " to a URI");
363+
}
364+
}
365+
355366
if (path.endsWith(File.separator)) {
356367
path = path.substring(0, path.lastIndexOf(File.separator));
357368
}

0 commit comments

Comments
 (0)