User-defined Functions
You can create your own function by coding in Java or calling an existing Java library.
- Go to Specifications -> Function -> click Add button. Enter function name. We suggest to follow the naming convention used for prebuilt functions:
/<class>/<command type: check,get,format>/<command>, for example,/string/format/collapseWhitespace. - Click on the newly created function to see its properties.
- Upload thrid-party libraries that you are going to use into
<infolink home>/libs. All such libaries are added to class path when the function is compiled and executed. - In the Import textarea, enter a list of import statements (using Java syntax) to import libraries you are going to use in the function implementation. It can be standard JRE libraries and third-party libaries uplaoded by you.
- In the function implementation editor, enter Java statements that implement the function. The statements will be wrapped in the class implementation provided below. It means that:
- the last statment must be
returnreturning a string value; - you can use
out.print(String)andout.println(String)to print something to the output, for example, `out.println(“something”); - you can use
ilparameter to refer to the execution context that supports the following methods:- getVariableValue(String name) - returns the value of a variable that can be application variable or variable of the current execution context (eg scenario parameter);
- setContextVariable(String name, String value) - set a variable in the current execution context.
paramsJson- is JSONObject that contains extra parameters passed on function execution.
- the last statment must be
How the function implementation is wrapped for compilation:
package javafunctions;
import org.json.JSONObject;
<content of the import textarea>
public class "+className+" extends com.paloaltodatabases.infolink.common.java.JavaFunctionImplementation {
public String execute(com.paloaltodatabases.infolink.common.java.JavaScriptContext il, String value, JSONObject paramsJson) throws Exception {
<content of implementation editor>
}
} JavaFunctionImplementation class is defined as follows:
public class JavaFunctionImplementation implements JavaFunctionInterface {
public String guid = UtilLib.genGUID();
public JavaOut out = new JavaOut();
public String execute(JavaScriptContext il, String value, JSONObject paramsJson) throws Exception {
throw new ApplicationException("JavaFunctionImplementation.execute: Not implemented");
}
public void onCatchingOutput() throws Exception {
out.isOn = true;
}
public void offCatchingOutput() throws Exception {
out.isOn = false;
}
public String getOutput() throws Exception {
return out.output.toString();
}
public String getGUID() throws Exception {
return guid;
}
}Bofore testing/using the function you must compile it by clicking on Compile button. You can check compilation errors in the output window under the implementation editor.
You can test the function by entering test value (without quotes, just text) into the Test with value field and clicking the button. Enter exra parameter in JSON format into Parameters field if needed.
Example of creating a user-defined function that uses a third-party Java library
- Download strman-0.4.0.jar from here and put it into
<infolink home>/libs. - Create
/string/format/collapseWhitespacefunction as described above. - In the Import textarea copy the following:
import strman.Strman; - In the implementation editor copy the following:
out.println("print something for debugging"); return Strman.collapseWhitespace(value); - Click Compile button and check that there are no errors in the output windows under the implementation editor.
- Now the function is ready to be used from the operations. You can test it by entering (without quotes) in Test with value field: “space boy”. Click Test with value button and check the result in the outpub window: “space boy”
Last updated on