Aspect Oriented Programming - A Different Aspect.
Yesterday I was having a meeting about a new project startup. At some level the meeting was stuck on an argument of logging. Well I suggested the same thing as any experienced developer would suggest... "Why don't we use the Aspects?".
Our project is running on Websphere Application Server and using JDK 1.4 which means we won't be using EJB3 or annotations. So I just tried AspectJ - although I found the examples were confusing, what I achieved was very simple and easy. Here is a basic tutorial on how to enable and using Aspects.
Let's say we have a web application running on any server consisting of several servlets and we want to know the count of servlet calls and we also want to log the parameters received. If we are in the beginning of the project we can just copy several lines of codes to all doGet or doPost methods. Seems easy but what if the project is at the end we need to add that feature and what if the required parameters are subject to change. In this case everytime we need to visit every single doGet method and change everything again.
Aspects are interceptors - you may just imagine registering an invisible listener to the methods you want to watch and Aspects just do whatever you need. What makes them great for logging is that you just register (or give a naming pattern) of the methods and from there the Aspects will watch execution or exiting of your methods. So lets code..
This our test servlet, so just imagine we have hundreds of those with different parameters and codes.
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AspectTest extends HttpServlet implements Servlet {
public AspectTest() {
super();
}
public void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
System.out.println("*******************servlet running");
}
}
Now lets enable AspectJ, I assume you are using Eclipse and already installed AspectJ plugin. If not just use the Eclipse update site.
Right click your project and click to Convert to AspectJ Project. Now we have added AspectJ support. Next click select New > Aspect. We can code our aspect now.
public aspect AutoLog {
pointcut loggableCalls() : execution(public * *.doGet(..));
before() : loggableCalls(){
System.out.println("***getting in "+ thisJoinPoint.getSignature().toString());
}
after() : loggableCalls(){
System.out.println("***getting out "+ thisJoinPoint.getSignature().toString());
}
}So simply, the pointcut just captures all doGet methods of all classes in all packages and then before and after methods just prints the class and method name. We can now deploy our project on server and test our servlet.
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting in void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O *******************servlet running
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting out void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
Now lets get more advanced and log all parameters going into our servlet. We only need to change our before block.
before() : loggableCalls(){
System.out.println("***getting in "+ thisJoinPoint.getSignature().toString());
Object[] obj = thisJoinPoint.getArgs();
HttpServletRequest request=(HttpServletRequest)obj[0];
Enumeration enumeration=request.getParameterNames();
while (enumeration.hasMoreElements()){
String param=enumeration.nextElement().toString();
System.out.println(param+": "+request.getParameter(param));
}
}After we publish, lets run the servlet with some parameters...
https://localhost:9444/AspectWEB/AspectTest?a=aa&b=bb&c=mneokjnfvjr&d=nnnnnıerhvnj
And the output is..
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting in void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O b: bb
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O a: aa
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O d: nnnnnierhvnj
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O c: mneokjnfvjr
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O *******************servlet running
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting out void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
Very simple and very efficient, don't worry about the future requests of your boss or customer. All you need to change is either the before or after block. If this is not agile can please someone tell me what is?
- Login or register to post comments
- 3770 reads
- Flag as offensive
- Email this Story
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)







Comments
sumit kishore replied on Thu, 2008/07/24 - 12:05pm
William Louth replied on Thu, 2008/07/24 - 4:43pm
For what you are doing you might as well use a ServletFilter.
Does the world need another AOP solution that uses logging? NO, the 1 million other implementations are more than enough even though to date I have yet to see an actual good implementation published.
AOP promised to reduce so much code clutter but in the end it only replicated the clutter on a much grander scale.
William
yenerm replied on Thu, 2008/07/24 - 6:10pm
in response to: sumitkishore
yenerm replied on Thu, 2008/07/24 - 6:20pm
in response to: wl88194
Yes I could use similar initializers if i used EJB, or any similar thing if any other technology. I wonder what is wrong to see aspect way instead of the servletfilter way.
AOP is nice but i dont believe you can just depand everything on it. AOP is just like garlic, if you use some it is nice, to much just stinks.. and if you just use it for vampires than you are just dreaming...
tsurdilo replied on Thu, 2008/07/24 - 9:08pm
I think promoting AspectJ for any development efforts, no matter how small, is a good thing. However, I find the provide example very incomplete and many details are hidden by the statement that you are using Eclipse with the AspectJ plugin. How do I use this aspect outside of Eclipse? Do you explain the core concepts of AspectJ - like what is a joinpoint, a pointcut, advice and what is a crosscutting concern??? Do you explain that you are using compile-time weaving of your aspect into your code and that it means having to compile your code with the AspectJ ajc compiler? Do you explain that it is also possible to load-time weave your aspect into the code and what the tradeoffs are? The reason to use AOP is to provide robust reusable implementations for crosscutting concerts. Logging is one of these concerns of course and used by so many articles as the simplest example of how to implement AOP. The logging solution shown here is somewhat misleading that the pointcut expression covers only a small part of what logging in an application really is.
Since you mention this is work-related, I think next time it would be better to first complete your solution before posting your code to read and use :)
Overall, I applaud the attempt to promote AspectJ.
yenerm replied on Fri, 2008/07/25 - 2:15am
contra replied on Fri, 2008/07/25 - 2:05am
William Louth replied on Fri, 2008/07/25 - 2:19am
And at the same time shoot themselves in the foot if applied similarly to their own code base because this little example has many potential issues with it (hint: re-entrant). If you are really going to use aspectj for such logging concerns you might as well fire up a debugger it would be much more intuitive and easier to use and most importantly safe.
You would not believe the number of times I have come across performance issues because someone has deployed such an logging example to actually monitor performance or find a bug which magically disappears when such aspects are installed.
William
yenerm replied on Fri, 2008/07/25 - 2:31am
in response to: wl88194
William Louth replied on Fri, 2008/07/25 - 2:40am
in response to: yenerm
I have nothing against AspectJ I have written more custom aspects than most developers I know. AOP is extremely powerful technology but one must invest time and effort to clearly understand the concepts and the implications of particular design approaches and implementations. Some of this is covered in the AspectJ user guide which includes a simple tracing aspect which is explained much better here. Maybe you could have just referenced this chapter in the aspect docs and then showed how you extended the simple tracing aspect for you particular needs.
With AOP it is important you know what you are doing. With power comes responsibility. The current state of AOP runtimes and programming interfaces lends this technology more suitable to a system developer technology.
William Louth replied on Fri, 2008/07/25 - 2:42am
This is an extraordinary statement considering the code listed and the lack of information provided. There is nothing efficient about the code and I would be very very worried if such code would even be considered for a production environment. I fail to see what Agile has got to do with this.
Stas Ostapenko replied on Fri, 2008/07/25 - 4:11am
partyzant replied on Fri, 2008/07/25 - 5:06am
in response to: wl88194
For what you are doing you might as well use a ServletFilter.
Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)
Stas Ostapenko replied on Fri, 2008/07/25 - 5:14am
in response to: partyzant
Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)
I had the same feeling :) I wrote a blog entry about another one AOP usecase :
Search with Spring Hibernate Lucene and Aspect Oriented Programming in action
harry66 replied on Fri, 2008/07/25 - 6:17am
in response to: sumitkishore
"...what's so different...?"
It's a play on words...
harry66 replied on Fri, 2008/07/25 - 6:23am
in response to: daoway
Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)
I had the same feeling :) I wrote a blog entry about another one AOP usecase :
Search with Spring Hibernate Lucene and Aspect Oriented Programming in action
aspects can simplify implementing the observer pattern. Is the SpaceWars example still included in AspectJ?
Basically you set a pointcut to intercept your set methods (or even assignment to the field - then you don't even need to code get/set methods) and it inserts your notification code.