Groovy Programming
An Introduction for Java Developers
Kenneth Barclay
John Savage
Morgan Kaufmann
To Irene -K.B.
To Salwa -J.S.
Publisher Denise E. M. Penrose Publishing Services Manager George Morrison Senior Editor Tim Cox Assistant Editor Mary E. James Project Manager Marilyn E. Rash Cover Design Chen Design Composition and Illustrations SPi Production Services SPi Interior printer Maple-Vail Book Manufacturing Group Cover printer Phoenix Color Corp.
Morgan Kaufmann Publishers is an imprint of Elsevier. 500 Sansome Street, Suite 400, San Francisco, CA 94111
This book is printed on acid-free paper.
Designations used by companies to distinguish their products are often claimed as trademarks or registered trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim, the product names appear in initial capital or all capital letters. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any meanselectronic, mechanical, photocopying, scanning, or otherwisewithout prior written permission of the publisher.
Permissions may be sought directly from Elseviers Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, E-mail: ), by selecting Support & Contact then Copyright and Permission and then Obtaining Permissions.
Library of Congress Cataloging-in-Publication Data Barclay, Kenneth A., 1947- Groovy programming : an introduction for Java developers / Kenneth Barclay, John Savage. p. cm.
Includes bibliographical references and index.
ISBN-13: 978-0-12-372507-3 (alk. paper)
ISBN-10: 0-12-372507-0 (alk. paper)
1. Java (Computer program language) I. Savage, W. J. (W. John) II. Title. QA76.73.J38B358 2006 005. 133-dc22 2006036352
For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com or www.books.elsevier.com
Printed in the United States of America
07 08 09 10 5 4 3 2 1
Working together to grow libraries in developing countries www.elsevier.com www.bookaid.org www.sabre.org
FOREWORD
Scripting languages are not new. Primarily, they are used on Linux and UNIX machines for things such as shell scripting tasks that automate software installations, platform customizations, scientific application prototyping with Python, and one-shot command-line jobs with bash scripts. Languages such as PHP have also been widely used for developing high-volume websites and scripting languages have been found to be suitable for serious business applications.
Usually, scripting languages are platforms on their own and do not necessarily interact with others. Although bindings may exist for bridging with other systems, the integration is not always intuitive or natural. Groovy seeks to fill that gap, bringing a genuine innovative language that can interact natively with Java by living on the same virtual machine.
Groovy brings a concise and expressive Java-like syntax to ease the learning curve for Java developers. But beyond the syntax, Groovy also provides two key aspects to the landscape by providing wrapper APIs around common JDK Application Programming Interfaces. It simplifies the implementation of common tasks and integrates meta-programming capabilities to develop powerful new language constructs or to easily manipulate existing ones.
Groovy can be used in various situations: as a shell scripting language to do data crunching and file manipulation tasks or to experiment with new APIs. It can also be adapted for creating full-blown small- to mid-size applications to leverage the wealth of Java libraries and components. Moreover, another important use is to marry Java and Groovy by embedding Groovy inside Java or Java EE applications. This can help write and externalize often-changing business rules or bring programmatic configuration to an application infrastructure.
Although the first two uses are quite common, I believe the embedded- use case is the most appealing and promising. Currently, developers have been using template engines for customizing and factoring out views, or they have used business rules engines to externalize some logic. Beyond the limited functionality set, however, programmers are often given little support. Fortunately, platform-hosted scripting languages such as Groovy can help bridge this functionality gap. This is evident with the success of Groovy and its childGrailsa versatile model-view-controller (MVC) Web framework. Sun, too, believes in this alternative way of adding dynamicity to applications, by including a new Java Specification Request in Java 6: the javax.script.* APIs allow seamless embedding of any scripting or dynamic language into Java applications with a coherent programming API.
Scripting languages have evolved to a point of maturity where they meet the limitations of the standardized mainstream platforms. When those languages and platforms come across, thats when magic happens, and we can celebrate a marriage made in Heaven.
Ken Barclay and John Savage are respected teachers and are well positioned to introduce Groovy to both experienced developers and novices. They demonstrate in a clear manner how Groovy augments the Java platform and how to exploit many of its innovative features. The book is easy to read and not intimidating for those less experienced with programming. It is a complete exposition of Groovy that addresses all aspects of the programming language.
The structure of the book fulfills this aim by presenting the basics of Groovy in the early chapters and more advanced concepts in the latter. In addition, extensive appendices consider more detailed aspects of the language.
Deliberately, each chapter is relatively small and easy to absorb, yet they contain a large number of complete code examples, extensive exercises, and solutions. To illustrate Groovy in application, the book features a rolling case study that grows in complexity and sophistication by drawing on the materials from each preceding chapter. In addition, incremental development and unit testing are central themes in the text and are necessary to support Groovys dynamic nature. The authors also consider Groovy as a multiparadigm language.
The authors own experience suggests that Groovy has a place in the academic curriculum as well as the experienced developers toolbox.
Have a fun time learning Groovy by reading this great book! You wont regret it.
Guillaume Laforge
Groovy Project Manager
JSR-241 Specification Lead
PREFACE
This book is an introduction to the scripting language Groovy. For Java developers, Groovy makes writing scripts and applications for the Java platform both fast and easy. It includes many language features found in other scripting languages such as Python, Ruby, and Smalltalk. As Groovy is based on Java, applications written in Groovy can make full use of the Java Application Programming Interfaces (APIs). This means that Groovy integrates seamlessly with frameworks and components written in Java.
Groovy, the scripting language, and Java, the systems programming language, complement each other. Both contribute to the development of programming applications. For example, components and frameworks might be created with Java and glued together with Groovy. The ease with which Groovy can make use of them significantly enhances their usage. The increasing importance of component architectures, Graphical User Interfaces (GUIs), database access, and the internet all increase the applicability of scripting in Groovy.
Groovy developers can take advantage of rapid application development features, such as those found in scripting languages. Groovy is suitable for many data or file processing tasks, testing applications, or as a replacement for Java in small- and medium-sized projects.
Next page