This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-398
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-398 by this push:
new 7cc397f WIP.
7cc397f is described below
commit 7cc397fb091baecedaf53a47296b628ca8a49d2a
Author: Sergey Kamov <[email protected]>
AuthorDate: Mon Aug 9 16:52:22 2021 +0300
WIP.
---
.../probe/mgrs/deploy/NCDeployManager.scala | 57 +++++++++++-----------
1 file changed, 28 insertions(+), 29 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
index 507ab45..fb65b47 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
@@ -109,13 +109,14 @@ object NCDeployManager extends NCService {
*/
case class SynonymHolder(elmId: String, syn: NCProbeSynonym)
- case class MethodHolder(objClassName: String, obj: Any, method: Method) {
+ case class MethodOwner(method: Method, objClassName: String, obj: Any) {
+ require(method != null)
require(objClassName != null ^ obj != null)
private var lazyObj: Any = obj
- def getObject: Any =
- if (lazyObj == null) {
+ def getObject: Any = {
+ if (lazyObj == null)
try
lazyObj = U.mkObject(objClassName)
catch {
@@ -123,10 +124,8 @@ object NCDeployManager extends NCService {
case e: Throwable => throw new NCE(s"Error initializing
object of type: $objClassName", e)
}
- lazyObj
- }
- else
- lazyObj
+ lazyObj
+ }
}
/**
@@ -1112,14 +1111,14 @@ object NCDeployManager extends NCService {
s"#${argIdx + (if (cxtFirstParam) 1 else 0)} of ${method2Str(mtd)}"
/**
- * @param mh
+ * @param mo
* @param mdl
* @param intent
*/
@throws[NCE]
- private def prepareCallback(mh: MethodHolder, mdl: NCModel, intent:
NCIdlIntent): Callback = {
+ private def prepareCallback(mo: MethodOwner, mdl: NCModel, intent:
NCIdlIntent): Callback = {
val mdlId = mdl.getId
- val mtd = mh.method
+ val mtd = mo.method
// Checks method result type.
if (mtd.getReturnType != CLS_QRY_RES)
@@ -1226,7 +1225,7 @@ object NCDeployManager extends NCService {
(ctx: NCIntentMatch) => {
invoke(
mdl.getId,
- mh,
+ mo,
(
(if (ctxFirstParam) Seq(ctx)
else Seq.empty) ++
@@ -1239,25 +1238,25 @@ object NCDeployManager extends NCService {
/**
* @param mdlId
- * @param mh
+ * @param mo
* @param args
*/
@throws[NCE]
- private def invoke(mdlId: String, mh: MethodHolder, args: Array[AnyRef]):
NCResult = {
- val obj = if (Modifier.isStatic(mh.method.getModifiers)) null else
mh.getObject
+ private def invoke(mdlId: String, mo: MethodOwner, args: Array[AnyRef]):
NCResult = {
+ val obj = if (Modifier.isStatic(mo.method.getModifiers)) null else
mo.getObject
- var flag = mh.method.canAccess(obj)
+ var flag = mo.method.canAccess(obj)
try {
if (!flag) {
- mh.method.setAccessible(true)
+ mo.method.setAccessible(true)
flag = true
}
else
flag = false
- mh.method.invoke(obj, args: _*).asInstanceOf[NCResult]
+ mo.method.invoke(obj, args: _*).asInstanceOf[NCResult]
}
catch {
case e: InvocationTargetException => e.getTargetException match {
@@ -1267,25 +1266,25 @@ object NCDeployManager extends NCService {
case e: Throwable =>
throw new NCE(s"Intent callback invocation error [" +
s"mdlId=$mdlId, " +
- s"callback=${method2Str(mh.method)}" +
+ s"callback=${method2Str(mo.method)}" +
s"]", e)
}
case e: Throwable =>
throw new NCE(s"Unexpected intent callback invocation error ["
+
s"mdlId=$mdlId, " +
- s"callback=${method2Str(mh.method)}" +
+ s"callback=${method2Str(mo.method)}" +
s"]", e)
}
finally
if (flag)
try
- mh.method.setAccessible(false)
+ mo.method.setAccessible(false)
catch {
case e: SecurityException =>
throw new NCE(s"Access or security error in intent
callback [" +
s"mdlId=$mdlId, " +
- s"callback=${method2Str(mh.method)}" +
+ s"callback=${method2Str(mo.method)}" +
s"]", e)
}
}
@@ -1572,8 +1571,8 @@ object NCDeployManager extends NCService {
// Second, scan class for class-level @NCIntent annotations (intent
declarations).
processClass(Class.forName(mdl.meta[String](MDL_META_MODEL_CLASS_KEY)))
- def processMethod(mh: MethodHolder): Unit = {
- val m = mh.method
+ def processMethod(mo: MethodOwner): Unit = {
+ val m = mo.method
val mtdStr = method2Str(m)
@@ -1582,12 +1581,12 @@ object NCDeployManager extends NCService {
throw new NCE(s"The intent cannot be bound to more than
one callback [" +
s"mdlId=$mdlId, " +
s"origin=${mdl.getOrigin}, " +
- s"class=${mh.objClassName}, " +
+ s"class=${mo.objClassName}, " +
s"intentId=${intent.id}" +
s"]")
else {
intentDecls += intent
- intents += (intent -> prepareCallback(mh, mdl, intent))
+ intents += (intent -> prepareCallback(mo, mdl, intent))
}
}
@@ -1601,14 +1600,14 @@ object NCDeployManager extends NCService {
s"id=${intent.id}" +
s"]")
else
- bindIntent(intent, prepareCallback(mh, mdl, intent))
+ bindIntent(intent, prepareCallback(mo, mdl, intent))
// Process intent references from @NCIntentRef annotation.
for (ann <- m.getAnnotationsByType(CLS_INTENT_REF)) {
val refId = ann.value().trim
intentDecls.find(_.id == refId) match {
- case Some(intent) => bindIntent(intent,
prepareCallback(mh, mdl, intent))
+ case Some(intent) => bindIntent(intent,
prepareCallback(mo, mdl, intent))
case None => throw new NCE(
s"""@NCIntentRef("$refId") references unknown intent
ID [""" +
s"mdlId=$mdlId, " +
@@ -1622,7 +1621,7 @@ object NCDeployManager extends NCService {
// Third, scan all methods for intent-callback bindings.
for (m <- getAllMethods(mdl))
- processMethod(MethodHolder(objClassName = null, obj = mdl, method
= m))
+ processMethod(MethodOwner(method = m, objClassName = null, obj =
mdl))
// External references.
def processReferences[T <: Annotation](clazz: Class[T], getReferences:
T => Seq[Class[_]]): Unit = {
@@ -1643,7 +1642,7 @@ object NCDeployManager extends NCService {
refs.foreach(ref => {
processClass(ref)
- getAllMethods(ref).foreach(m =>
processMethod(MethodHolder(objClassName = ref.getName, obj = null, method = m)))
+ getAllMethods(ref).foreach(m =>
processMethod(MethodOwner(method = m, objClassName = ref.getName, obj = null)))
})
}
}