Skip to content

Commit

Permalink
OPENNLP-1607: SimpleClassPathModelFinder not returning list of matchi…
Browse files Browse the repository at this point in the history
…ng paths (#652)

* Update SimpleClassPathModelFinder.java

Updated SimpleClassPathModelFinder to return a list of paths instead of the delimiter

* Updating configuration to run skipped surefire tests

* Update SimpleClassPathModelFinder.java

Updating getClassPathUrlsFromSystemProperty to handle Windows paths
  • Loading branch information
codyfearer authored Sep 20, 2024
1 parent a1c46d1 commit 025f1b7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
23 changes: 21 additions & 2 deletions opennlp-tools-models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,32 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<configuration>
<argLine>-Xmx2048m -Dorg.slf4j.simpleLogger.defaultLogLevel=off --add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
<forkCount>${opennlp.forkCount}</forkCount>
<useSystemClassLoader>false</useSystemClassLoader>
<failIfNoSpecifiedTests>false</failIfNoSpecifiedTests>
</configuration>
<executions>
<execution>
<id>with-reflection</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>-Xmx2048m -Dorg.slf4j.simpleLogger.defaultLogLevel=off --add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
</configuration>
</execution>
<!-- This phase is here to trigger a fallback to the classpath from the system properties, which cannot be hit, if add-opens is present. -->
<execution>
<id>no-reflection</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>-Xmx2048m -Dorg.slf4j.simpleLogger.defaultLogLevel=off</argLine>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Locale;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
Expand Down Expand Up @@ -64,7 +63,8 @@ public class SimpleClassPathModelFinder extends AbstractClassPathModelFinder imp

private static final Logger logger = LoggerFactory.getLogger(SimpleClassPathModelFinder.class);
private static final String FILE_PREFIX = "file";
private static final Pattern CLASSPATH_SEPARATOR_PATTERN = Pattern.compile("[;:]");
private static final Pattern CLASSPATH_SEPARATOR_PATTERN_WINDOWS = Pattern.compile(";");
private static final Pattern CLASSPATH_SEPARATOR_PATTERN_UNIX = Pattern.compile(":");
// ; for Windows, : for Linux/OSX

/**
Expand Down Expand Up @@ -191,20 +191,26 @@ private List<URL> getClassPathElements() {
if (fromUcp != null && fromUcp.length > 0) {
return Arrays.asList(fromUcp);
} else {
final String cp = System.getProperty("java.class.path", "");
final Matcher matcher = CLASSPATH_SEPARATOR_PATTERN.matcher(cp);
final List<URL> jarUrls = new ArrayList<>();
while (matcher.find()) {
try {
jarUrls.add(new URL(FILE_PREFIX, "", matcher.group()));
} catch (MalformedURLException ignored) {
//if we cannot parse a URL from the system property, just ignore it...
//we couldn't load it anyway
}
}
return jarUrls;
return getClassPathUrlsFromSystemProperty();
}
}
}

private List<URL> getClassPathUrlsFromSystemProperty() {
final String cp = System.getProperty("java.class.path", "");
final String[] matches = isWindows()
? CLASSPATH_SEPARATOR_PATTERN_WINDOWS.split(cp)
: CLASSPATH_SEPARATOR_PATTERN_UNIX.split(cp);
final List<URL> jarUrls = new ArrayList<>();
for (String classPath: matches) {
try {
jarUrls.add(new URL(FILE_PREFIX, "", classPath));
} catch (MalformedURLException ignored) {
//if we cannot parse a URL from the system property, just ignore it...
//we couldn't load it anyway
}
}
return jarUrls;
}

/*
Expand Down

0 comments on commit 025f1b7

Please sign in to comment.