Highlighted
Valued Contributor.
Valued Contributor.
195 views

Driver Ecma script logs for debugging.

Jump to solution

Hi ,

I have a logic in driver where it calls Ecma Script function with GivenName and Surname parameter.

The Ecma script function job is to generate Unique email for a user object by using Given Name and Surname.

I noticed that users have same given name and surname are getting provisioned with same email address which is incorrect.

I am just looking for a way to debug my Ecma script by printing some variable values using print function or any other function so that I can see the ecma related logs in driver log.

 

Is it possible to get the logs of ecma script in driver logs and how can i put some alert or print message in y ecma script.

 

Thanks,

CAP

Labels (1)
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Driver Ecma script logs for debugging.

Jump to solution

To debug complex ECMAScript functions, you can run them step-by-step in the Rhino debugger.

Save this as test.js:

 

function getId(first, last) {
    return (first + '.' + last);
}

var result = (getId('Monika', 'Mustermann'));
print(result);

 

and run

java -classpath "js.jar;nxsl.jar;XDS.jar;dirxml_misc.jar;dirxml.jar;xp.jar;RESTUtil.jar;RESTCommon.jar;ldap.jar" com.novell.soa.script.mozilla.javascript.tool
s.debugger.Main test.js

in a directory with all the JARs from lib/dirxml/classes.

This will start the Rhino Debugger where you can set break points, look at local variables and interactivly execute commands:

Rhino DebuggerRhino Debugger

 

This example uses the println() command. Its output goes to standard out, which is the Console window in the Debugger and the driverset log in the engine.

To trace within the scope of a driver, one can use com.novell.nds.dirxml.driver.Trace. To make it work not only in the engine, but also in Designer Simulation and the ECMA Expression Builder as well as the Rhino Debugger you need to wrap it like this:

/**
 * @classdesc Factory wrapping com.novell.nds.dirxml.driver.Trace to avoid Designer complaining
 * about a missing TraceInterface implementation. If no default-tracer is found, output is send
 * to standard out.
 * @class Trace
 * @since 1.2.6
 * @since 1.2.7
 * @example
 * var myTrace = Trace("my-ecma-lib");
 * @Param {string} name Identifier to be used when logging from a specific library.
*/
if (typeof Trace === 'undefined') { // eslint-disable-line no-use-before-define
    var Trace = function (name) {
        var trace = {};
        trace.name = String(name);
        trace.engineTrace = null;

        // initialize
        try {
            trace.engineTrace = Packages.com.novell.nds.dirxml.driver.ThreadGroupLocal.get('default-tracer');
            if (trace.engineTrace == null) {
                trace.engineTrace = Packages.com.novell.nds.dirxml.engine.ThreadVars.get('default-tracer');
            }
            if (trace.engineTrace == null) {
                trace.engineTrace = Packages.com.novell.nds.dirxml.engine.ThreadGroupVars.get('default-tracer');
            }
        } catch (e) {
            java.lang.System.out.println(e);
        }

        /**
         * log message using driver trace or Designer error log
         * @memberof Trace
         * @function trace
         * @since 1.2.6
         * @since 1.2.7
         * @example
         * function testTraceError() {
         *     try {
         *         throw new Error('some error');
         *     } catch(e) {
         *         myTrace.trace(arguments.callee.name + ': ' + e.toString(), 0, e);
         *     }
         * }
         * testTraceError();
         * @Param {string} msg The message to display
         * @Param {int} level The trace level for this message
         * @Param {Error} [error] Exception for logging a stack trace
         */
        trace.trace = function (msg, level, error) {
            // normalize log level
            if (typeof level === 'undefined') {
                level = 0;
            } else {
                level = Number(level);
            }

            // get exception if present
            var exception = null;
            if (error) {
                if (error.rhinoException) {
                    exception = error.rhinoException;
                } else if (error.javaException) {
                    exception = error.javaException;
                }
            }

            // log message
            if (this.engineTrace != null) {
                this.engineTrace.trace(level, (this.name ? this.name + ': ' : '') + msg);
                if (exception) {
                    this.engineTrace.trace(level, getJavaStackTrace(exception));
                }
            } else {
                java.lang.System.out.println((this.name ? this.name + ': ' : '') + msg);
                if (exception) {
                    java.lang.System.out.println(getJavaStackTrace(exception));
                }
            }
        };
        return trace;
    };
}

 

Together with this helper function

/**
 * Return Java Stack Trace as string
 * @since 1.2.4
 * @Param {Exception} e
 * @returns {string} Stack Trace
 */
function getJavaStackTrace(exception) { // eslint-disable-line no-unused-vars
    var str = '';
    if (exception instanceof java.lang.Exception) {
        var sw = new java.io.StringWriter();
        var pw = new java.io.PrintWriter(sw);
        exception.printStackTrace(pw);
        pw.flush();
        str = sw.toString();
        sw.close();
    }
    return str;
}

you'll get some detailed insights including line numbers where an exception occured in your ECMAScript code. For example:

[06/25/20 12:27:12.564]:PAM-CC ST: NOVLPUMCC-JS: cannot build association
[06/25/20 12:27:12.565]:PAM-CC ST: com.novell.soa.script.mozilla.javascript.EcmaError: TypeError: Cannot read property "Rule" from undefined (vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data#467)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4052)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4030)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4063)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4082)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4094)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1520)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34._c_anonymous_13(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data:467)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34._c_RestResponseToXDS_14(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data:549)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:399)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3362)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.xsl.extensions.ECMAScriptFunctionImpl.invoke(ECMAScriptFunctionImpl.java:108)
at com.novell.xml.xpath.FunctionCall.evaluate(FunctionCall.java:90)
at com.novell.xml.dom.DOMEvaluator.evaluate(DOMEvaluator.java:89)
at com.novell.nds.dirxml.engine.rules.RuleDynamicContext.evaluateXPath(RuleDynamicContext.java:176)
at com.novell.nds.dirxml.engine.rules.TokenXPath.expand(TokenXPath.java:72)
at com.novell.nds.dirxml.engine.rules.Arg.evaluate(Arg.java:457)
at com.novell.nds.dirxml.engine.rules.DoSetLocalVariable.apply(DoSetLocalVariable.java:101)
at com.novell.nds.dirxml.engine.rules.ActionSet.apply(ActionSet.java:189)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:310)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:436)
at com.novell.nds.dirxml.engine.Transformer.applyInputTransformation(Transformer.java:333)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:515)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:305)
at com.novell.nds.dirxml.engine.Subscriber$SubscriberAppQueryProcessor.query(Subscriber.java:2424)
at com.novell.nds.dirxml.driver.XdsQueryProcessor.query(XdsQueryProcessor.java:84)
at com.novell.nds.dirxml.engine.rules.TokenQuery.expand(TokenQuery.java:326)
at com.novell.nds.dirxml.engine.rules.Arg.evaluate(Arg.java:457)
at com.novell.nds.dirxml.engine.rules.DoSetLocalVariable.apply(DoSetLocalVariable.java:101)
at com.novell.nds.dirxml.engine.rules.ActionSet.apply(ActionSet.java:189)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:310)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:436)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:421)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:305)
at com.novell.nds.dirxml.engine.Subscriber$ModifyProcessor.process(Subscriber.java:1769)
at com.novell.nds.dirxml.engine.Subscriber.processEvent(Subscriber.java:1206)
at com.novell.nds.dirxml.engine.Subscriber.processEvents(Subscriber.java:1019)
at com.novell.nds.dirxml.engine.Driver.submitTransaction(Driver.java:901)
at com.novell.nds.dirxml.engine.DriverEntry.submitTransaction(DriverEntry.java:1174)
at com.novell.nds.dirxml.engine.DriverEntry.processCachedTransaction(DriverEntry.java:1058)
at com.novell.nds.dirxml.engine.DriverEntry.eventLoop(DriverEntry.java:866)
at com.novell.nds.dirxml.engine.DriverEntry.run(DriverEntry.java:640)
at java.lang.Thread.run(Thread.java:748)

 

--
Norbert

View solution in original post

4 Replies
Highlighted
Honored Contributor.
Honored Contributor.

Re: Driver Ecma script logs for debugging.

Jump to solution

Hi.

You can use this class:

https://www.novell.com/documentation/developer/dirxml/dirxmlbk/ref/javadocs/com/novell/nds/dirxml/driver/Trace.html

So for instance:

var tracer = new Packages.com.novell.nds.dirxml.driver.Trace("MyTrace");

tracer.trace("My log message", 3);

3 indicates the log level of the driver that is needed to show the message in the trace log.

Best regards

Marcus

Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Driver Ecma script logs for debugging.

Jump to solution

To debug complex ECMAScript functions, you can run them step-by-step in the Rhino debugger.

Save this as test.js:

 

function getId(first, last) {
    return (first + '.' + last);
}

var result = (getId('Monika', 'Mustermann'));
print(result);

 

and run

java -classpath "js.jar;nxsl.jar;XDS.jar;dirxml_misc.jar;dirxml.jar;xp.jar;RESTUtil.jar;RESTCommon.jar;ldap.jar" com.novell.soa.script.mozilla.javascript.tool
s.debugger.Main test.js

in a directory with all the JARs from lib/dirxml/classes.

This will start the Rhino Debugger where you can set break points, look at local variables and interactivly execute commands:

Rhino DebuggerRhino Debugger

 

This example uses the println() command. Its output goes to standard out, which is the Console window in the Debugger and the driverset log in the engine.

To trace within the scope of a driver, one can use com.novell.nds.dirxml.driver.Trace. To make it work not only in the engine, but also in Designer Simulation and the ECMA Expression Builder as well as the Rhino Debugger you need to wrap it like this:

/**
 * @classdesc Factory wrapping com.novell.nds.dirxml.driver.Trace to avoid Designer complaining
 * about a missing TraceInterface implementation. If no default-tracer is found, output is send
 * to standard out.
 * @class Trace
 * @since 1.2.6
 * @since 1.2.7
 * @example
 * var myTrace = Trace("my-ecma-lib");
 * @Param {string} name Identifier to be used when logging from a specific library.
*/
if (typeof Trace === 'undefined') { // eslint-disable-line no-use-before-define
    var Trace = function (name) {
        var trace = {};
        trace.name = String(name);
        trace.engineTrace = null;

        // initialize
        try {
            trace.engineTrace = Packages.com.novell.nds.dirxml.driver.ThreadGroupLocal.get('default-tracer');
            if (trace.engineTrace == null) {
                trace.engineTrace = Packages.com.novell.nds.dirxml.engine.ThreadVars.get('default-tracer');
            }
            if (trace.engineTrace == null) {
                trace.engineTrace = Packages.com.novell.nds.dirxml.engine.ThreadGroupVars.get('default-tracer');
            }
        } catch (e) {
            java.lang.System.out.println(e);
        }

        /**
         * log message using driver trace or Designer error log
         * @memberof Trace
         * @function trace
         * @since 1.2.6
         * @since 1.2.7
         * @example
         * function testTraceError() {
         *     try {
         *         throw new Error('some error');
         *     } catch(e) {
         *         myTrace.trace(arguments.callee.name + ': ' + e.toString(), 0, e);
         *     }
         * }
         * testTraceError();
         * @Param {string} msg The message to display
         * @Param {int} level The trace level for this message
         * @Param {Error} [error] Exception for logging a stack trace
         */
        trace.trace = function (msg, level, error) {
            // normalize log level
            if (typeof level === 'undefined') {
                level = 0;
            } else {
                level = Number(level);
            }

            // get exception if present
            var exception = null;
            if (error) {
                if (error.rhinoException) {
                    exception = error.rhinoException;
                } else if (error.javaException) {
                    exception = error.javaException;
                }
            }

            // log message
            if (this.engineTrace != null) {
                this.engineTrace.trace(level, (this.name ? this.name + ': ' : '') + msg);
                if (exception) {
                    this.engineTrace.trace(level, getJavaStackTrace(exception));
                }
            } else {
                java.lang.System.out.println((this.name ? this.name + ': ' : '') + msg);
                if (exception) {
                    java.lang.System.out.println(getJavaStackTrace(exception));
                }
            }
        };
        return trace;
    };
}

 

Together with this helper function

/**
 * Return Java Stack Trace as string
 * @since 1.2.4
 * @Param {Exception} e
 * @returns {string} Stack Trace
 */
function getJavaStackTrace(exception) { // eslint-disable-line no-unused-vars
    var str = '';
    if (exception instanceof java.lang.Exception) {
        var sw = new java.io.StringWriter();
        var pw = new java.io.PrintWriter(sw);
        exception.printStackTrace(pw);
        pw.flush();
        str = sw.toString();
        sw.close();
    }
    return str;
}

you'll get some detailed insights including line numbers where an exception occured in your ECMAScript code. For example:

[06/25/20 12:27:12.564]:PAM-CC ST: NOVLPUMCC-JS: cannot build association
[06/25/20 12:27:12.565]:PAM-CC ST: com.novell.soa.script.mozilla.javascript.EcmaError: TypeError: Cannot read property "Rule" from undefined (vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data#467)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4052)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4030)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4063)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4082)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4094)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1520)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34._c_anonymous_13(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data:467)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34._c_RestResponseToXDS_14(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data:549)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:399)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3362)
at com.novell.soa.script.mozilla.javascript.gen.vnd_nds_stream___IDM_system_driverset1_PAM_CC_NOVLPUMCC_ECMAScript_DirXML_Data_34.call(vnd.nds.stream://IDM/system/driverset1/PAM-CC/NOVLPUMCC-ECMAScript#DirXML-Data)
at com.novell.xsl.extensions.ECMAScriptFunctionImpl.invoke(ECMAScriptFunctionImpl.java:108)
at com.novell.xml.xpath.FunctionCall.evaluate(FunctionCall.java:90)
at com.novell.xml.dom.DOMEvaluator.evaluate(DOMEvaluator.java:89)
at com.novell.nds.dirxml.engine.rules.RuleDynamicContext.evaluateXPath(RuleDynamicContext.java:176)
at com.novell.nds.dirxml.engine.rules.TokenXPath.expand(TokenXPath.java:72)
at com.novell.nds.dirxml.engine.rules.Arg.evaluate(Arg.java:457)
at com.novell.nds.dirxml.engine.rules.DoSetLocalVariable.apply(DoSetLocalVariable.java:101)
at com.novell.nds.dirxml.engine.rules.ActionSet.apply(ActionSet.java:189)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:310)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:436)
at com.novell.nds.dirxml.engine.Transformer.applyInputTransformation(Transformer.java:333)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:515)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:305)
at com.novell.nds.dirxml.engine.Subscriber$SubscriberAppQueryProcessor.query(Subscriber.java:2424)
at com.novell.nds.dirxml.driver.XdsQueryProcessor.query(XdsQueryProcessor.java:84)
at com.novell.nds.dirxml.engine.rules.TokenQuery.expand(TokenQuery.java:326)
at com.novell.nds.dirxml.engine.rules.Arg.evaluate(Arg.java:457)
at com.novell.nds.dirxml.engine.rules.DoSetLocalVariable.apply(DoSetLocalVariable.java:101)
at com.novell.nds.dirxml.engine.rules.ActionSet.apply(ActionSet.java:189)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:310)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:436)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:421)
at com.novell.nds.dirxml.engine.Subscriber.execute(Subscriber.java:305)
at com.novell.nds.dirxml.engine.Subscriber$ModifyProcessor.process(Subscriber.java:1769)
at com.novell.nds.dirxml.engine.Subscriber.processEvent(Subscriber.java:1206)
at com.novell.nds.dirxml.engine.Subscriber.processEvents(Subscriber.java:1019)
at com.novell.nds.dirxml.engine.Driver.submitTransaction(Driver.java:901)
at com.novell.nds.dirxml.engine.DriverEntry.submitTransaction(DriverEntry.java:1174)
at com.novell.nds.dirxml.engine.DriverEntry.processCachedTransaction(DriverEntry.java:1058)
at com.novell.nds.dirxml.engine.DriverEntry.eventLoop(DriverEntry.java:866)
at com.novell.nds.dirxml.engine.DriverEntry.run(DriverEntry.java:640)
at java.lang.Thread.run(Thread.java:748)

 

--
Norbert

View solution in original post

Highlighted
Knowledge Partner
Knowledge Partner

Re: Driver Ecma script logs for debugging.

Jump to solution
Great article, Norbert!
I have to test this debug option
Highlighted
Respected Contributor.
Respected Contributor.

Re: Driver Ecma script logs for debugging.

Jump to solution
I build a logging function like the trace messages, which dumps the json, array and other object as string and log in either idapps.log for user app and in the driver trace log. Based on the standaard logging.

Did not yet try the rhino debug might be an interesting addition.

Michiel Los
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.