Skip to content

Commit

Permalink
OPENNLP-1567 - OpenNLP Models: Provide a Finder / Loader Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rzo1 committed Jun 18, 2024
1 parent d21d2e5 commit a489c64
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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 @@ -63,6 +64,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("[;:]");
// ; for Windows, : for Linux/OSX

/**
* By default, it scans for "opennlp-models-*.jar".
Expand Down Expand Up @@ -99,6 +102,7 @@ protected List<URI> getMatchingURIs(String wildcardPattern, Object context) {
return Collections.emptyList();
}

final boolean isWindows = isWindows();
final List<URL> cp = getClassPathElements();
final List<URI> cpu = new ArrayList<>();
final Pattern jarPattern = Pattern.compile(asRegex("*" + getJarModelPrefix()));
Expand All @@ -107,7 +111,7 @@ protected List<URI> getMatchingURIs(String wildcardPattern, Object context) {
for (URL url : cp) {
if (matchesPattern(url, jarPattern)) {
try {
for (URI u : getURIsFromJar(url)) {
for (URI u : getURIsFromJar(url, isWindows)) {
if (matchesPattern(u.toURL(), filePattern)) {
cpu.add(u);
}
Expand Down Expand Up @@ -138,9 +142,13 @@ private boolean matchesPattern(URL url, Pattern pattern) {
return pattern.matcher(url.getFile()).matches();
}

private List<URI> getURIsFromJar(URL fileUrl) throws IOException {
private List<URI> getURIsFromJar(URL fileUrl, boolean isWindows) throws IOException {
final List<URI> uris = new ArrayList<>();
final URL jarUrl = new URL(JAR + ":" + escapeWindowsURL(fileUrl) + "!/");
final URL jarUrl = new URL(JAR + ":" +
(isWindows
? fileUrl.toString().replace("\\", "/")
: fileUrl.toString())
+ "!/");
final JarURLConnection jarConnection = (JarURLConnection) jarUrl.openConnection();
try (JarFile jarFile = jarConnection.getJarFile()) {
final Enumeration<JarEntry> entries = jarFile.entries();
Expand All @@ -160,11 +168,6 @@ private List<URI> getURIsFromJar(URL fileUrl) throws IOException {
return uris;
}


private String escapeWindowsURL(URL url) {
return isWindows() ? url.toString().replace("\\", "/") : url.toString();
}

private boolean isWindows() {
return System.getProperty("os.name", "unknown").toLowerCase(Locale.ROOT).contains("win");
}
Expand All @@ -189,11 +192,11 @@ private List<URL> getClassPathElements() {
return Arrays.asList(fromUcp);
} else {
final String cp = System.getProperty("java.class.path", "");
final String[] elements = cp.split("[;:]"); // ; for Windows, : for Linux/OSX
final Matcher matcher = CLASSPATH_SEPARATOR_PATTERN.matcher(cp);
final List<URL> jarUrls = new ArrayList<>();
for (String element : elements) {
while (matcher.find()) {
try {
jarUrls.add(new URL(FILE_PREFIX, "", element));
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
Expand All @@ -210,13 +213,13 @@ private List<URL> getClassPathElements() {
*/
private URL[] getURLs(ClassLoader classLoader) {
try {
final Class builtinClazzLoader = Class.forName("jdk.internal.loader.BuiltinClassLoader");
final Class<?> builtinClazzLoader = Class.forName("jdk.internal.loader.BuiltinClassLoader");

final Field ucpField = builtinClazzLoader.getDeclaredField("ucp");
ucpField.setAccessible(true);

final Object ucpObject = ucpField.get(classLoader);
final Class clazz = Class.forName("jdk.internal.loader.URLClassPath");
final Class<?> clazz = Class.forName("jdk.internal.loader.URLClassPath");

if (ucpObject != null) {
final Method getURLs = clazz.getMethod("getURLs");
Expand Down

0 comments on commit a489c64

Please sign in to comment.