Looking at Code


We now take a quick look at some AspectJ placeholder code for the application and license model part of our system. This code is presented just to give a concrete sense of how this design might be carried out. The approach used to map the themes to implementation is slightly different from that described in Chapter 7. In that chapter, abstract aspects were used to capture the separation between the template methods (which became abstract pointcuts) and the bind specifications (which became concrete pointcuts).

Here, for convenience, we use concrete aspects and concrete pointcuts to mix the semantics of the Theme/UML templates and binding specifications. Of course, we lose the separation that the abstract pointcut approach gives us. First, we describe the base class, and then the AspectJ aspects used to implement the licensing model theme and the enforce theme.

Listing 9-1 shows some placeholder AspectJ code for the base application. It defines a simple class, Application, that contains one method, start().

Listing 9-1. Application Theme as Core AspectJ Code
 public class Application {  public static void main(String [] args) {     Application a = new Application();     a.start ();  }  public void start(){     System.out.println("Request Start");  } } 

Listing 9-2 provides an outline for a generic license model theme as it would appear in AspectJ as an aspect. Here, we have defined a concrete pointcut (checkModel()) that specifies application start-up as the joinpoint of interest. The checkModel() before advice occurs on execution of the Application.start() method. The model theme contains one method, check(), that performs all the necessary checking for the validity of the usage license. It returns true or false, depending on the results of this check. Another option would have been to place the license-usage checking inside the checkMethod() advice. We discuss the implications of each option when we go over the next aspect, Enforce.

Listing 9-2. Generic Model Theme as an AspectJ Aspect
 public aspect Model {  pointcut checkModel(): execution                         (* Application.start (..));  before(): checkModel(){     System.out.println("checking...");     check();  }  public boolean check(){     //perform all license usage checking     return resultOfChecking;  } } 

The enforce theme is described in Listing 9-3 as an AspectJ aspect. It contains one after returning advice on its enforce() pointcut. The enforce pointcut is bound to the execution of the Model.check() method, which is defined in the Model aspect shown in Listing 9-2. Had we chosen to place the license-usage checking directly inside the checkModel() before advice, we would have had to use a different construct, the pointcut declaration, to define the pointcut:

 pointcut enforce: adviceexecution() && within(Model); 

adviceexecution() is a construct that selects all advice execution joinpoints. The within(Model) specification denotes that the advice that is being executed should be within the Model aspect (it selects all execution within the named class or aspect). The above enforce pointcut designator thus selects all the joinpoints that are the execution of advice within the Model aspect. This construct is useful to us only if there is just one advice in the aspect. At the time of writing, there is no way to specify which advice execution joinpoint to select based on its pointcut name.[1] In this case, that is not a problem, since Model has only one advice. But in general, we would choose to place aspect functionality that we want to crosscut into a method rather than straight into the advice.

[1] It is likely that in the future, adviceexecution will be able to identify specific advice through use of annotations. Still, the approach suggested here (using a method to capture advice behavior) is probably preferred.

In the Enforce aspect, an after returning advice is used. The result returned by the Model.check(..) method is captured, and action can be taken depending whether the result is true or false. If the check fails (returns an invalid result), then a system call to exit() is made to stop execution of the application. Otherwise, application start-up is allowed to continue.

Listing 9-3. Generic Enforce Theme as an AspectJ Aspect
 public aspect Enforce {  pointcut enforce(): execution (boolean Model.check(..));  after() returning(boolean valid) : enforce(){     if(!valid) {        System.out.println("Your License Has Expired");        System.exit(1);     }     else{        System.out.println("You have a valid license");     }     return valid; }} 



Aspect-Oriented Analysis and Design(c) The Theme Approach
Aspect-Oriented Analysis and Design: The Theme Approach
ISBN: 0321246748
EAN: 2147483647
Year: 2006
Pages: 109

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