Skip to content

Overview

Concepts and Principles

Development

Overview

IDEs

API Explorer

Releases

Release Notes

TORO Integrate

Coder Studio

Coder Cloud

Bug Reports

Search

Customizing Groovy Compilation

In Groovy, classes and scripts can be compiled using to a certain compiler configuration:

Whether you are using groovyc to compile classes or a GroovyShell, for example, to execute scripts, under the hood, a compiler configuration is used. This configuration holds information like the source encoding or the classpath but it can also be used to perform more operations like adding imports by default, applying AST transformations transparently or disabling global AST transformations.

This compiler configuration can be changed and this still holds true even in TORO Integrate's development environment. In fact, TORO Integrate itself uses a compilation customizer by default (defined in <toro-integrate-home>/conf/ESBGroovyCompilerConfig.groovy) to apply the following changes to your Groovy classes and scripts when they're compiled from Coder:

  • Transparently import the following packages:
    • groovy.json.*
    • groovyx.net.http.*
    • io.swagger.annotations.*
    • io.toro.gloop.*
    • io.toro.gloop.annotation.*
    • io.toro.gloop.engine.*
    • io.toro.gloop.exception.*
    • io.toro.gloop.object.*
    • io.toro.gloop.object.property.*
    • io.toro.integrate.core.api.*
    • io.toro.integrate.core.service.annotation.*
    • io.toro.integrate.core.util.*
    • io.toro.integrate.tracker.model.*
    • javax.validation.*
    • javax.validation.constraints.*
    • javax.xml.bind.annotation.*
    • org.springframework.security.access.annotation.*
    • org.springframework.web.bind.annotation.*
    • wslite.soap.*
  • Parses Groovydoc or Javadoc and implicity adds @Documented annotations. The @Documented annotation allows for the to allow for the automatic population of Swagger annotations and generation of Swagger API definitions.

Take the following snippet, for example (switch between the Before and After tabs below):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package endpointServices.groovy

@RequestMapping('SQLOnlinerExamples')
class SQLExamples {

    /**
     * TORO Integrate allows you to easily get a <a href="http://docs.groovy-lang.org/docs/latest/html/api/groovy/sql/Sql.html">Groovy SQL</a>
     * object that's already connected to a TORO Integrate JDBC Connection pool.
     * This script shows how easy it is to expose a RESTful api that uses a database to add a record.
     * @param person The person to add. This example uses a strongly typed object that contains JAXB and validation annotations.
     */
    @ResponseBody
    @RequestMapping(value = 'sql/add-person', method = [RequestMethod.POST])
    APIResponse addPerson(@RequestParam(value = 'dataSource', required = true) DataSource dataSource, 
                          @Valid @RequestBody Person person) {
        // More code...
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package endpointServices.groovy;

import groovy.json.*
import groovyx.net.http.*
import io.swagger.annotations.*
import io.toro.gloop.*
import io.toro.gloop.annotation.*
import io.toro.gloop.engine.*
import io.toro.gloop.exception.*
import io.toro.gloop.object.*
import io.toro.gloop.object.property.*
import io.toro.integrate.core.api.*
import io.toro.integrate.core.service.annotation.*
import io.toro.integrate.core.util.*
import io.toro.integrate.tracker.model.*
import javax.validation.*
import javax.validation.constraints.*
import javax.xml.bind.annotation.*
import org.springframework.security.access.annotation.*
import org.springframework.web.bind.annotation.*
import wslite.soap.*

@RequestMapping({'SQLOnlinerExamples'})
@Documented
@Path('SQLOnlinerExamples')
@Api('SQLOnlinerExamples')
class SQLExamples {

    @ResponseBody
    @RequestMapping(value = 'sql/add-person', method = [RequestMethod.POST])
    @Documented("TORO Integrate allows you to easily get a <a href=\"http://docs.groovy-lang.org/docs/latest/html/api/groovy/sql/Sql.html\">Groovy SQL</a>\nobject that's already connected to a TORO Integrate JDBC Connection pool.\nThis script shows how easy it is to expose a RESTful api that uses a database to add a record.\n@param person The person to add. This example uses a strongly typed object that contains JAXB and validation annotations.")
    @Path('sql/add-person')
    @ApiOperation(notes = "TORO Integrate allows you to easily get a <a href=\"http://docs.groovy-lang.org/docs/latest/html/api/groovy/sql/Sql.html\">Groovy SQL</a> object that's already connected to a TORO Integrate JDBC Connection pool. This script shows how easy it is to expose a RESTful api that uses a database to add a record.", response = APIResponse.class, value = 'sql/add-person')
    @POST
    APIResponse addPerson(@RequestParam(value = 'dataSource', required = true) @ApiParam(name = 'dataSource', value = '', required = true) SQLExamples.DataSource dataSource, 
                          @Valid @RequestBody @ApiParam(name = 'person', value = 'The person to add. This example uses a strongly typed object that contains JAXB and validation annotations.', required = true) Person person) {
        // More code...
    }
}

As you may have observed, TORO Integrate has injected import statements and annotations for your convenience whilst preserving the original elements of your code. TORO Integrate will do as much as it can to help you write better, less verbose code.

Procedure

To change TORO Integrate's compilation process for Groovy files, simply edit the <toro-integrate-home>/conf/ESBGroovyCompilerConfig.groovy file. This configuration file is made known to the actual Groovy compiler via the configscript flag1.

With access to the configuration file, you can do plenty of things, like adding an implicit import to all your Groovy files using code like below:

1
2
3
4
5
6
def importCustomizer = new ImportCustomizer()

// More code...

importCustomizer.addStarImports 'org.foo' // import org.example.*
importCustomizer.addImport('AliasForBar', 'org.foo.Bar') // import org.foo.Bar as AliasForBar

The official Groovy documentation on compilation customizers does a great job at enumerating and discussing behaviors configurable during compilation. There are also plenty of resources available online, like this Groovy Goodness blog post, that you may refer to.

Restart required

It is recommended to restart your TORO Integrate instance after changing ESBGroovyCompilerConfig.groovy to ensure the changes are reflected.


  1. This is entirely dependent on TORO Integrate's internal implementation. As of now, there is no way to use a different filename for the Groovy compiler configuration script.