This is an automated email from the ASF dual-hosted git repository.

jzemerick pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/main by this push:
     new 025f1b7a OPENNLP-1607: SimpleClassPathModelFinder not returning list 
of matching paths (#652)
025f1b7a is described below

commit 025f1b7a3ffc3d9813914317b0346a88f37ec89c
Author: Cody F <[email protected]>
AuthorDate: Fri Sep 20 16:01:40 2024 -0400

    OPENNLP-1607: SimpleClassPathModelFinder not returning list of matching 
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
---
 opennlp-tools-models/pom.xml                       | 23 +++++++++++++--
 .../models/simple/SimpleClassPathModelFinder.java  | 34 +++++++++++++---------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/opennlp-tools-models/pom.xml b/opennlp-tools-models/pom.xml
index 1bddfa5a..409e7852 100644
--- a/opennlp-tools-models/pom.xml
+++ b/opennlp-tools-models/pom.xml
@@ -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>
diff --git 
a/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
 
b/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
index 0d00f385..8e158952 100644
--- 
a/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
+++ 
b/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
@@ -33,7 +33,6 @@ import java.util.List;
 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;
@@ -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
 
   /**
@@ -191,20 +191,26 @@ public class SimpleClassPathModelFinder extends 
AbstractClassPathModelFinder imp
       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;
   }
 
   /*

Reply via email to