You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2021/07/10 22:01:39 UTC

[incubator-nlpcraft] branch NLPCRAFT-357 updated: Fix for NLPCRAFT-357

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

aradzinski pushed a commit to branch NLPCRAFT-357
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-357 by this push:
     new 7232f8c  Fix for NLPCRAFT-357
7232f8c is described below

commit 7232f8cf8509e5bd1b1699ba057b402b2fd37464
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Sat Jul 10 15:01:29 2021 -0700

    Fix for NLPCRAFT-357
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       |  7 +--
 .../tools/cmdline/NCCliFileNameCompleter.java      |  3 --
 .../tools/cmdline/NCCliModelClassCompleter.java    | 56 +++++++++++++++++++++-
 3 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 7845c0d..fc9819e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -54,6 +54,7 @@ import org.jline.utils.InfoCmp.Capability
 import org.apache.nlpcraft.model.tools.cmdline.NCCliRestSpec._
 import org.apache.nlpcraft.model.tools.cmdline.NCCliCommands._
 
+import java.util
 import scala.util.Using
 import scala.collection.mutable
 import scala.compat.java8.OptionConverters._
@@ -2328,7 +2329,7 @@ object NCCli extends NCCliBase {
             private val cmds = CMDS.map(c => (c.name, c.synopsis, c.group))
 
             private val fsCompleter = new NCCliFileNameCompleter()
-            private val cpCompleter = new NCCliModelClassCompleter()
+            private val mdlClsCompleter = new NCCliModelClassCompleter()
 
             // All '--cp' names.
             private val CP_PARAM_NAMES = (
@@ -2478,7 +2479,7 @@ object NCCli extends NCCliBase {
                         .mkString(CP_SEP)
 
                         try {
-                            for (cls <- cpCompleter.getModelClassNamesFromClasspath(cp.split(CP_SEP_CHAR).toSeq.asJava).asScala)
+                            for (cls <- mdlClsCompleter.getModelClassNamesFromClasspath(cp.split(CP_SEP_CHAR).toSeq.asJava).asScala)
                                 candidates.add(
                                     mkCandidate(
                                         disp = "--mdls=" + cls,
@@ -2490,7 +2491,7 @@ object NCCli extends NCCliBase {
                         }
                         catch {
                             // Just ignore.
-                            case e: Exception => ()
+                            case _: Exception => ()
                         }
                     }
 
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
index 1dd31cc..8c69e1c 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
@@ -107,9 +107,6 @@ class NCCliFileNameCompleter
                     }
                 });
             }
-            catch (IOException e) {
-                // Ignore.
-            }
         }
         catch (Exception e) {
             // Ignore.
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliModelClassCompleter.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliModelClassCompleter.java
index 50a6412..f567e69 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliModelClassCompleter.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliModelClassCompleter.java
@@ -23,9 +23,12 @@ import java.io.*;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.*;
 import java.util.jar.*;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * Completer for model classes from classpath. Currently only JAR files are supported.
@@ -62,6 +65,46 @@ class NCCliModelClassCompleter {
 
     /**
      *
+     * @param dirPath Path of the directory.
+     * @return Set of class names from the given JAR file.
+     * @throws IOException Thrown in case of any I/O errors.
+     */
+    private Set<String> getClassNameFromDirectory(File dirPath) throws IOException {
+        assert dirPath != null;
+
+        int dirPathLen = dirPath.getAbsolutePath().length();
+        Set<String> classNames = new HashSet<>();
+
+        try (Stream<Path> pathTree = Files.walk(dirPath.toPath())) {
+            pathTree.forEach(path -> {
+                File file = path.toFile();
+
+                if (file.isFile() && !file.isHidden() && file.getName().toLowerCase().endsWith(".class")) {
+                    String clsName = file.getAbsolutePath().substring(dirPathLen)
+                        .replace(File.separator, ".")
+                        .replace(".class", "");
+
+                    if (clsName.startsWith("."))
+                        clsName = clsName.substring(1);
+
+                    classNames.add(clsName);
+                }
+            });
+        }
+
+        return classNames;
+    }
+
+    /**
+     *
+     * @param path Path to test.
+     */
+    private boolean isJar(String path) {
+        return path.toLowerCase().endsWith(".jar");
+    }
+
+    /**
+     *
      * @param cp List of classpath entries.
      * @return Set of model class name for the given classpath.
      * @throws IOException Thrown in case of any I/O errors.
@@ -69,9 +112,16 @@ class NCCliModelClassCompleter {
     public Set<String> getModelClassNamesFromClasspath(List<String> cp) throws IOException {
         Set<URL> urls = cp.stream().map(entry -> {
             try {
-                return new URL("jar:file:" + entry + "!/");
+                if (isJar(entry))
+                    return new URL("jar:file:" + entry + "!/");
+                else {
+                    boolean trailingSlash = entry.endsWith("/");
+
+                    return new URL("file:" + entry + (trailingSlash ? "" : "/"));
+                }
             }
             catch (MalformedURLException e) {
+                e.printStackTrace();
                 return null;
             }
         }).filter(Objects::nonNull).collect(Collectors.toSet());
@@ -85,7 +135,9 @@ class NCCliModelClassCompleter {
         try (URLClassLoader clsLdr = URLClassLoader.newInstance(urlsArr)) {
             for (String cpEntry : cp) {
                 try {
-                    Set<String> classNames = getClassNamesFromJar(new File(cpEntry));
+                    File file = new File(cpEntry);
+
+                    Set<String> classNames = isJar(cpEntry) ? getClassNamesFromJar(file) : getClassNameFromDirectory(file);
 
                     for (String name : classNames) {
                         try {