Flylib.com

Books Software

 
 
 

Language Versus Framework


Language Versus Framework

The designers of AspectJ elected to enhance the Java language to support AOP. Over the years , the AspectJ variant of Java has matured into a stable and complete AOP platform, with the AJDT offering excellent tool support.

However, changing the core Java language has met with a degree of criticism from some quarters . Some of the issues include the following:

  • Learning Java becomes more difficult because the additional keywords mean developers new to Java must embrace the concepts and semantics of both the OOP and AOP paradigms .

  • Compilation of Java and AOP hybrid languages requires the adoption of a new compiler.

  • Moving to an AOP language is an issue when looking to employ AOP on an existing project, since migration of the complete code base to the new language is required.

Recent AOP implementations take the approach of defining aspects externally to the Java application using a framework in order to circumvent these issues. Frameworks enable the superimposing of AOP concepts onto existing Java applications without the need to move the entire application to a new AOP compiler. These frameworks typically use XML configuration files or J2SE 5.0-style metadata annotations to specify pointcuts and advice within the main application, ensuring AOP-specific keywords are isolated from the application. Advice is defined as standard Java classes, referenced by the framework's configuration.

A main advantage of these frameworks is that they bring AOP to vanilla Java, thereby easing the task of adopting AOP techniques within existing systems.

AOP Framework Implementations

The number of open source AOP frameworks available is growing steadily as AOP continues to gain interest among the Java community. Table 11-1 lists a selection of the AOP frameworks available as open source.

Table 11-1. AOP Frameworks

Name

Description

Reference

AspectWerkz

A popular, easy-to-learn AOP framework for Java. Features include both bytecode and load-time weaving.

http://aspectwerkz.codehaus.org

JBoss AOP

The JBoss AOP framework forms part of the JBoss application server and offers load-time and runtime aspect weaving. The framework is available standalone.

http://www.jboss.org/products/aop

Nanning

Nanning Aspects is a simple and scalable AOP framework for Java.

http://nanning.codehaus.org

CAESAR

Offers new AOP language that is compatible with Java. The CAESAR language compiles to standard Java bytecode.

http://caesarj.org/

JAC

Java Aspect Components (JAC) is an open source project targeting the use of AOP for developing an aspect-oriented middleware layer.

http://jac.objectweb.org/

Spring

Spring is an extensive Java-based framework in its own right based on the popular inversion of control (IOC) pattern. Spring also offers an AOP framework or is configurable to work with external AOP implementations.

http://www.springframework.org


To contrast AspectJ against a framework AOP implementation, we look at the approach to defining aspects taken by AspectWerkz.


Introducing AspectWerkz

AspectWerkz is a dynamic AOP implementation for Java, jointly developed by Jonas Bonr and Alexandre Vasseur. The framework is open source and available for download from the Codehaus site at http://aspectwerkz.codehaus.org. Check the site for full details of the license.

The AspectWerkz framework is not as comprehensive as AspectJ, but has a reputation as a workable and easy to learn AOP implementation. Unlike AspectJ, AspectWerkz does not extend the Java language to support aspects but instead defines aspects using either an XML definition file or J2SE 5.0-style metadata annotations. These methods for defining aspects avoid the need for an AOP-aware Java compiler.

Note

Before the release of J2SE 5.0, AOP frameworks like AspectWerkz used XDoclet-style metatags embedded within Javadoc comment blocks. With metatags now supported by J2SE 5.0 as annotations, the AspectWerkz team has stated it intends to update the AOP framework to use the new syntax for defining metadata in Java code.


Chapter 6 provides an overview of metadata annotations .


To compare how the approach of the AspectWerkz framework to AOP differs from that of AspectJ, consider the code snippet in Listing 11-4, which depicts a plain Java implementation of the AuditingAspect from the AspectJ example.

Listing 11-4. AspectWerkz AuditingAspect Class
package stock;

import org.codehaus.aspectwerkz.joinpoint.JoinPoint;

public class AuditingAspect {

  public void beforeAddStockItem(JoinPoint joinPoint) {
    .
    .
    .
  }

  public void afterAddStockItem(JoinPoint joinPoint) {
    .
    .
    .
  }
}

The code snippet in Listing 11-4 is devoid of any AOP language constructs, although there are a few hints such as the JoinPoint class. We look first at how an external XML definition file transforms the class into a full-fledged aspect.

XML Aspect Definition

One of the first approaches of many of the AOP frameworks was to declare pointcut designators and the location of advice methods using XML definition files. Listing 11-5 shows a sample of an AspectWerkz definition file that turns the plain AuditingAspect Java class into an aspect.

Listing 11-5. XML Definition File aop.xml
<aspectwerkz>
  <system id="stockcontrol">
    <package name="stock">

<!-- Define the class implementing the aspect -->

<aspect class="stock.AuditingAspect"
              deployment-model="perInstance">

<!-- Named pointcut -->

<pointcut name="auditStockItem"
                   expression="call(* Warehouse.add(..))"/>

<!-- Bind aspect before method to pointcut -->

<advice name="beforeAddStockItem"
                 type="before"
                 bind-to="auditStockItem"/>

<!-- Bind aspect after method to pointcut -->

<advice name="afterAddStockItem"
                 type="after"
                 bind-to="auditStockItem"/>

      </aspect>
    </package>
  </system>
</aspectwerkz>

Comparing the aspect definition in the XML file against the example from AspectJ reveals many similarities. Most notable is the syntax for the pointcut declaration. For example, here is the declaration for the pointcut in AspectJ:

pointcut auditStockItem() :

call(* Warehouse.add(..));


Compare this to the corresponding declaration in the AspectWerkz configuration:

<pointcut name="auditStockItem"
          expression=

"call(* Warehouse.add(..))"/>


Note

Supporters of AspectJ emphasize that these definition files present a similar learning curve to the new language constructs of AspectJ. This is a valid argument, but the strength of AspectWerkz lies in its ability to bring AOP to an existing system without requiring a move to an AOP compiler.


Aspects as Metadata Annotations

Later versions of AspectWerkz offer an alternative mechanism for specifying aspects using metadata annotations. This method moves AspectWerkz closer to the AspectJ model by embedding AOP constructs as annotations within the language. The advantage of embedded annotations over an external XML file is that all code related to the aspect resides within a single file.

Listing 11-6 shows the AuditingAspect class but this time adorned with AspectWerkz annotations.

Listing 11-6. AuditingAspect Class with Metatags
package stock;

import org.codehaus.aspectwerkz.joinpoint.JoinPoint;

/**
 *

@Aspect

perInstance
 */
public class AuditingAspect {

  /**
   *

@Before

call(* Warehouse.add(..))
   */
  public void beforeAddStockItem(JoinPoint joinPoint) {
    .
    .
    .
  }

  /**
   *

@After

call(* Warehouse.add(..))
   */
  public void afterAddStockItem(JoinPoint joinPoint) {
    .
    .
    .
  }
}

This new version of the AuditingAspect now bears a strong resemblance to the AspectJ version.

Here is the declaration of the before advice from the AspectJ example:


before() :

auditStockItem() {...}

AspectWerkz produces a similar construct using metatags. The metadata also identifies the join points for the advice.

/**
 *

@Before

call(* Warehouse.add(..))
 */
public void beforeAddStockItem(JoinPoint joinPoint) {...}

Metadata annotations replace the previous XML definition file, although a smaller file is still required to alert the AspectWerkz framework to the presence of aspects within the application.

Precompilation of AOP metadata information into the application makes it available at runtime for the AspectWerkz weaver . However, moving to J2SE 5.0 removes the need for the precompilation step.

Tip

The introduction of metadata annotations with J2SE 5.0 offers other interesting advantages to AOP frameworks. In addition to supporting the annotation model used by AspectWerkz, annotations make excellent join point candidates because they are unambiguous. For example, the declaration call(@annotation * *(..)) offers the AOP developer more options when determining the location of join points within the application.


AspectWerkz Weaving Options

The framework supports several weaving schemes, including bytecode and load-time weaving. AspectWerkz refers to these weaving schemas in terms of an operating mode: offline and online modes.

Offline mode is compile-time weaving and is a two-step process in AspectWerkz. The first step is the standard compilation of all source code with javac . The final step is the weaving process. Here, the AspectWerkz framework weaves the class files of the main application with the advice from the pertinent aspects. The result is a set of woven classes that run under any JVM, 1.3 or higher.

The online mode is more interesting, offering dynamic load-time weaving. In this mode, AspectWerkz hooks itself directly into the class-loading mechanism of the JVM. From this unique vantage point, AspectWerkz monitors class-loading activity and intercepts classes as they are loaded, weaving in advice at the bytecode level on the fly.

Aspect-Oriented Middleware

AOP frameworks like AspectWerkz are gaining recognition for the advantages they bring to the development of enterprise software. The binding of application servers with AOP frameworks has given rise to a new term , aspect-oriented middleware .

The J2EE platform already takes care of many major system-level crosscutting concerns for the developer, providing services for handling threading, security, persistence, transactions, and component-location transparency. Despite this long list of services, the J2EE platform employs a static model for the management of crosscutting concerns. In comparison to AOP-based frameworks, the J2EE model is rigid and inflexible , with new concerns requiring ad hoc solutions. Aspects promise a fluid and dynamic model for development, and experts in the field believe aspects will revolutionize application server design and the way we develop enterprise software.

Major J2EE server vendors are investing in AOP technology in order to take advantage of the new dimension aspects bring to the middle tier . IBM sponsors AspectJ, and formal support for AspectJ in WebSphere and WebSphere Application Developer (WSAD) is expected in the near future. Meanwhile, BEA has hired the creators of AspectWerkz to add dynamic AOP capabilities to its JRockit JVM. JBoss is currently forging ahead and have come up with its own AOP framework, JBoss AOP, which is an integral part of its application server.

Aspect-oriented middleware is at the (cross) cutting edge and may in the future represent a fundamental change in the way we develop enterprise software.