Friday, August 30, 2013

Aspect Oriented Programming (AOP) using CDI for Java EE

As defined by wikipedia, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development. (Aspect oriented programming)
Using AOP we break down our program logic into distinct parts called concerns. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. 
Certain terms and terminology commonly used in AOP are:-
  • Aspect: a modularization of a concern that cuts across multiple classes.
  • Join point: a point during the execution of a program
  • Advice: action taken by an aspect at a particular join point.
AOP has been very common in Spring Framework and is also available in Context & Dependency Injection (CDI) for Java EE. 
The benefits of using AOP are:-
  1. modularization of system level coding i.e. logging, transaction, security 
  2. minimal coupling and duplication of code making it easier to add new functionalities.
  3. faster development as developers can focus mainly on the business logic rather than thinking of cross-cutting concerns such as security, transaction, logging etc. The business logic looks clear and does not have logging, security, transactional details.

Some example areas where AOP can be used:-

  1. Logging - We can write aspect around methods so that we can log before and after states of a method without actually putting logging statements in the actual method.
  2. Transaction management - in applications where transaction has to be managed manually, we can write aspects to begin and commit or rollback transactions depending on success or failure of the business method.
  3. Security - we can apply method level security around a method. So we do not have to implement security and role check in every business method. Aspect can be written that will be fired before a method is invoked and incase the user is not authenticated it will return an exception.
  4. Checking and using cached data before data access from database - we can write aspects around a data access method which will access the cache to see if the data is present. Incase the data is present it will return the data from the cache. Otherwise it will make data access call to the database and also add the returned data to the existing cache. Sample application below will use this as an example. 

Sample Application

Since we are developing using CDI for Java EE, I will present a sample implementation of AOP and cross-cutting using Java EE. This application uses Glassfish 4 as the application server and using Java EE.
 In this application I will show how we can write an Aspect around a web service method. This aspect will be fired before and after the web service method and can be used for security validation as well as checking cached data before making a DB call.
 We have a RestFulService service, which handles RESTful calls. Method retrieveRecordByResource handles the @Get RESTful call and retrieves all the parameters that are sent as part of the RESTful service. 
We will create an aspect around this call, which will
  • monitor the variables coming as input to the REST call 
  • response returned as part of the REST call
  • will modify variables before the call reaches retrieveRecordByResource method.
Code for Advice:-

In the code above, we create an advice class and method checkCache, with annotation @AroundInvoke. @AroundInvoke defines an interceptor method that interposes on business methods. 
Line 42, calls the proceed api of InvocationContext. Before this call we see that we have extracted the input parameters that came as input from the REST service call. It also returns returnParam which is the output after the call to our business method retrieveRecordByResource. 
Line 31 to 42, we see that we have extracted input parameters before the call to business method and we can manipulate these variables. We can do logging, security check, transaction management etc. before actually calling the business method. 
Line 44, we have control of the output parameters and we can log or manipulate these parameters. 

Line 17, defines an annotation @CacheResource. We need to define an interface CacheResource for the same.


In simple terms this interface and annotation @CacheResource, link our advice and the business class.
The interceptor is defined in WEB-INF/beans.xml:-

This configuration defines our interceptor. 

Conclusion:-
AOP is a very useful and powerful feature of OO programming. CDI brings AOP and dependency injection (DI). We can integrate AOP with caching using caching frameworks like JCache or EhCache.
CDI is the Java standard for dependency injection and interception (AOP). You can read futher about the same in JSR 299.

No comments:

Post a Comment