Introducing AspectWerkz


AspectWerkz is a dynamic AOP implementation for Java, jointly developed by Jonas Bonér 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 >     <package name="stock">       <!-- Define the class implementing the aspect -->       <aspect                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.



    Rapid J2EE Development. An Adaptive Foundation for Enterprise Applications
    Rapid J2EEв„ў Development: An Adaptive Foundation for Enterprise Applications
    ISBN: 0131472208
    EAN: 2147483647
    Year: 2005
    Pages: 159
    Authors: Alan Monnox

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net