Does groovy provide an include mechanism?

13,825

Solution 1

Since you already mentioned “cross-cutting-concerns” I’d say that you need to intercept your webservice calls AOP style (not an include mechanism).

Grails is completely integrated with Spring framework, so this makes for a good option for exploiting Spring AOP features. Take a look at this chapter from grails official guide: http://grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html and search for word AOP.

Maybe there is a purely groovy way of doing AOP, but I'd go with grails and spring.

Solution 2

Groovy treats its files as objects (think of it as of automatic wrapping). And it makes all .groovy files within the java classpath available as classes. So if you have the file util.groovy, that contains something like this inside:

def static AuxMethod() {
    return "Hello World"
}

To call it from another file you just write:

println util.AuxMethod()

That's it. Again, just make sure that your util.groovy file is in the classpath.

Solution 3

To invoke script u.groovy from the current script, passing along the original arguments to the u.groovy, run

run(new File('u.groovy'), args)

Obviously, you could also send any String arguments that you want to:

run(new File('u.groovy'),
        ['one', new File('two.text').absolutePath] as String[])

Solution 4

Look at the evaluate(File) function:

 Object evaluate(File file) 

http://groovy.codehaus.org/api/groovy/lang/Script.html

Solution 5

I did some research on this for a Domain Specific Language I was creating. There are three possibilities:

  1. Create your classes as inheriting a parent groovy class. Put your shared code in the base class.

  2. Use the ScriptBaseClass see http://groovy.codehaus.org/Embedding+Groovy . This is a class upon which all of your scripts will be created.

  3. Use the import static methods capability. Note that you can do this inside the java container (see http://mrhaki.blogspot.com/2011/06/groovy-goodness-add-imports.html ).

All of these work great. My preference is the ScriptBaseClass. This works best if the common code is Groovy (the ScriptBaseClass must be a groovy class. It can not be a java class.)

Of course, with all of these items, you will still need to actually call the common method in your groovy code. For example:

doCommonStuff();
.
. do the rest of it here
.

That's not too awful, I don't think. Certainly about the same as adding some sort of #include pre-processor statement.

And finally, all of this assumes that you have access to the java program which is calling your Groovy code. If that's not the case, you can still use the static imports. It's just one extra line of code.

import static com.mycompany.mycode.doCommonStuff
doCommonStuf()
.
. do the rest of it here
.
Share:
13,825
Christopher Klewes
Author by

Christopher Klewes

Updated on June 14, 2022

Comments

  • Christopher Klewes
    Christopher Klewes almost 2 years

    We are searching for an include mechanism for groovy scripts to have space for cross-cutting-concerns.

    In my example we have, web service endpoints as groovy scripts and want to log to our web service protocol. for that we use our implicit object (getting from our framework) to create the logging statement.

    But this is boilerplate code if we code this in every web service endpoint.

    We are searching for something like include() in php, that includes other groovy scripts, are there any ideas how to do this?

  • djangofan
    djangofan almost 13 years
    I suspect that only works if your groovy scripts are object oriented classes. With regular functional scripts, probably doesn't work?
  • Blaine
    Blaine over 12 years
    Useless if you don't explain how to make or get a Script object, because that class is abstract.
  • Blaine
    Blaine over 12 years
    The example only works, at least from Windows, if the util.groovy script is renamed with capitalization Util.groovy, and the reference correspondingly changed to 'println Util.AuxMethod()'
  • Blaine
    Blaine over 12 years
    Follow-up to my previous comment. This applies to UNIX also. Does not work until I rename "util" to "Util". Also, most experience Java+Groovy developers would write the method declaration to conform with Java conventions: static def auxMethod. I.e. "static" before the type name and all methods begin with lower-case (see e.g. tons of static methods in java.util.Collections).
  • Jesse Glick
    Jesse Glick about 10 years
    Generally the code that would be calling this is compiled to an instance of an anonymous Script subclass.