You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2021/04/26 13:53:24 UTC
[incubator-nlpcraft] 01/01: Configuration processing fixed.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-304
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 3e4ebef19c4491c2e7689db78604951e5ebc8269
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Mon Apr 26 16:51:45 2021 +0300
Configuration processing fixed.
---
.../nlpcraft/common/config/NCConfigurable.scala | 59 ++++++++++++----------
.../org/apache/nlpcraft/common/util/NCUtils.scala | 26 ++++++++++
.../org/apache/nlpcraft/probe/NCProbeBoot.scala | 13 ++---
.../org/apache/nlpcraft/server/NCServer.scala | 10 ++--
4 files changed, 66 insertions(+), 42 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/config/NCConfigurable.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/config/NCConfigurable.scala
index bdbaa53..7fcab34 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/config/NCConfigurable.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/config/NCConfigurable.scala
@@ -21,6 +21,8 @@ import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.LazyLogging
import org.apache.nlpcraft.common._
+import java.io.File
+import java.net.{MalformedURLException, URL}
import scala.collection.JavaConverters._
/**
@@ -289,37 +291,38 @@ object NCConfigurable extends LazyLogging {
overrideCfg: Option[Config],
cfgFileOpt: Option[String],
dfltCfg: Option[Config],
- valFun: Config ⇒ Boolean): Unit = {
- var tmpCfg: Config = null
-
+ valFun: Config ⇒ Boolean
+ ): Unit = {
require(cfgFileOpt.isDefined || dfltCfg.isDefined)
-
- // Only default configuration is provided.
- if (cfgFileOpt.isEmpty) {
- logger.info(s"Using built-in default configuration.")
- tmpCfg = ConfigFactory.load(dfltCfg.get)
- }
- else {
- val name = cfgFileOpt.get
-
- logger.info(s"Attempting to load/merge configuration from configuration file: $name")
-
- tmpCfg =
+ val tmpCfg =
+ // Only default configuration is provided.
+ if (cfgFileOpt.isEmpty) {
+ logger.info(s"Using built-in default configuration.")
+
+ ConfigFactory.load(dfltCfg.get)
+ }
+ else {
+ val name = cfgFileOpt.get
+
+ logger.info(s"Attempting to load/merge configuration from configuration file: $name")
+
+ // Order is: file, URL, resource (File and URL can override resource)
+ var cfg = ConfigFactory.parseFile(new File(name))
+
+ try
+ cfg = cfg.withFallback(ConfigFactory.parseURL(new URL(name)))
+ catch {
+ case _: MalformedURLException ⇒ // No-op.
+ }
+
+ cfg = cfg.withFallback(ConfigFactory.parseResources(name))
+
if (dfltCfg.isDefined)
- ConfigFactory.load(
- ConfigFactory.
- parseFile(new java.io.File(name)).
- withFallback(ConfigFactory.parseResources(name)).
- withFallback(dfltCfg.get)
- )
- else
- ConfigFactory.load(
- ConfigFactory.
- parseFile(new java.io.File(name)).
- withFallback(ConfigFactory.parseResources(name))
- )
- }
+ cfg = cfg.withFallback(dfltCfg.get)
+
+ cfg
+ }
// Validate.
if (!valFun(tmpCfg)) {
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 f0c9f2b..b51c63d 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
@@ -2158,4 +2158,30 @@ object NCUtils extends LazyLogging {
!list.zip(list.tail).exists { case (x, y) ⇒ x > y }
}
}
+
+ /**
+ *
+ * @param s
+ */
+ def isSuitableConfig(s: String): Boolean = {
+ def isFile: Boolean = {
+ val f = new File(s)
+
+ f.exists() && f.isFile
+ }
+
+ def isResource: Boolean = getClass.getClassLoader.getResource(s) != null
+
+ def isUrl: Boolean =
+ try {
+ new URL(s)
+
+ true
+ }
+ catch {
+ case _: MalformedURLException ⇒ false
+ }
+
+ isFile || isResource || isUrl
+ }
}
\ No newline at end of file
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 cacab98..f8e75ee 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -135,15 +135,12 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
overrideCfg,
args.find(_.startsWith("-config=")) match {
case Some(s) ⇒
- val fileName = s.substring("-config=".length)
-
- val f = new java.io.File(fileName)
-
- if (!(f.exists && f.canRead && f.isFile))
- throw new NCE(s"Specified probe configuration file does not exist or cannot be read: $fileName")
-
- Some(fileName)
+ val cfg = s.substring("-config=".length)
+ if (!U.isSuitableConfig(cfg))
+ throw new NCE(s"Specified probe configuration file does not exist or cannot be read: $cfg")
+
+ Some(cfg)
case None ⇒ Some("nlpcraft.conf")
},
Some(mkDefault()),
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 5270eec..e2cfaf0 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -179,14 +179,12 @@ object NCServer extends App with NCIgniteInstance with LazyLogging with NCOpenCe
None, // No overrides.
args.find(_.startsWith("-config=")) match {
case Some(s) ⇒
- val fileName = s.substring("-config=".length)
+ val cfg = s.substring("-config=".length)
- val f = new java.io.File(fileName)
+ if (!U.isSuitableConfig(cfg))
+ throw new NCE(s"Specified probe configuration file does not exist or cannot be read: $cfg")
- if (!(f.exists && f.canRead && f.isFile))
- throw new NCE(s"Specified server configuration file does not exist or cannot be read: $fileName")
-
- Some(fileName)
+ Some(cfg)
case None ⇒
Some("nlpcraft.conf") // Default to 'nlpcraft.conf'.