About This Book

AspectJ in Action is a practical guide to applying AspectJ to real-world problems. I cover a broad spectrum of solutions—from simple examples that address logging and tracing, to complex ones dealing with transactions and security. Regardless of your area of expertise, you are bound to find several examples that you can adapt to the challenges you face in your work.

AspectJ in Action is aimed at intermediate to advanced Java developers. Readers with a background in designing and building large systems will also find a good part of this book useful. While knowledge of object-oriented programming is desirable, I do not assume that you are familiar with aspect-oriented programming or AspectJ. For special topics, I provide sufficient background material and cite resources (both text and online) for those who want to gain an in-depth understanding.

Roadmap

This book is divided into three parts. If you are new to AOP and AspectJ, you should first read part 1 followed by at least a couple of chapters in part 2. Within part 3, you can read chapters in any sequence. If you find that one of the sections specifically addresses your current problem, start using the techniques I present, learn from the experience, and go from there. You can also choose to study all the other chapters and apply hybrid techniques to suit your current needs.

Most chapters in parts 2 and 3 follow a pattern of presenting a conventional solution followed by an AspectJ solution that implements the identical functionality. This pattern provides better understanding of the problem domain, and comparing the two solutions shows the effectiveness of AspectJ.

Part 1 introduces the aspect-oriented programming methodology and the AspectJ language.

Chapter 1 introduces the problems aspect-oriented programming aims to address and explains how it handles them. We discuss the concern decomposition of a system, the classification of concerns, and issues with current implementations of crosscutting concerns. We then show how AOP helps modularize those concerns.

Chapter 2 introduces the AspectJ programming language. We discuss the various language concepts and constructs. The join point model presented in this chapter is the most fundamental concept in AspectJ. We finish the chapter by briefly showing the IDE support for AspectJ.

Chapter 3 gets into the details of the AspectJ language by examining the concepts of pointcuts, advice, the introduction mechanism, and so forth. This chapter provides you with enough information to start writing simple AspectJ programs.

Chapter 4 shows the advanced concepts in AspectJ that you need to understand before you start writing nontrivial AspectJ programs.

Part 2 examines the real-world application of AspectJ using simple constructs.

Chapter 5 introduces logging and monitoring using AspectJ. We show how AspectJ includes logging in a system without touching any of its core modules. You?ll also see the ease with which you can switch between different logging APIs.

Chapter 6 shows how to enforce system wide contracts through policy-enforcement aspects. We offer a few simple examples that serve as building blocks. Then we describe an implementation of a UI application and EJB policy enforcement.

Chapter 7 examines how AspectJ can modularize the optimization concerns of pooling and caching. We study a generic template and utilize it to address the concrete challenges of JDBC connection and thread pooling. We finish the chapter with a caching example.

Part 3 examines the advanced application of AspectJ. You must have a good understanding of AspectJ before reading this part of the book.

Chapter 8 introduces a few brand-new AspectJ patterns. We also show a few idioms to avoid certain common pitfalls. Some of these patterns are original contributions from the author. This chapter is required reading before you tackle any of the remaining chapters in part 3, because all of the chapters use one or more of the patterns we present.

Chapter 9 addresses the modularization of thread safety using AspectJ. We specifically address two problems: the thread safety of Swing applications and the read-write lock pattern.

Chapter 10 examines the use of AspectJ for authentication and authorization. We utilize JAAS to implement the underlying authentication and authorization functionality and use AspectJ to achieve modularization.

Chapter 11 explains how AspectJ can separate the transaction concern from the core concern. We examine a JDBC-based as well as a JTA-based transaction.

Chapter 12 shows a novel application of AspectJ—we utilize it to modularize business rule implementations. We discuss AspectJ-based solutions that use plain Java as well as a rule engine (Jess) that evaluates the business rules.

Chapter 13 rounds out the book by showing a pragmatic approach to adopting AspectJ.

The two appendices explain, in detail, how to use the AspectJ compiler and AspectJ/Ant integration. In "Resources," you will find a wealth of information, both text and online, related to AspectJ.

Packages and tools used

The examples in this book use the following external tools and packages. The number in parentheses indicates the version I used for testing?a newer compatible version of these packages should work as well. I will attempt to provide updated source code that you can download from the book?s source code download site (see the ?Source code? section) whenever significantly newer versions of the packages are released:

Source code

The source code for the example applications in this book is freely available from Manning?s web site, http://www.manning.com/laddad. Much of the source code is reusable either in its original state or after some customization. The download package contains the source code, instructions on how to obtain the required external packages and set up the test environment, and scripts that automate compiling and running the programs.

Typographical conventions

Author Online

The purchase of AspectJ in Action includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to http://www.manning.com/laddad. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning?s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher?s web site as long as the book is in print.

About the author

Ramnivas Laddad is a Sun Certified Architect of Java Technology. He has worked with object-oriented systems for over a decade and with aspect-oriented programming for the past three years. He is the author of several articles and papers and co-author of Professional Java XML (Wrox Press, 2001). His series of articles on AOP and AspectJ was published in JavaWorld. He lives in Sunnyvale, California.

About the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, retelling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action guide is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

About the cover

The figure on the cover of AspectJ in Action is an ?Ysleno Moluco,? an inhabitant of the Molucan Islands, also known as the Spice Islands, a southwestern province of Indonesia. The illustration is taken from a Spanish compendium of regional dress customs first published in Madrid in 1799.

The title page of the Spanish compendium states:

Coleccion general de los Trages que usan actualmente todas las Nacionas del Mundo desubierto, dibujados y grabados con la mayor exactitud por R.M.V.A.R. Obra muy util y en special para los que tienen la del viajero universal

which we translate, as literally as possible, thus:

General collection of costumes currently used in the nations of the known world, designed and printed with great exactitude by R.M.V.A.R. This work is very useful especially for those who hold themselves to be universal travelers

Although nothing is known of the designers, engravers, and workers who colored this illustration by hand, the "exactitude" of their execution is evident in this drawing. The "Ysleno Moluco" is just one of many figures in this colorful collection. Their diversity speaks vividly of the uniqueness and individuality of the world?s towns and regions just 200 years ago. This was a time when the dress codes of two regions separated by a few dozen miles identified people uniquely as belonging to one or the other. The collection brings to life a sense of isolation and distance of that period—and of every other historic period except our own hyperkinetic present.

Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

We at Manning celebrate the inventiveness, the initiative, and the fun of the computer business with book covers based on the rich diversity of regional life of two centuries ago? brought back to life by the pictures from this collection.