Skip to content

Commit

Permalink
Fix BMUnit resource-based script lookup
Browse files Browse the repository at this point in the history
The original lookup logic involved function `normalize` which is intended for file-system path manipulation. On Windows this means that all path components are separated by a backslash; however such paths are not usable in `Classloader.getResource()`. The logic was fixed to use `/` for resource path construction.

In addition, it appears that methods `getLoadDirectory` and `getResourceLoadDirectory` in `org.jboss.byteman.contrib.bmunit.BMUnitConfigState` did incorrectly determine whether to do a chained lookup, contradicting associated comments and possibly causing NullPointerException. The respective conditions were fixed.
  • Loading branch information
bbobcik authored and adinn committed Mar 15, 2024
1 parent ebba8a4 commit 51ee6cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions contrib/bmunit/src/org/jboss/byteman/contrib/bmunit/BMUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ protected static String findScript(String dir, String name) {
} else {
// n.b. defaults either are "" or end with correct separator
filename = normalize(getLoadDirectory(), true) + filename;
resourceName = normalize(getResourceLoadDirectory(), true) + filename;
String resourceDir = getResourceLoadDirectory();
if(resourceDir != null && resourceDir.length() > 0) {
String separator = resourceDir.endsWith("/") ? "" : "/";
resourceName = resourceDir + separator + resourceName;
}
}

final String[] filenames={filename, filename + ".btm", filename + ".txt"};
Expand All @@ -347,11 +351,11 @@ protected static String findScript(String dir, String name) {
return fname;
}

ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
for(String rname: resourceNames) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
URL resource=loader.getResource(rname);
if(resource != null) {
File file=new File(resource.getFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public int getPort() {
public String getLoadDirectory() {
// loadDirectory can be overridden so don't look up previous
// unless it is empty or null
if ((loadDirectory != null || loadDirectory.length() == 0) && previous != null) {
if ((loadDirectory == null || loadDirectory.length() == 0) && previous != null) {
return previous.getLoadDirectory();
}
return loadDirectory;
Expand All @@ -849,7 +849,7 @@ public String getLoadDirectory() {
public String getResourceLoadDirectory() {
// loadDirectory can be overridden so don't look up previous
// unless it is empty or null
if ((resourceLoadDirectory != null || resourceLoadDirectory.length() == 0) && previous != null) {
if ((resourceLoadDirectory == null || resourceLoadDirectory.length() == 0) && previous != null) {
return previous.getResourceLoadDirectory();
}
return resourceLoadDirectory;
Expand Down

0 comments on commit 51ee6cf

Please sign in to comment.