Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1607: SimpleClassPathModelFinder not returning list of matching paths #652

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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