23,50 €
You may already be aware that the Java virtual machine runs on everything from the largest mainframe to the smallest microchip and supports almost every conceivable application. What you may not realize is that to develop software in some of these scenarios requires a targeted, single purpose language, a Domain Specific Language. The popular scripting language Groovy can be used to create a Domain Specific Language that can run directly on the JVM alongside regular Java code. This comprehensive tutorial will take you through the design and development of Groovy-based Domain Specific Languages. It is a complete guide to the development of several mini-DSLs with a lot of easy-to-understand examples. This book will help you to gain all of the skills needed to develop your own Groovy-based DSLs, as it guides you from the basics through to the more complex meta-programming features of Groovy. The focus is on how the Groovy language can be used to construct domain-specific mini-languages. Practical examples are used throughout to de-mystify the seemingly complex language features and to show how they can be used to create simple and elegant DSLs. The examples include a quick and simple Groovy DSL to interface with Twitter. The book concludes with a chapter focusing on integrating Groovy-based DSLs in such a way that the scripts can be readily incorporated into the readers' own Java applications. The overall goal of this book is to take Java developers through the skills and knowledge they need to start building effective Groovy-based DSLs to integrate into their own applications.
This practical guide will teach you how to build Domain Specific Languages in Groovy that integrate seamlessly with your Java applications
This book is a practical tutorial, walking the reader through examples of building DSLs with Groovy covering meta-programming with Groovy. Some complex concepts are covered in the book but we go through these in a clear and methodically way so that readers will gain a good working knowledge of the techniques involved.
This book is for Java software developers who have an interest in building domain scripting into their Java applications. No knowledge of Groovy is required, although it will be helpful. The book does not teach Groovy, but quickly introduces the basic ideas of Groovy. An experienced Java developer should have no problems with these and move quickly on to the more involved aspects of creating DSLs with Groovy. No experience of creating a DSL is required. The book should also be useful for experienced Groovy developers who have so far only used Groovy DSLs such as Groovy builders and would like to start building their own Groovy-based DSLs.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 387
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2010
Production Reference: 1210510
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847196-90-3
www.packtpub.com
Cover Image by Karl Moore (<[email protected]>)
Author
Fergal Dearle
Reviewer
Chanwit Kaewkasi
Guillaume Laforge
Robert F. Castellow
Acquisition Editor
Douglas Paterson
Development Editor
Rakesh Sejwal
Technical Editor
Shadab Khan
Copy Editor
Lakshmi Menon
Indexer
Rekha Nair
Monica Ajmera
Editorial Team Leader
Akshara Aware
Project Team Leader
Lata Basantani
Project Coordinator
Shubhanjan Chatterjee
Proofreader
Dirk Manuel
Graphics
Geetanjali Sawant
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
Fergal Dearle is a seasoned software development professional with 23 years of experience in software product development across a wide variety of technologies. He is currently the principal consultant with his own software development consulting company, Dearle Technologies Ltd., engaged in design, development, and architecture for new software products for client companies. In the past, Fergal has worked in lead architect and developer roles for Candle Corporation on the OMEGAMON product, which is now part of IBM's Tivoli product suite, and as development manager for the Unix implementations of Lotus 1-2-3. In the early 1990s, Fergal led the team at Glockenspiel that developed CommonView, the first object-oriented UI framework for Microsoft Windows. The team was awarded one of the first ever Jolt Productivity Awards by Dr Dobbs Journal.
I would like to thank the team at Packt for their patience and forbearance during the writing of this book. With special thanks to Doug, Rajashree, Rakesh, and Shubhanjan for keeping me on the straight and narrow. I would also like to thank David Petherick, "The Digital Biographer" for introducing me to the wonders of cask strength malt Scotch whiskey and for his words of wisdom and encouragement both of which gave me the courage to take on this project in the first place. Finally, but by no means least, I want to thank my girls Caroline, Eabha, Nessa, and Sadhbh who put up with my long absences during the writing, without whose love and tolerance I would never have completed this book.
Chanwit Kaewkasi is a lecturer at the School of Computer Engineering, Suranaree University, Thailand, where he received his BEng (First-class honours) in Computer Engineering. He received MEng in computer engineering from Chulalongkorn University, Thailand. Currently, he is doing a PhD in computer science at the University of Manchester, United Kingdom. His research involves an aspect-oriented approach to performance improvement of a dynamic language.
Chanwit has been an active contributor to the Groovy and Grails community since 2006. He was the very first committer of the Grails plugin project, where he maintains two of them—XFire and ZK plugin for Grails. In 2008, he joined the Google Summer of Code program for the development of a just-in-time compiler for Groovy.
Guillaume Laforge is the project lead of Groovy, the highly popular and successful dynamic language for the JVM. He co-authored Manning's bestseller "Groovy in Action" with Dierk König, and is working for SpringSource (a division of VMWare) where he's working full time on cool and Groovy stuff. You can meet Guillaume at conferences around the world where he evangelizes the Groovy dynamic language, Domain-Specific Languages in Groovy, and the agile Grails web framework.
Rob F. Castellow is the president of PAC Enterprises LLC, a contract and development company responsible for providing quality professional services. He has provided services in the development of several J2EE-based projects for large corporations in the telecommunications and financial services sectors.
Rob graduated in 1998 with a Masters in Electrical Engineering from the Georgia Institute of Technology, and began his career developing embedded systems. Rob soon found that all the excitement was in developing enterprise systems and has been working on J2EE-based applications ever since.
Rob is an enthusiast of new technologies. When he is not proofreading books on Groovy DSLs, he can be found developing Grails applications, attending user groups, reading books, managing/developing several websites, or playing with his two sons.
The Java virtual machine runs on everything from the largest mainframe to the smallest microchip and supports every conceivable application. But Java is a complex and sometimes arcane language to develop with. Groovy allows us to build targeted single-purpose mini languages, which can run directly on the JVM alongside regular Java code.
This book provides a comprehensive tutorial on designing and developing mini Groovy-based Domain-Specific Languages. It is a complete guide to the development of several mini DSLs with a lot of easy-to-understand examples. This book will help you to gain all of the skills needed to develop your own Groovy-based DSLs.
Groovy for Domain-Specific Languages guides the reader from the basics through to the more complex meta-programming features of Groovy. The focus is on how the Groovy language can be used to construct domain-specific mini languages. Practical examples are used throughout to demystify these seemingly complex language features and to show how they can be used to create simple and elegant DSLs. The examples include a quick and simple Groovy DSL to interface with Twitter.
The book concludes with a chapter focusing on integrating Groovy-based DSL in such a way that the scripts can be readily incorporated into the reader's own Java applications. The overall goal of this book is to take Java developers through the skills and knowledge they need to start building effective Groovy-based DSLs to integrate into their own applications.
Chapter 1, Introduction to DSL and Groovy, discusses how DSLs can be used in place of general-purpose languages to represent different parts of a system. You will see how adding DSLs to your applications can open up the development process to other stakeholders in the development process. You'll also see how, in extreme cases, the stakeholders themselves can even become co-developers of the system by using DSLs that let them represent their domain expertise in code.
Chapter 2, Groovy Quick Start, covers a whistle-stop tour of the Groovy language. It also touches on most of the significant features of the language as a part of this tour.
Chapter 3, Groovy Closures, covers closures in some depth. It covers all of the important aspects of working with closures. You can explore the various ways to call a closure and the means of passing parameters. You will see how to pass closures as parameters to methods, and how this construct can allow us to add mini DSL syntax to our code.
Chapter 4, Example DSL: GeeTwitter, focuses on how we can start with an existing Java-based API and evolve it into a simple user-friendly DSL that can be used by almost anybody. You'll learn the importance of removing boilerplate code and how you can structure our DSL in such a way that the boilerplate is invisible to our DSL users.
Chapter 5, Power Groovy DSL Features, covers all of the important features of the Groovy language, and looks in depth at how some of these features can be applied to developing DSLs.
Chapter 6, Existing Groovy DSLs, discusses some existing Groovy DSLs that are in current use and are free to download.
Chapter 7, Building a Builder, explains how Groovy provides two useful support classes that make it much simpler to implement our own builders than if we use the MOP. You'll see how to use BuilderSupport and FactoryBuilderSupport to create our own builder classes.
Chapter 8, Implementing a Rules DSL, takes a look at Groovy bindings to see how they can be used in our DSL scripts. By placing closures strategically in the binding, you can emulate named blocks of code. You can also provide built-in methods and other shorthand by including closures and named Boolean values in the binding. These techniques can be used to a great effect to write DSL scripts that can be read and understood by stakeholders outside of the programming audience.
Chapter 9, Integrating it all, covers the many different ways in which you can integrate Groovy code into Java. You'll explore the issues around tightly integrating the two languages at compile time. You'll see how this can lead to dependency issues arising when Java code references Groovy classes and vice versa. You'll take a look at how you can use dependency injection frameworks like Spring to resolve some of these issues.
This book is for Java software developers who have an interest in building domain scripting into their Java applications. No knowledge of Groovy is required, though it will be helpful. The book will not teach Groovy, but will quickly introduce the basic ideas of Groovy. An experienced Java developer should have no problems with these and can move quickly onto the more evolved aspects of creating DSLs with Groovy.
No experience of creating a DSL is required. The book should also be useful for experienced Groovy developers who have so far only used Groovy DSLs such as Groovy builders and would like to start building their own Groovy-based DSLs.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "We can include other contexts through the use of the include directive."
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code for the book
Visit https://www.packtpub.com//sites/default/files/downloads/6903_Code.zip to directly download the example code.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
