FTP Online
Search:
Locator+ Code:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Make Your Applications Operations Friendly With AOP (Continued)

AOP is a programming technique that modularizes concerns (areas of interest or requirements) that cut across a system and are common to many modules. These modules, called aspects, associate pointcuts and advices. Pointcuts identify joinpoints, which are well-defined points in the flow of execution such as a method call or data variable access. Advices, which are associated with specific pointcuts, specify the code to execute at a joinpoint. Put another way, a pointcut declares an event and an advice declares an action to be performed when an event fires (see Table 1).

The problem with crosscutting concerns such as logging is that they are tangled with the application's core concern and scattered over many modules, whether it's credit card processing, online insurance, or whatever. The result: applications that are harder to develop and maintain. AOP provides a mechanism to modularize these crosscutting concerns.

AOP Implementations
Two open-source AOP projects are available for download: AspectJ and JBossAOP (see Resources). These two products implement AOP differently, but they both weave crosscutting behavior into a plain old Java object (POJO).

AspectJ
Xerox Palo Alto Research Center (PARC) decided to create a general-purpose AOP language and developed AspectJ over a six-year period. In December 2002, PARC transferred control of AspectJ to the Eclipse project to encourage growth of the AspectJ technology and community.

Available at the Eclipse Web site, the AspectJ download includes the AspectJ Compiler, an aspect browser, Ant tasks, and documentation. The AspectJ Browser provides a point-and-click interface to compile the Java classes and aspects you've defined to a build configuration. It compiles Java classes and aspects using the ajc compiler, which contains a weaver that interleaves the pointcuts and advices in the aspect with the Java classes.

AspectJ generates classfiles that can run on any Java virtual machine. It also supports several Integrated Development Environments (IDEs): Eclipse, Borland JBuilder, and SunOne Studio 4/NetBeans. All code and documentation is available under the Mozilla license, and the Ant tasks are released under the Apache license. In short, you can download everything you need to equip a professional development environment.

Aspects are defined in a file that looks much like a Java class. The LogPoints aspect, for example, outputs a Jakarta Commons log entry before and after any class's init() and doGet() methods, thereby exposing the lifecycle of any running servlet. The aspect file uses an aspect rather than a class identifier, but in all other ways it resembles a Java class. It contains an import statement for the Jakarta Commons Logging classes and a block of code with the advices and pointcuts enclosed within familiar curly braces:

// LogPoints Aspect
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 

aspect LogPoints {
      private static Log log = 
      LogFactory.getLog(EntryExitPoint.class);
      before(): call(public  *  init(..)) { 
	    log.info("Enter: " + thisJoinPoint);
      }
      after(): call(public * init(..)) {
            log.info("Exit: " + thisJoinPoint);
      }	
      before(): call(public  *  doGet(..)) { 
            log.info("Enter: " + thisJoinPoint);
      }
      after(): call(public * doGet(..)) {
            log.info("Exit: " + thisJoinPoint);
      }
}

Compiling and running this aspect in Jakarta Tomcat 4 together with the HelloWorldServlet class (see Listing 1) results in these log entries:

2004-02-28 13:46:56,574 INFO  EntryExitPoint : 
Enter: call(void HelloWorldServlet.
   init(ServletConfig))
2004-02-28 13:46:56,574 INFO  EntryExitPoint : 
Exit: call(void HelloWorldServlet.
   init(ServletConfig))
2004-02-28 13:46:56,574 INFO  EntryExitPoint : 
Enter: call(void HelloWorldServlet.doGet
   (HttpServletRequest, HttpServletResponse))
2004-02-28 13:46:56,589 INFO  EntryExitPoint : 
Exit: call(void HelloWorldServlet.doGet
   (HttpServletRequest, HttpServletResponse))

The important point is that even if there were 20 servlets executing, this aspect would output the same type of log entry for each one. This simple example shows you how it's possible to define a logging interface within a single module that cuts across an entire system.



Back to top



ADVERTISEMENT

Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home