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/05/25 02:18:48 UTC

[incubator-nlpcraft] branch NLPCRAFT-327 updated: Fix for NLPCRAFT-327.

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

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


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

commit bc7b73fab4aa774ec2080e0028edb94c480fa900
Author: Aaron Radzinzski <ar...@datalingvo.com>
AuthorDate: Mon May 24 19:18:37 2021 -0700

    Fix for NLPCRAFT-327.
---
 .../org/apache/nlpcraft/common/util/NCUtils.scala  | 12 +++++++
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 19 +++-------
 .../nlpcraft/model/tools/cmdline/NCCliBase.scala   |  8 ++---
 .../org/apache/nlpcraft/probe/NCProbeBoot.scala    |  2 ++
 .../org/apache/nlpcraft/server/NCServer.scala      | 40 ++++++++++++----------
 5 files changed, 44 insertions(+), 37 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 4a3bccc..fcfbad2 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -44,6 +44,7 @@ import com.typesafe.scalalogging.{LazyLogging, Logger}
 import org.apache.commons.codec.binary.Base64
 import org.apache.commons.codec.digest.DigestUtils
 import org.apache.commons.io.IOUtils
+import org.apache.commons.lang3.SystemUtils
 import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.common.ansi.NCAnsi._
 import org.apache.nlpcraft.common.blowfish.NCBlowfishHasher
@@ -340,6 +341,17 @@ object NCUtils extends LazyLogging {
     def toPath(`class`: Class[_]): String = `class`.getPackage.getName.replaceAll("\\.", "/")
 
     /**
+     * Ensures that NLPCraft home folder (${USER_HOME}/.nlpcraft) folder exists.
+     * Creates one, if necessary.
+     */
+    def ensureHomeDir(): Unit = {
+        val home = new File(SystemUtils.getUserHome, ".nlpcraft")
+
+        if (!home.exists() && !home.mkdirs())
+            throw new NCException(s"Failed to create NLPCraft internal directory: ${home.getAbsolutePath}")
+    }
+
+    /**
       * Reads lines from given file.
       *
       * @param path File path to read from.
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 e386cf0..019308d 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
@@ -72,6 +72,8 @@ object NCCli extends NCCliBase {
 
     var term: Terminal = _
 
+    U.ensureHomeDir()
+
     NCModule.setModule(NCModule.CLI)
 
     // Project templates for 'gen-project' command.
@@ -172,7 +174,6 @@ object NCCli extends NCCliBase {
      * @param args
      * @param id
      * @param dflt
-     * @throws
      * @return
      */
     @throws[InvalidParameter]
@@ -195,7 +196,6 @@ object NCCli extends NCCliBase {
      * @param args
      * @param id
      * @param dflt
-     * @throws
      * @return
      */
     @throws[InvalidParameter]
@@ -317,15 +317,6 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * Checks whether given list of models contains class names outside of NLPCraft project.
-     *
-     * @param mdls Comma-separated list of fully qualified class names for data models.
-     * @return
-     */
-    private def hasExternalModels(mdls: String): Boolean =
-        U.splitTrimFilter(mdls, ",").exists(!_.startsWith("org.apache.nlpcraft."))
-
-    /**
      * Handles tilda and checks that every component of the given class path exists relative to the current user working
      * directory of this process.
      *
@@ -349,7 +340,7 @@ object NCCli extends NCCliBase {
     private def cleanUpTempFiles(): Unit = {
         val tstamp = currentTime - 1000 * 60 * 60 * 24 * 2 // 2 days ago.
 
-        val files = new File(SystemUtils.getUserHome, ".nlpcraft").listFiles()
+        val files = new File(SystemUtils.getUserHome, NLPCRAFT_LOC_DIR).listFiles()
         
         if (files != null)
             for (file <- files)
@@ -988,7 +979,7 @@ object NCCli extends NCCliBase {
                     beacon.ph = ph
 
                     // See if we can detect server log if server was started by this script.
-                    val files = new File(SystemUtils.getUserHome, ".nlpcraft").listFiles(new FilenameFilter {
+                    val files = new File(SystemUtils.getUserHome, NLPCRAFT_LOC_DIR).listFiles(new FilenameFilter {
                         override def accept(dir: File, name: String): Boolean =
                             name.startsWith(s".pid_$ph")
                     })
@@ -1092,7 +1083,7 @@ object NCCli extends NCCliBase {
                     beacon.ph = ph
 
                     // See if we can detect probe log if server was started by this script.
-                    val files = new File(SystemUtils.getUserHome, ".nlpcraft").listFiles(new FilenameFilter {
+                    val files = new File(SystemUtils.getUserHome, NLPCRAFT_LOC_DIR).listFiles(new FilenameFilter {
                         override def accept(dir: File, name: String): Boolean =
                             name.startsWith(s".pid_$ph")
                     })
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
index 0d3bd9c..c8995bb 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
@@ -32,7 +32,7 @@ import java.util.regex.Pattern
 class NCCliBase extends App {
     final val NAME = "NLPCraft CLI"
 
-    private final val dir = ".nlpcraft"
+    protected final val NLPCRAFT_LOC_DIR = ".nlpcraft"
 
     /*
      * Disable warnings from Ignite on JDK 11.
@@ -67,9 +67,9 @@ class NCCliBase extends App {
     // | MAKE SURE TO UPDATE THIS VAR WHEN NUMBER OF SERVICES IS CHANGED. |
     // +==================================================================+
 
-    final val SRV_BEACON_PATH = s"$dir/server_beacon"
-    final val PRB_BEACON_PATH = s"$dir/probe_beacon"
-    final val HIST_PATH = s"$dir/.cli_history"
+    final val SRV_BEACON_PATH = s"$NLPCRAFT_LOC_DIR/server_beacon"
+    final val PRB_BEACON_PATH = s"$NLPCRAFT_LOC_DIR/probe_beacon"
+    final val HIST_PATH = s"$NLPCRAFT_LOC_DIR/.cli_history"
 
     final val DFLT_USER_EMAIL = "admin@admin.com"
     final val DFLT_USER_PASSWD = "admin"
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
index 5815871..aa2f2cc 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -184,6 +184,8 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
     private [probe] def start(args: Array[String], fut: CompletableFuture[Integer]): Unit = {
         checkStarted()
 
+        U.ensureHomeDir()
+
         val cfg = initializeConfig(args, None)
 
         new Thread() {
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
index 77c773e..b9dfafd 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -173,6 +173,8 @@ object NCServer extends App with NCIgniteInstance with LazyLogging with NCOpenCe
     private def start(): Unit = {
         NCAnsi.ackStatus()
 
+        U.ensureHomeDir()
+
         setSysProps()
 
         NCConfigurable.initialize(
@@ -244,25 +246,25 @@ object NCServer extends App with NCIgniteInstance with LazyLogging with NCOpenCe
             final object Config extends NCConfigurable {
                 final private val pre = "nlpcraft.server"
 
-                lazy val pid = ProcessHandle.current().pid()
-                lazy val restHost = getString(s"$pre.rest.host")
-                lazy val restApi = getString(s"$pre.rest.apiImpl")
-                lazy val restPort = getInt(s"$pre.rest.port")
-                lazy val upLink = getString(s"$pre.probe.links.upLink")
-                lazy val downLink = getString(s"$pre.probe.links.downLink")
-                lazy val dbUrl = getString(s"$pre.database.jdbc.url")
-                lazy val dbDriver = getString(s"$pre.database.jdbc.driver")
-                lazy val dbPoolMin = getInt(s"$pre.database.c3p0.pool.minSize")
-                lazy val dbPoolMax = getInt(s"$pre.database.c3p0.pool.maxSize")
-                lazy val dbPoolInit = getInt(s"$pre.database.c3p0.pool.initSize")
-                lazy val dbPoolInc = getInt(s"$pre.database.c3p0.pool.acquireIncrement")
-                lazy val dbInit = getBool(s"$pre.database.igniteDbInitialize")
-                lazy val tokProviders = getString(s"$pre.tokenProviders")
-                lazy val acsToksScanMins = getInt(s"$pre.user.timeoutScannerFreqMins")
-                lazy val acsToksExpireMins = getInt(s"$pre.user.accessTokenExpireTimeoutMins")
-                lazy val nlpEngine = getString("nlpcraft.nlpEngine")
-                lazy val extCfgUrl = getString("nlpcraft.extConfig.extUrl")
-                lazy val extCfgCheckMd5 = getBool("nlpcraft.extConfig.checkMd5")
+                lazy val pid: Long = ProcessHandle.current().pid()
+                lazy val restHost: String = getString(s"$pre.rest.host")
+                lazy val restApi: String = getString(s"$pre.rest.apiImpl")
+                lazy val restPort: Int = getInt(s"$pre.rest.port")
+                lazy val upLink: String = getString(s"$pre.probe.links.upLink")
+                lazy val downLink: String = getString(s"$pre.probe.links.downLink")
+                lazy val dbUrl: String = getString(s"$pre.database.jdbc.url")
+                lazy val dbDriver: String = getString(s"$pre.database.jdbc.driver")
+                lazy val dbPoolMin: Int = getInt(s"$pre.database.c3p0.pool.minSize")
+                lazy val dbPoolMax: Int = getInt(s"$pre.database.c3p0.pool.maxSize")
+                lazy val dbPoolInit: Int = getInt(s"$pre.database.c3p0.pool.initSize")
+                lazy val dbPoolInc: Int = getInt(s"$pre.database.c3p0.pool.acquireIncrement")
+                lazy val dbInit: Boolean = getBool(s"$pre.database.igniteDbInitialize")
+                lazy val tokProviders: String = getString(s"$pre.tokenProviders")
+                lazy val acsToksScanMins: Int = getInt(s"$pre.user.timeoutScannerFreqMins")
+                lazy val acsToksExpireMins: Int = getInt(s"$pre.user.accessTokenExpireTimeoutMins")
+                lazy val nlpEngine: String = getString("nlpcraft.nlpEngine")
+                lazy val extCfgUrl: String = getString("nlpcraft.extConfig.extUrl")
+                lazy val extCfgCheckMd5: Boolean = getBool("nlpcraft.extConfig.checkMd5")
                 lazy val restEndpoint = s"${Config.restHost}:${Config.restPort}/api/v1"
             }