Groovy for Domain-Specific Languages
PrefaceThe book Groovy for Domain-Specific Languages [Packt Publishing: 3, Amazon.com: 5] written by Fergal Dearle  has been published by Packt Publishing in May 2010. As the title mentions it is all about how to implement domain-specific languages aka DSLs based on the Groovy programming language. With this book the author explicitly targets Java programmers. As can be seen in the separate chapter descriptions below, the book covers Groovy basics, advanced DSL-related language features and script integration issues. Although not explicitly mentioned, this book covers Groovy up to version 1.6, as this can be deducted from some code samples and screenshots.
Chapter SummaryChapter 1 - Introduction to DSL and Groovy The first chapter starts with a general introduction to general purpose programming languages which leads to a brief introduction to specific languages also known as domain-specific languages (DSLs). In addition, the author gives a brief introduction into Groovy and how it relates to the Java platform. A first example of Groovy's XML markup builder gives a practical example on how certain dynamic programming features support building custom DSLs. Chapter 2 - Groovy Quick Start The second chapter covers Groovy from ground up split into two main parts: the first part is the administrative one - it explains where to get Groovy and how to install it on various operating systems and gives an overview about basic commands found in Groovy's binary directory:
groovyc. The second part of this chapter introduces Groovy in about 23 pages and clearly targets programmers with a Java background, not supposed to be an introduction for Groovy newbies without a Java background. Chapter 3 - Groovy Closures The third chapter handles Groovy closures in en detail. It starts with code examples of simple closures, explains closure parameters and how separate methods in Groovy's Closure class relate to them and goes over to currying. At the end of this chapter it introduces
owneras being important implicit variables in the context of Groovy closures. Chapter 4 - Example DSL: GeeTwitter The fourth chapter introduces a custom DSL around Twitter4J  - a library containing Java classes for accessing Twitter's REST API . The first version of GeeTwitter introduces Groovy's optional parenthesis on method calls and shows possible approaches for using that DSL in Groovy scripts: At the end of this chapter, the reader has built a source-complete abstract base class for creating GeeTwitter DSL scripts. Chapter 5 - Power Groovy DSL Features Now it really comes down to the crunch. This chapter can be seen as most important chapter, as it covers a lot of ground about Groovy language features used in real-world frameworks like Grails: Groovy's meta object protocol and meta-programming, the
ExpandoMetaClass, the Builder pattern and Groovy's base builder classes. The chapter gives a good appreciation of what can be achieved by using these powerful features and gives a greater understanding how to use fully-functioning custom DSLs. Chapter 6 - Existing Groovy DSLs This chapter mainly handles Grails-specific DSLs and how they are implemented in the light of using Groovy's power DSL features. In addition, the author gives an overview of some selected TDD frameworks out of Groovy space: GSpec, EasyB and Spock. The overview concentrates on what can be done with these frameworks and not so much of how this is done by the frameworks. Chapter 7 - Building a Builder Hint: This chapter is available as sample chapter . The chapters starts with a detailed discussion of variable resolving strategies in the context of Groovy closures and summarizes the most import meta-programming features for Groovy DSL development. Afterwards, it introduces Groovy's
FactoryBuilderSupport. Chapter 8 - Implementing a Rules DSL The eight chapter shows how to implement a business rules DSL based on the concept of Groovy scripts and bindings. The given example is about an imaginary broadband service provider program that needs business rules for rewarding customer accounts with a scoring system, leading to the Reward DSL. The chapter closes with a paragraph on testing DSLs with Groovy's
GroovyTestCase. Chapter 9 - Integrating It All This chapter handles typical integration issues: using Java classes from Groovy code and vice-vera, how to use Groovy classes within the Spring framework via dependency injection and how to integrate scripts in existing Java applications.