Brought to you by Adobe
- Adobe® Acrobat® 9 Pro Extended - a complete PDF solution
- Create interactive presentations
- Bring people & ideas together
- Communicate with impact
Featured White Papers
- Don't miss this enterprise mobility Webcast! (TechRepublic)
- Enterprise PBX buyer's guide (VoIP-News)
- 5 Strategies for Making Sales the Engine for Growth (AchieveGlobal)
Technology Industry
Industry: Email Alert RSS FeedAspect-oriented programming with AspectJ
IBM Systems Journal, July, 2005 by A. Colyer, A. Clement
public aspect SupervisorAlerts {
private static final Money
WITHDRAWAL_THRESHOLD = new Money (1000, 0);
private QueueConnectionFactory
connectionFactory;
...
pointcut withdrawal(BankAccount acc,
Money amount) :
execution(* withdrawal(Money)) &&
this(acc) &&
args(amount);
after(BankAccount account,
Money amountOfWithdrawal) returning :
withdrawal(account, amountOfWithdrawal) {
if (amountOfWithdrawal.greaterThan(
WITHDRAWAL_THRESHOLD)) {
sendMessage(
"Large withdrawal from account: " +
account + " : " + amountOfWithdrawal);
}
}
private void sendMessage (String messageText) {
...
}
This aspect could now be extended to encompass the other supervisor alerts that are required, keeping the whole alerting feature modularized and encapsulated. For example, given the requirement to alert a supervisor whenever an account operation fails with an InsufficientFundsException, or when an AuthorizationException is generated by any method in the banking package, then the aspect can be extended as follows:
pointcut accountOperation(BankAccount acct) :
execution(* *(..)) && this(acct);
after(BankAccount account)
throwing(InsufficientFundsException ex) :
accountOperation(account) {
sendMessage(
"Insufficient funds for transaction " +
"account" + account +
" : " + ex.getMessage( ));
}
pointcut bankingOperation( ) :
execution(* *(..)) && within(banking.*);
after( ) throwing(AuthorizationException ex):
bankingOperation( ) {
sendMessage ("Authorization failure: " +
ex.getMessage( ) );
}
If the logic to send supervisor alerts had not been encapsulated in the aspect in this way, there would be many places throughout the banking application where fragments of code concerned with. implementing this feature would be found.
The AspectJ language includes many more features that can be used to improve the modularity of software applications, but a full treatment is beyond the scope of this paper. Interested readers are referred to the online AspectJ tutorial (8) or one of the many books on Aspect J, for example, References 9 and 10.
AJDT
AJDT provides IDE support for programming in the AspectJ language and is freely available from the Eclipse website. (3) Along with the usual syntax highlighting, building, and error-reporting elements, AJDT also provides a wealth of features that help users understand the effects of the aspects in their program. This part of the AJDT tool set provides aspect-browsing capabilities, similar to the class-browsing capabilities that are available for object-oriented programs.
Figure 1 shows a screenshot of AJDT in use. Both the BankAccount class and the SupervisorAlerts aspect are being edited, and the syntax highlighting that AJDT provides can be seen. Notice the markers in the gutter to the left of the BankAccount editor that indicate the presence of advice on a join point that the marked code will give rise to at runtime. In this case, when the withdrawal method is executed, it will give rise to an execution join point that is advised by the SupervisorAlerts aspect. The Outline View to the right of the Eclipse window shows an outline for the SupervisorAlerts aspect. In addition to indicating the members of the aspect (the fields, methods, advice, and pointcuts in this case), the Outline View shows the places that a piece of advice is in effect (the advises relationship). The links can be used for navigation to the advised locations. The Outline View for an advised type also provides advised by relationships that allow the developer to see and to navigate to any advice affecting the type.
