cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Modular Client Side Triggers

ptc-4940367
1-Visitor

Modular Client Side Triggers

I'm using client side triggers, and I'd like to modularize my js code, but I can't figure out how to include another js file from with in the js file called by the trigger. For example:

File : /Custom/library/ValueAdd.js

<code>

function ValueAdd (key)

{

var k = key;

var obj =

{

getKey : function ()

{

return k;

}

};

return obj;

}

</code>

File : /Custom/TriggerFile.js

<code>

load("/library/ValueAdd.js"); // Command used by Rhino to load external files, doesn't seem to work here

var va = new ValueAdd("something");

</code>

When the map a trigger to the TriggerFile.js and the trigger is executed, I get an error "load is not defined". It's possible that I have the path to the file wrong, but it doesn't even get that far. How do I "import" ValueAdd.js into TriggerFile.js?

ACCEPTED SOLUTION

Accepted Solutions

I just started working on triggers and came to the same question that Scott had in mind.

First of all the best way is probably to write your own Java custom code and add it as Jeremy tried to explain.

Yet, including one's custom JavaScript code is feasible,too ...but I don't know if it's recommendable 😉

Here is a kind of "Proof of Concept" which I tested with the breakLockNotification.js script. I added the following code

to the end of that script:

// 1. Load the script you'd like to execute
var content = new String(new java.util.Scanner(new java.io.File("C:/Program Files/MKS/IntegrityServer2009/data/triggers/scripts/samples/testeval.js")).useDelimiter("\\Z").next());


// 2. Evaluate the loaded code

eval(content.toString());

// 3. Execute the freshly evaluated code

multiply_xy();

// 4. Mail me the result of the executed code. Use your own mail address!

environmentBean.sendMail( emailTo, emailTo, "content", z.toString());

The code I evaluated was fairly simple ...and stupid 🙂 testeval.js is:

var x = 10;

var y = 20;

var z = 0;

function multiply_xy()

{

z = x*y;

}

This can be a way to include other JavaScript sources into your trigger scripts, but it comes with all the side-effects that one get's from using "eval()".

E.g. you have to ponder if it is fine for you that the execution of the code is slowed down by the use of "eval()"

For for further readings on "eval()" I'd recommend "eval() isn’t evil, just misunderstood" by Nicholas C. Zakas.

By the way, you can evaluate my "Proof of conecept" without compromising your sacred Integrity server if you download "Rhino" and the
"Bean Scripting Framework". Beforehand you might want to read "Making BSF and Rhino work" .

View solution in original post

7 REPLIES 7

I'm new to these forums. About how long does it usually take to get a response? Am I asking a question that no one has ever encountered before?

Sorry for the delay, Scott. The community is checked a few times a day by support staff, and other users are good about helping out with things they know.

As for your question, please give this a try and let me know how it works for you:

1. Copy the relevant classes into /data/java/classes and the relevant jar files into /data/java/jars -- both on the server's filesystem.

2. Restart the Integrity Server so that the JVM will recognize that they are there.

3. Add the actual function call to your trigger script.

This is typically done as a two-step process. The first step is to instantiate an object of the given class. The second step is to then actually invoke the functions of the object. Sample code is given below for an object class of "Foo" to run its method "getMessage":

var foo = new Packages.Foo();

foo.getMessage();

Best Practices:

1. Ensure that custom packages do not contain functions under the com.mks.* or Packages.mks.* trees. Doing so has a high probability of overriding built-in MKS functions which presents significant stability and security concerns.

2. If custom packages have dependencies on third party libraries, ensure that they are also copied to the appropriate directories -- .lib files should be copied to the /jre/lib folder in your server's install directory, .dll files should be copied to the /jre/bin folder.

3. Your third party java code will most likely consist of .jar files, but if there are also class files, the class files will take precedence over the .jar files in the event that the class names are identical.

Possible errors:

Problem: The error "NoClassDefFound" is given when invoking a function from an imported package.

Possible causes: The jars and/or classes may not have been placed in /data/java/jars and/or /data/java/classes. The JVM may not have been rebooted since the jars and/or classes have been put in place. The package may have unsatisfied dependencies. Thus, the original functions are then calling other functions which do not exist on the Integrity JVM.


That is not helpful, as I'm unclear if that addresses the problem I'm facing. That seems like it would work if I encapsulated my code into a Java class and wanted to call it from my JavaScript trigger, but what I'm trying to do is include another JavaScript file (not a Java class) in my JavaScript trigger.

The documentation for Rhino (which is what I assume is being used behind the scenes to execute the trigger) includes a "load" command, but that isn't working for me.

Am I the only one who is having this problem?

I haven't found any way to load a js file inside a trigger, nor have I been able to chain one trigger into another in linux utility cli style.

We try to make trigger scripts multi-purpose enough so that they can handle multiple similar use cases, but when the requirements call for complex logic, we build a standalone trigger script for it.

There are utility functions that we have in almost every trigger script and keeping them synced across everything when bugs are fixed is definitely a challenge.

What we have found helpful is to standardized our global variables/functions. That makes it easier to compare scripts so that we can apply fixes to global utility functions across scripts more easily.

It is also helpful to separate out the global utility type functions from the trigger script specific functions, again so we can try to keep the utility functions more in line across everything.

I just started working on triggers and came to the same question that Scott had in mind.

First of all the best way is probably to write your own Java custom code and add it as Jeremy tried to explain.

Yet, including one's custom JavaScript code is feasible,too ...but I don't know if it's recommendable 😉

Here is a kind of "Proof of Concept" which I tested with the breakLockNotification.js script. I added the following code

to the end of that script:

// 1. Load the script you'd like to execute
var content = new String(new java.util.Scanner(new java.io.File("C:/Program Files/MKS/IntegrityServer2009/data/triggers/scripts/samples/testeval.js")).useDelimiter("\\Z").next());


// 2. Evaluate the loaded code

eval(content.toString());

// 3. Execute the freshly evaluated code

multiply_xy();

// 4. Mail me the result of the executed code. Use your own mail address!

environmentBean.sendMail( emailTo, emailTo, "content", z.toString());

The code I evaluated was fairly simple ...and stupid 🙂 testeval.js is:

var x = 10;

var y = 20;

var z = 0;

function multiply_xy()

{

z = x*y;

}

This can be a way to include other JavaScript sources into your trigger scripts, but it comes with all the side-effects that one get's from using "eval()".

E.g. you have to ponder if it is fine for you that the execution of the code is slowed down by the use of "eval()"

For for further readings on "eval()" I'd recommend "eval() isn’t evil, just misunderstood" by Nicholas C. Zakas.

By the way, you can evaluate my "Proof of conecept" without compromising your sacred Integrity server if you download "Rhino" and the
"Bean Scripting Framework". Beforehand you might want to read "Making BSF and Rhino work" .

Thank you for this! This is the single most helpful Integrity answer I've recieved EVER (and that includes support tickets I've logged)!

Announcements


Top Tags