Grabbing output from an xpath runtime script in null driver

Hi there,

We're running a script from the null driver. (Thanks to Steven Tharp!) This script produces a REST request, and I'm currently writing the response to a file. But I would like to get it to appear in the null driver trace. Is there any way to assign the script output to a variable you can write to the trace? I know the scripting driver is smarter about these things, and I am working in parallel to try to get that going, but (of course!) we're under a time crunch, and it is currently working with null. So... any ideas? and java junkies out there? I tried searching for xpath and runtime and saw some java.lang bits that looked like it would get the result (being, I assume, either zero - worked, or non-zero, didn't work), but that's not super helpful at this time.

Thanks for any advice -
Karla B
  • Anything called via Java should have the possibility to send output back
    to java, so assuming a variable is assigned to it then capturing it should
    be possible.

    Without seeing how the call is made, and if the script even generates
    output, that's all i can provide other than generic Java docs which may
    match what you are doing:

    https://stackoverflow.com/questions/1410741/how-to-invoke-a-linux-shell-command-from-java
    #how to call a function from Java, which I presume will look similar to
    your XPath stuff

    https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html #see the
    getOutputStream() method to get the object that will then have your
    standard output (STDOUT) data. There is also one for standard error.

    In your case if your script is writing to a file and NOT to STDOUT, then
    you'd probably just need to have some other xpath command read that file
    and get its contents to then be traced. There is a tracer ECMAscript
    method that you should be able to use once you have the data in the engine
    somewhere (vs. in a filesystem).

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below.

    If you want to send me a private message, please let me know in the
    forum as I do not use the web interface often.
  • kborecky wrote:

    > We're running a script from the null driver.


    What kind of script and how do you run it? If it's an ECMA called from xpath
    you can trace directly by calling:

    var tracer = new Packages.com.novell.nds.dirxml.driver.Trace("myEcmaScript");
    tracer.trace(message, level);

    --
    http://www.is4it.de/en/solution/identity-access-management/

    (If you find this post helpful, please click on the star below.)
  • Thanks, everyone! Aaron found the solution. After you define your command line string, you use "execString" to grab the output (before you actually run the command, of course.) This will get whatever output the script produces (echo, print, etc.)

    Steps:

    1. Add the necessary namespaces:

    <namespace-def name="es" value="">www.novell.com/.../>
    <namespace-def name="js" value="">www.novell.com/.../>
    <namespace-def name="cmd" value="">www.novell.com/.../>
    <namespace-def name="runtime" value="">www.novell.com/.../>

    or, using the Edit Namespaces wizard:

    [table="width: 250"]

    Prefix

    URI

    Java ext?


    [tr]
    es

    www.novell.com/.../ecmascript

    [td][/td]
    [/tr]
    [tr]
    js

    www.novell.com/.../ecmascript

    [td][/td]
    [/tr]

    cmd

    com.novell.nds.dirxml.driver.XdsCommandProcessor

    yes




    runtime

    java.lang.Runtime

    yes



    [/table]

    2. Define local variables to establish the runtime instance and the command line you want to run.

    Runtime instance:

    <do-set-local-variable name="runtime-instance" scope="policy">
    [INDENT] <arg-object>
    [/INDENT] [INDENT][INDENT]<token-xpath expression="runtime:getRuntime()"/>[/INDENT][/INDENT]
    [INDENT]</arg-object>[/INDENT]
    </do-set-local-variable>

    Command line:

    <do-set-local-variable name="cmdLine" scope="policy">
    [INDENT]<arg-string>[/INDENT]
    [INDENT][INDENT]<token-text xml:space="preserve">/your/path/here/myscript.sh</token-text>[/INDENT][/INDENT]
    [INDENT][INDENT][INDENT]<token-text xml:space="preserve"> </token-text>[/INDENT][/INDENT][/INDENT]
    [INDENT][INDENT][INDENT]<token-attr name="workforceID"/>[/INDENT][/INDENT][/INDENT]
    [INDENT][INDENT][INDENT]<token-text xml:space="preserve"> </token-text>[/INDENT][/INDENT][/INDENT]
    [INDENT][INDENT][INDENT]<token-attr name="CN"/>[/INDENT][/INDENT][/INDENT]
    [INDENT]</arg-string>[/INDENT]
    </do-set-local-variable>

    This example defines this command line (with arguments) "/your/path/here/myscript.sh workforceID CN" Don't forget to add the spaces between arguments, if you're using them.

    If you want, you can echo the resulting command line back to the trace file:

    <do-trace-message>
    [INDENT]<arg-string>[/INDENT]
    [INDENT][INDENT]<token-local-variable name="cmdLine"/>[/INDENT][/INDENT]
    [INDENT]</arg-string>[/INDENT]
    </do-trace-message>

    3. Define the local variable that prepares to grab the output of the command:

    <do-set-local-variable name="result" scope="policy">
    [INDENT]<arg-string>[/INDENT]
    [INDENT][INDENT]<token-xpath expression="es:execString($cmdLine)"/>[/INDENT][/INDENT]
    [INDENT]</arg-string>[/INDENT]
    </do-set-local-variable>

    Note the use of the command line variable defined in step 2.

    4. Define a local variable that uses the runtime:exec command to execute the script:

    <do-set-local-variable name="process" scope="policy">
    [INDENT]<arg-object>[/INDENT]
    [INDENT][INDENT]<token-xpath expression="runtime:exec($runtime-instance, $cmdLine)"/>[/INDENT][/INDENT]
    [INDENT]</arg-object>[/INDENT]
    </do-set-local-variable>

    5. (Optional) Print the contents of the result variable to the trace file:

    <do-trace-message level="1">
    [INDENT]<arg-string>[/INDENT]
    [INDENT][INDENT]<token-text xml:space="preserve">Results: </token-text>[/INDENT][/INDENT]
    [INDENT][INDENT]<token-local-variable name="result"/>[/INDENT][/INDENT]
    [INDENT]</arg-string>[/INDENT]
    </do-trace-message>

    I hope this helps someone else! Kudos to Aaron and Steven Tharp (for the original command line wizardry).