How To
ForgeRock Identity Platform
Does not apply to Identity Cloud

How do I call one Groovy script from another in IDM (All versions)?

Last updated Mar 17, 2021

The purpose of this article is to provide assistance in calling a custom Groovy script from another Groovy script in IDM. Groovy scripts can be used to customize your deployment.


Background information

Groovy scripts can be used to customize your deployment in the following situations:

  • Health Check Scripts
  • Custom Endpoints
  • Groovy Toolkit and Scripted SQL Connectors
  • Synchronization Customizations Logic
  • Audit Data Filters

This article contains two suggestions for keeping your Groovy scripts modularized and organized. If you have high load, you may hit the following Groovy performance issue and should use the suggested workaround instead of the examples in this article. 

Groovy performance issue

There is a known performance issue in the current Groovy library (GROOVY-6655), which has not yet been fixed. If you encounter this issue, you will see the following error:

Exception in thread "DefaultQuartzScheduler_QuartzSchedulerThread" java.lang.OutOfMemoryError: Java heap space [111] Feb 25, 2021 11:31:02.357 AM org.forgerock.openidm.script.registry.ScriptRegistryImpl$ScriptImpl eval SEVERE: Script invocation error java.lang.OutOfMemoryError: Java heap space

Until Groovy fix this issue and release an updated library, the suggested workaround is to write a Groovy library and put that jar in the /path/to/lib directory. 

Calling a Groovy script from within the same source location

The source location for your custom Groovy script is configured in the conf/script.json file (see project-script location). The benefit of calling a Groovy script this way is that you will not need to restart IDM if you make changes to the referenced script. Any changes should automatically get recompiled by the Groovy interpreter.

The following example shows how you can create a custom Groovy script and then call it from within the same source location:

  1. Copy the endpoint-echo.json file from the following sample location to the /path/to/idm/conf directory depending on your version:
    • IDM 5.5 and later: copy the file from the /path/to/idm/samples/example-configurations/custom-endpoint/conf/ directory.
    • IDM 5: copy the file from the /path/to/idm/samples/customendpoint/conf/ directory.
  2. Copy the echo.groovy file from the following sample location to the /path/to/idm/script directory depending on your version:
    • IDM 5.5 and later: copy the file from the /path/to/idm/samples/example-configurations/custom-endpoint/script/ directory.
    • IDM 5: copy the file from the /path/to/idm/samples/customendpoint/script/ directory
  3. Create your Groovy script in the /path/to/idm/script directory. This example creates a GroovyTest.groovy file with the following content: import org.slf4j.*; class GroovyTest { def logger = LoggerFactory.getLogger('logger');     void printTest(String msg) {          logger.info(msg);     } }
  4. Edit the echo.groovy file and add the following lines immediately after the import statements at the top of the file to call your Groovy script: File sourceFile = new File("script/GroovyTest.groovy"); Class groovyClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile); GroovyObject myObject = (GroovyObject) groovyClass.newInstance(); myObject.printTest("this is from my project script directory");
  5. Test your Groovy script by invoking it using a curl command such as: $ curl -u openidm-admin:openidm-admin "http://localhost:8080/openidm/endpoint/echo?_prettyPrint=true"
  6. Check the log file to verify that the script has run as expected; you should see the following entry for this example: INFO: this is from my project script directory

Calling a Groovy script from the Library location

Calling another Groovy script from the Library location (located in the conf/script.json file as groovy.classpath) is a simple matter of adding the import statement at the top of your calling script:

import MyGroovyTest

You can call a Groovy script from the Library location as follows:

  1. Create your Groovy script in the /path/to/idm/script directory. This example creates a GroovyTest.groovy file with the following content: import org.slf4j.*; class MyGroovyTest { def logger = LoggerFactory.getLogger('logger');     void printTest(String msg) {          logger.info(msg);     } }
  2. Add the following to your calling script to invoke your Groovy script: import MyGroovyTest MyGroovyTest mgt = new MyGroovyTest(); mgt.printTest("This is my library location");
  3. Restart the IDM instance to pick up the changes you made to your calling script.

The downside to doing it this way is that any changes made to your library will not get picked up by the calling script until you restart IDM. You should only place libraries in the groovy.classpath location if you do not expect them to change often.

See Scripting Guide › Script Configuration for further information about the groovy.classpath.

See Also

How do I add logging to Groovy scripts in IDM (All versions)?

How do I invoke a jar file from a Groovy script in IDM (All versions)?

Scripting Guide › Script Configuration

Connectors Guide › Groovy Connector Toolkit

Connector Developer's Guide › Writing Scripted Connectors With the Groovy Connector Toolkit

Scripting Guide › Custom Endpoint Configuration File

Related Training

N/A

Related Issue Tracker IDs

OPENIDM-16168 (Update documentation for importing groovy scripts)


Copyright and Trademarks Copyright © 2021 ForgeRock, all rights reserved.