Add around advice to servlet in GAE using AspectJ

Today, we will discuss about how to incorporate AspectJ in Google App Engine project. The AspectJ advices will be shown with example code.

1. First of all, download and install Google App Engine plugin for Eclipse. I am using eclipse juno.

2. Install AspectJ package (AJDT-AspectJ Development toolkit) in eclipse using this link.

3. Now, we have to add AspectJ support to Google App Engine. To add AspectJ support,

a). Go to lib folder of project and copy-paste two aspectJ library files to it.

Download latest aspectj-*.*.* from here. (As of now its aspectj-1.7.2.jar, I am using 1.7.1 version)
Download latest aspectjrt.jar from here. Both file just copy paste in shown folder. Don't do it from outside of eclipse. Use eclipse IDE for copy paste.



b). Now right click on project and go to configure and select > Convert to AspectJ project.


c). Nowthere will be already one default class in GAE project.

Sample code is given below. 
 package com.gae.aspectj.test;  
 import java.io.IOException;  
   
 import javax.servlet.ServletException;  
 import javax.servlet.http.*;  
   
 @SuppressWarnings("serial")  
 public class GAE_AspectJServlet extends HttpServlet {  
      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{  
           resp.setContentType("text/plain");  
           resp.getWriter().println("Hello, world");  
      }  
 }  
   

Add one more class file to same folder of above servlet.

Sample code is given below. See the class definition, its not a class but aspect. 

 package com.gae.aspectj.test;  
 import java.io.IOException;  
   
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import com.gae.aspectj.test.GAE_AspectJServlet;  
 public aspect aspectj {  
        
      pointcut call_Process(HttpServletRequest req, HttpServletResponse resp)  :   
           (execution (void GAE_AspectJServlet.doGet(HttpServletRequest, HttpServletResponse)) && args(req,resp));  
      void around(HttpServletRequest req, HttpServletResponse resp)   
                throws ServletException,IOException :call_Process(req, resp)  
      {  
           resp.getWriter().println("Hello, world from aspectJ");  
           proceed(req,resp);  
           resp.getWriter().println("Hello, world from aspectJ");  
      }  
 }  

It may give errors while you write this aspect code. 

So, right click file and select Open With>AspectJ/Java Editor.

d). Run the program using Google App Engine.

The output will be as shown below.


Here around advice is used. In output you can see that hello world from aspectj comes first.

Since it is around advice, it will not call actual class until aspect writes / executes proceed function with appropriate arguments.

Because of that, the aspectJ output came first and last. Between that actual code runs.

The following line is important in aspect, since it captures doGet method of target servlet.
You can also use wildcards to apply aspect to multiple servlet files.

 (execution (void GAE_AspectJServlet.doGet(HttpServletRequest, HttpServletResponse)) && args(req,resp));   

For any queries please leave a comment.




Comments

Popular posts from this blog

ICT dialler installation

Install Pear in Wamp Server 2.2 (with php 5.3.8) for windows

Upload multiple files using AjaxFileUpload